#PTAOOP523. 5-23 创建一个Box类,求其体积
5-23 创建一个Box类,求其体积
本站提交说明
此题属于 面向对象程序设计练习。请在练习页填写原题的填空或函数片段并提交;本题独立提交页接收拼接后的完整 Java 程序。
评测使用题面样例与本站补充测试,包含编译、输出及部分成员 API 检查。通过本站测试不代表通过 PTA 隐藏测试。
设计并实现Box类,它包含盒子的高度、宽带和深度的实例数据:还包含布尔变量full这个实例数据,变量full表示盒子是否满了。所有数据都定义为私有。定义Box构造函数接收并初始化盒子的高度、宽带和深度。每次新创建的Box都为空,构造函数将full初始化为假。该类所有实例都有getter和setter方法;该类还包含了一个计算盒子体积的方法。 该类包含一个toString方法,用来返回一行关于盒子的描述。 例如,输入的长、宽、高分别为4,如果盒子为空,则输出: Box is empty, The Volumn is 64 若将盒子的宽度修改为5,盒子设置为满,则输出: Box is full, The Volumn is 80 创建一个测试类BoxTest,其main方法输入盒子的长、宽、高,实例化一个Box对象,并输出盒子的描述。 根据以下的测试类,在空格出填入Box类的设计程序。
class {
private boolean ;
private int width;
private int height;
private int lenght;
public Box() {
full = false;
this.width = width;
this.height = height;
this.lenght = lenght;
}
public isFull() {
return full;
}
public void setFull(boolean full) {
this.full = full;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int h) {
this.height = height;
}
public int getLenght() {
return lenght;
}
public void setLenght(int lenght) {
this.lenght = lenght;
}
public int getVolumn() {
}
@Override
public String {
return "Box is " + (full ? "full" : "empty") + ", The Volumn is " + getVolumn();
}
}
public class Main {
public main(String[] args) {
Scanner input = new Scanner(System.in);
int w = input.nextInt();
int h = input.nextInt();
int l = input.nextInt();
Box box1 =
System.out.println(box1.toString());
box1.setWidth(5);
box1.setFull();
System.out.println(box1.toString());
}
}
本站测试模板说明
- 完整示例修正原题 setHeight 的笔误 this.height=height 为 this.height=h;逐空答案仍按原填空位置。
- 题面代码围栏有缺失或语言标签错误,完整示例按 Java 源码提取。