谢谢。利用java编程。创建一个Box类,在其中定义三个变量表示一个立方体的长,宽,高,再定义一个方法setD

2025-01-05 03:25:46
推荐回答(4个)
回答1:

你的这个问题 是你定义类的问题,java虽然大小敏感,但是根据命名规范 一般类名首字母需要大写,你2个类定义为同名,就是首字母大小写不同,建议你改下。当然文件名得跟public类名一样
class Box{

private int x,y,z;

public void setDemo(){
x=3;
y=4;
z=5;

}

public void calculate(int x,int y,int z){
int t;
t=x*y*z;
System.out.println(t);

}

}

public class Test{

public static void main(String[] args) {
int t;
Box d=new Box();
d.setDemo();
d.calculate(3,4,5);

}
}

回答2:

class Box{
private double length;
private double width;
private double height;

public void setDemo(){
this.length = 3;
this.width = 4;
this.height = 5;
}

//计算长方体的体积
public double getVolume(){
return this.length * this.width * this.height;
}
}
public class Test11 {
public static void main(String[] args) {
Box b = new Box();
b.setDemo();
System.out.println("立方体 b 的体积是 " + b.getVolume());
}
}

回答3:

ivate int data;
private String str;

public A(){
data = 0;
str = "";
}

public A(int data,String str){
this.data = data;
this.str = str;
}

public void add(int k,String s){
this.data+=k;
this.str+=s;
}

public void clear(){
this.data = 0;
this.str = "";
}

public String toString(){
String s = data+"";
s+=str;
return (s);
}

public static void main(String args[]){
A a = new A();
a.add(10,"Hello");
System.out.println("data is:"+a.data+" str is:"+a.str);
System.out.println(a.toString());
a.clear();
A b = new A(199,"nihao");
System.out.println("data is:"+b.data+" str is:"+b.str);
System.out.println(b.toString());
b.add(200, " dajiahao");System.out.println("data is:"+b.data+" str is:"+b.str);
System.out.println(b.toString());
}
}

回答4:

public class Box
{
private double length;
private double width;
private double height;

public void setD()
{
this.length=3;
this.width=4;
this.height=5;
}

public double cal_volume()
{
return(this.length*this.width*this.height);
}

public static void main(String[] args)
{
Box b=new Box();
b.setD();
System.out.println("the volume of the cuboid is "+b.cal_volume());

}

}