作业帮 > 综合 > 作业

java:创建一个桌子Table类,该类中有桌子名称,重量,桌面宽度,长度及桌子高度属性.

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/06/02 15:00:15
java:创建一个桌子Table类,该类中有桌子名称,重量,桌面宽度,长度及桌子高度属性.
其中有:
1) 编写一个无参的构造方法和带4个参数的构造方法初始化所有数据成员
2) int area() :计算桌面的面积
3) void display():在屏幕上输出所有数据成员的值
4) 建立每个属性的get和set方法
注意:桌子的重量,宽度、长度和高度初始化时不能为负数,改变属性值时也不能为负数.
我是java初学者,老师不值得作业,有点迷茫,
public class Table {
// 名称
private String name;
// 重量
private float weight;
// 宽度
private float width;
// 高度
private float height;
// 长度
private float length;
public Table() {
}
// 带4个参数的构造方法初始化所有数据成员
public Table(float weight, float width, float height, float length) {
super();
if (weight < 0 || height < 0 || width < 0 || length < 0) {
System.out.println("桌子的重量,宽度、长度和高度初始化时不能为负数");
} else {
this.weight = weight;
this.width = width;
this.height = height;
this.length = length;
}
}
// 计算桌面面积
public int area() {
return (int) (this.length * this.width);
}
public void display() {
System.out.println("名字:" + name + ";桌面长度:" + length + ";桌面宽度:" + width
+ ";重量:" + weight+";高度:"+height);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
if (weight < 0) {
System.out.println("桌子的重量不能为负数");
} else {
this.weight = weight;
}
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
if (width < 0) {
System.out.println("桌子的宽度不能为负数");
} else {
this.width = width;
}
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
if (height < 0) {
System.out.println("桌子的高度不能为负数");
} else {
this.height = height;
}
}
public float getLength() {
return length;
}
public void setLength(float length) {
if (height < 0) {
System.out.println("桌子的长度不能为负数");
} else {
this.length = length;
}
}
}
另外 5个属性 4个参数 初始化全部 不能理解