1,创建一个Recagle 类,添加width 和height 两个成员变量。

2,在Rectangle 中添加两种方法分别计算矩形的周长和面积。

3,编程利用Red tangle 输出一个矩形的周长和面积。

 

package yaoqiu;
public class yaoqiu {
private int width;
private int height;
public static void main(String[] args) {
yaoqiu rec = new yaoqiu(30,10);
System.out.println("这个矩形的周长是:"+rec.getPerimeter());
System.out.println("这个矩形的面积是:"+rec.getArea());
}
public int getPerimeter()
{
return this.width *2 + this.height *2;
}


public int getArea()
{
return this.width * this.height;
}


public yaoqiu(int width,int height) 
{
super();
this.width = width;
this.height = height;
}
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;
}


}



按要求编程22

相关文章: