java 实验 2

创建一个桌子Table 类,该类中有桌子名称、重量、桌面宽度、长度
及桌子高度属性。其中有:
a.构造函数初始化所有数据成员
b.Area() :计算桌面的面积
c.Display(): 在屏幕上输出所有数据成员的值
d.ChangeWeight(int ):改变桌子重量的函数
e.在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,
并在屏幕上输出所有桌子数据成员的值。

 

public class Table{
 private String tableName;
 private float tableWeight;
 private float tableLength;
 private float tableWidth;
 private float tableHeigth;
 
 public Table(String name,float weight,float length,float width,float heigth)
 {
  tableName=name;
  tableWeight=weight;
  tableLength=length;
  tableWidth=width;
  tableHeigth=heigth;
 }
 
 public void area()
 {
  System.out.println("the area is:"+tableLength*tableWidth); 
 }
 
 public void display()
 {
  System.out.println("Name:"+tableName+",Weight:"+tableWeight+",Length:"+tableLength+",Width:"+tableWidth+
   ",Height:"+tableHeigth);
 }
 
 public void changeWeight(int w)
 {
  tableWeight=w;
 }
  
}

class Test{
 public static void main(String [] args){
  Table tb=new Table("haha",20f,10f,10f,1.5f);
  tb.area();
  System.out.println("the value of table is:");
  tb.display();
  tb.changeWeight(666);
  System.out.println("!now the value of table is:");
  tb.display();
 }
}

posted @ 2009-03-18 20:34  ::米麒麟::  阅读(225)  评论(0编辑  收藏  举报