日期:2014-05-20  浏览次数:20850 次

一个类的实验,我运行到了.但老师说没按照他的要求(要什么构造方法.不懂)帮忙看看啊,谢!
创建一个桌子Table类,该类中有桌子名称     重量     桌面宽度     长度和桌子高度属性,
以及以下几个方法:
  (1)   构造方法:初始化所有成员变量.
  (2)area():计算桌面的面积.
  (3):display():在屏幕上输出所有成员变量的值.
changWeight(int   w):改变桌子的重量.
在main()方法中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在
屏幕上输出所有桌子属性的值.


------解决方案--------------------
public class Table
{
private String name;
private double weight;
private double width;
private double longth;
private double height;

public Table()
{
this.name = "Defualt ";
this.weight = 50D;
this.longth = 100D;
this.height = 100D;
this.width = 100D;
}

public Table(String name, double weight, double width, double longth,double height)
{
this.name = name;
this.weight = weight;
this.longth = longth;
this.height = height;
this.width = width;
}

public void area()
{
System.out.println(width * longth);
}

public void changWeight(double weight)
{
this.weight = weight;
}

public void display()
{
System.out.println( "name: " + name);
System.out.println( "weight: " + weight);
System.out.println( "width: " + width);
System.out.println( "longth: " + longth);
System.out.println( "height: " + height);

}

public static void main(String s[])
{
Table t1 = new Table();
t1.area();
t1.changWeight(100);
t1.display();

Table t2 = new Table( "新建桌子 ", 50D, 70D, 80D, 90D);
t2.area();
t2.changWeight(100);
t2.display();
}

}

如果你们老师对以上这个不满意你来找我!!!

给分吧!最好是所有的分 *^_^*