按要求编写程序

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

package com.task01;
//矩形类
public class Rectangle {
private int width;
private int height;
public static void main(String[] args) {
//声明变量
Rectangle rec = new Rectangle(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 Rectangle(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;
}
}

编译结果
按要求编写程序_第1张图片

你可能感兴趣的:(按要求编写程序)