/*定义一个长方形类MyBox并在另一个BoxTest类中使用。
MyBox类:
成员变量:宽度: width 长度: length
构造方法: MyBox(double width,double length)
成员方法:计算面积、计算周长、修改长度、修改宽度
BoxTest类:
成员方法:
main方法*/
public class boxtest {
public static void main(String[] args) {
mybox box = new mybox(3.0,5.0);
System.out.println("area: " + box.area());
System.out.println("circumference: " + box.circumference());
mybox box1=new mybox(20.0,32.0);
System.out.println("修改长度和宽度后");
System.out.println("area: " + box1.area());
System.out.println("circumference: " + box1.circumference());
}
}
2
/*定义一个长方形类MyBox并在另一个BoxTest类中使用。
MyBox类:
成员变量:宽度: width 长度: length
构造方法: MyBox(double width,double length)
成员方法:计算面积、计算周长、修改长度、修改宽度
BoxTest类:
成员方法:
main方法*/
public class mybox {
private double width;
private double length;
// 构造方法
public mybox(double width, double length) {
this.width = width;
this.length = length;
}
//area and circumference
public double area() {
return width * length;
}
public double circumference() {
return 2 * (width + length);
}
//Modify the length and width
public void Modifylength(double length) {
this.length = length;
}
public void Modifywidth(double width) {
this.width = width;
}
}