求矩形的周长和面积

一 需求分析 求矩形的周长和面积

二 功能设计 

编写程序,定义一个矩形类,具有长、宽、面积、周长共四个成员变量,计算面积和计算周长的两个方法,在主类中创建矩形对象,输入长和宽,输出矩形的面积和周长。

三 代码 

 

class tri {
float longth;
float width;
float area;
float circum;

public tri(float longth, float width) {
this.longth = longth;
this.width = width;
}
public float getArea() {
area = longth * width;
return area;
}

public float getCircum() {

circum = (longth + width) * 2;
return circum;

}

}

public class Num {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入长:");
float longth = sc.nextFloat();

System.out.println("请输入宽:");
float width = sc.nextFloat();
tri test = new tri(longth, width);
System.out.println("矩形的周长是" + test.getCircum());
System.out.println("矩形的面积是" + test.getArea());
}
}

四 测试运行

posted on 2019-09-18 18:33  吴思  阅读(957)  评论(0)    收藏  举报

导航