JAVA编程题目,求助大家帮忙解答!!!

按以下要求编写程序
(1) 创建一个Rectangle类,添加width和height两个成员变量
(2) 在Rectangle中添加两种方法分别计算矩形的周长和面积
(3) 编程利用Rectangle输出一个矩形的周长和面积

package test;


public class Rectangle {

private int width;// 宽

private int length;// 长


public int getWidth() {

return width;

}


public void setWidth(int width) {

this.width = width;

}


public int getLength() {

return length;

}


public void setLength(int length) {

this.length = length;

}


// 计算面积

public int getMJ(int length, int width) {

return (this.length * this.width);

}


// 计算周长

public int getZC(int length, int width) {

return (this.length + this.width) * 2;

}


public static void main(String[] args) {

Rectangle rt = new Rectangle();

// 设置长、宽的具体值

rt.setLength(5);

rt.setWidth(4);

// 输出周长和面积

System.out.println("周长为:" + rt.getZC(rt.getLength(), rt.getWidth()));

System.out.println("面积为:" + rt.getMJ(rt.getLength(), rt.getWidth()));

}

}


温馨提示:内容为网友见解,仅供参考
第1个回答  2016-10-18
方法我知道写 但是计算公式忘了。。麻烦说下公式追问

我也不懂公式

相似回答