创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度和桌子高度等属性以及以下几个方法:
构造方法:初始化所有成员变量
area():计算桌面的面积
display():在屏幕上输出所有成员变量的值
changeWeight(int w):改变桌子的重量
在main()方法中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出桌子属性的值。Java的函数学不懂呀,这么简单的都做不出来  哪位高手帮哈忙呀? 全过程哈  特别是构造方法

解决方案 »

  1.   


    package com.jwei.csdn;public class TestTable { private String name;

    private int weight;

    private int width;

    private int height;

    /***
     * 构造函数
     * @param String name
     * @param int heigth;
     * @param int width;
     * @param int weight;
     * **/
    public TestTable(String name, int height, int width,int weight){
    this.name = name;
    this.height = height;
    this.width = width;
    this.weight = weight;
    }

    /**
     * @return int  table 面积
     * **/
    public int getArea(){

    return this.height*this.width;
    } public String getName() {
    return name;
    }

    /***
     * @return string 所有的属性值, 其实也就是重写toString函数
     * **/
    public String display(){
    StringBuilder build = new StringBuilder();
    build.append("name:"+this.name+";");
    build.append("height:"+this.height+";");
    build.append("width:"+this.width+";");
    build.append("weight:"+this.weight+";");
    return build.toString();
    }

    public int changeWeight(int w){
    this.setWeight(w);
    return this.weight;
    } public void setName(String name) {
    this.name = name;
    } public int getWeight() {
    return weight;
    } public void setWeight(int weight) {
    this.weight = weight;
    } public int getWidth() {
    return width;
    } public void setWidth(int width) {
    this.width = width;
    } public int getHeight() {
    return height;
    } public void setHeight(int height) {
    this.height = height;
    }


    public static void main(String args[]){
    TestTable test = new TestTable("test", 20, 30, 40);
    System.out.println(test.getArea());
    System.out.println(test.display());
    System.out.println(test.changeWeight(50));
    }


    }
      

  2.   

    同意楼上,以后这种问题,请发java se 区。