java,类和对象的使用 要求:写一个表示立方体的类。 具体见下面描述,这 ...
发布网友
发布时间:2024-10-04 19:05
我来回答
共3个回答
热心网友
时间:2024-10-05 08:51
public class Test2{
public static void main(String[] args) {
Cube c1=new Cube(2.5);
double area = c1.area();
double volume = c1.volume();
System.out.println("area="+area+" volume="+volume);
Cube c2=new Cube("红色");
double area2 = c2.area();
double volume2 = c2.volume();
System.out.println("area="+area2+" volume="+volume2);
Cube c3=new Cube(3,4,5);
double area3 = c3.area();
double volume3 = c3.volume();
System.out.println("area="+area3+" volume="+volume3);
}
}
class Cube {
private double length = 2;
private double width = 3;
private double height = 5;
private String color;
public Cube(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public Cube(String color) {
super();
this.color = color;
}
public Cube(double height) {
super();
this.height = height;
}
public double volume() {
return length * width * height;
}
public double area() {
return 2*(length * width+length*height+width*height);
}
}
热心网友
时间:2024-10-05 08:47
public class Cube {
/**
* 长度
*/
private int length;
/**
* 宽度
*/
private int width;
/**
* 高度
*/
private int height;
/**
* 颜色
*/
private String color;
/**
* 默认高度
*/
public final static int HEIGHT = 10;
/**
* 默认长度
*/
public final static int LENGTH = 10;
/**
* 默认宽度
*/
public final static int WIDTH = 10;
/**
* 通过指定高创建立方体
* @param height
*/
public Cube(int height) {
this.length = Cube.LENGTH;
this.width = Cube.WIDTH;
this.height = height;
}
/**
* 通过指定长宽高创建立方体
* @param length
* @param width
* @param height
*/
public Cube(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
/**
* 通过指定颜色创建立方体
* @param color
*/
public Cube(String color) {
this.length = Cube.LENGTH;
this.width = Cube.WIDTH;
this.height = Cube.HEIGHT;
this.color = color;
}
/**
* 计算表面积
* @return
*/
public int superficialArea() {
return 2 * (this.height * this.length + this.height * this.width + this.width
* this.length);
}
/**
* 计算体积
* @return
*/
public int volume() {
return this.width * this.length * this.height;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public static void main(String[] args){
Cube cube = new Cube(1,1,1);
Cube cube2 = new Cube("red");
Cube cube3 = new Cube(5);
System.out.println(cube3.superficialArea());
System.out.println(cube3.volume());
}
}
热心网友
时间:2024-10-05 08:49
public class Cube {
private double length;
private double width;
private double height;
private String color;
public static void main(String[] args) {
//颜色
Cube c1 = new Cube("红色");
double g1 = c1.getGirth();
double a1 = c1.getArea();
//高
Cube c2 = new Cube(20.0);
double g2 = c2.getGirth();
double a2= c2.getArea();
//长宽高
Cube c3 = new Cube(20.0, 20.0, 20.0);
double g3 = c3.getGirth();
double a3 = c3.getArea();
}
//周长
public double getGirth(){
return 2*(length+width+height);
}
//面积
public double getArea(){
return length*width*height;
}
public Cube(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public Cube(String color) {
super();
this.color = color;
this.length = 10.0;
this.width = 10.0;
this.height = 10.0;
}
public Cube(double height) {
super();
this.height = height;
this.length = 10.0;
this.width = 10.0;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}