public?class?Calc?{
站在用戶的角度思考問題,與客戶深入溝通,找到盤錦網(wǎng)站設(shè)計(jì)與盤錦網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋盤錦地區(qū)。
int?s,m;
public?int?add(int?a,int?b)?{
s=a+b;
return?s;
}
public?int?mult(int?a,int?b)?{
m=a*b;
return?m;
}
public?static?void?main(String[]?args)?{
Calc??c?=?new?Calc();
System.out.println("2+5等于"+c.add(2,?5));
System.out.println("3*4等于"+c.mult(3,?4));
}
}
class A{//類A計(jì)算立方體體積 }class B{//類A計(jì)算球體體積 } class C{//類A計(jì)算圓柱體積 }//主類public class test{ static{ System.out.println("請輸入1、2、3對應(yīng)立方體、球體和圓柱的體積計(jì)算..."); }public static void main(String args[]){ if(args.length1 || Integer.parseint(args[0])1|| Integer.parseint(args[0])3){//判斷輸入是否符合標(biāo)準(zhǔn) System.out.println("錯(cuò)誤的選擇,程序自動(dòng)退出.."); System.exit(1); } else{ int x = Integer.parseint(args[0]) ; switch(x){ case 1: //調(diào)用A類 case 2: //調(diào)用B類 defualt: //調(diào)用C類 } } }}
下面是抽象類的代碼:
public abstract class ObjectVolume {
abstract double getVolume();
}
下面是球體類的代碼:
public class Sphere extends ObjectVolume {
@Override
double getVolume() {
return 85d;
}
}
下面是立方體類的代碼:
public class Cube extends ObjectVolume {
@Override
double getVolume() {
return 46d;
}
}
下面是圓柱體類的代碼:
public class Cylinder extends ObjectVolume {
@Override
double getVolume() {
return 92d;
}
}
下面是public類的代碼:
import java.util.Scanner;
public class PublicCommon {
static void get(ObjectVolume obj) {
System.out.println("體積為:" + obj.getVolume());
}
public static void main(String[] args) {
System.out.println("1.球體");
System.out.println("2.立方體");
System.out.println("3.圓柱體");
System.out.print("請選擇要表示的體積編號:");
Scanner scanner = new Scanner(System.in);
int select = scanner.nextInt();
if (select == 1) {
get(new Sphere());
} else if (select == 2) {
get(new Cube());
} else if (select == 3) {
get(new Cylinder());
} else {
System.out.println("輸入有誤。");
}
scanner.close();
}
}
麻煩您看一下,是否能夠滿足要求。
自己思考下 這個(gè)很簡單 真受不了你 自己思考比什么都好
class cube
{
private int bianchang;
public void setBianChang(int bc)
{
this.bianchang = bc;
}
public int getBianChang()
{
return this.bianchang;
}
public int getBiaoMianji()
{
return 6*bianchang*bianchang;
}
public int getTiji()
{
return bianchang * bianchang *bianchang;
}
}
public class Test
{
public static void main(String args[])
{
cube mycube = new cube();
mycube.setBianChang(6); //設(shè)置新邊長
int bianc = mycube.getBianChang();
System.out.println("邊長為:" + bianc);
System.out.println("表面積為:" + mycube.getBiaoMianji());
System.out.println("體積為:" + mycube.getTiji());
}
}
public?class?MyCube?{????//MyCube是類名,不是MyCude
double?width;??????????//類的數(shù)據(jù)成員
public?static?void?main(String[]?args)?{???//main方法
MyCube?cube=?new?MyCube(2.1);?????//調(diào)用MyCube(double)構(gòu)造函數(shù)創(chuàng)建類實(shí)例
System.out.println(?cube.calcVolume());???//調(diào)用方法calcVolume,printly改成println
}
MyCube(double?w){???//類的構(gòu)造函數(shù)
this.width?=?w;???//將參數(shù)賦給類實(shí)例的數(shù)據(jù)成員width
}
double?calcVolume(){??//計(jì)算立方體的體積
return?width*width*width;
}
}
注意文件名必須是MyCube.java
class Square{
double w=0;
double l=0;
Square(double w,double l){
this.w=w;
this.l=l;
}
}
public class Cube extends Square{
protected double h=0;
Cube(double w,double l,double h){
super(w,l);
this.h=h;
}
public double volume(){
return super.w*super.l*this.h;
}
public static void main(String[] args) {
Cube Cube1=new Cube(3,2,1);
Cube Cube2=new Cube(7,3,2);
System.out.println(Cube1.volume());
System.out.println(Cube2.volume());
}
}