1. 程式人生 > >1.建立一個Rectangle類,新增width和height兩個成員變數。 2.在Rectangle中新增兩種方法分別計算矩形的周長和麵積 3.程式設計利用Rectangle輸出一個矩形的周

1.建立一個Rectangle類,新增width和height兩個成員變數。 2.在Rectangle中新增兩種方法分別計算矩形的周長和麵積 3.程式設計利用Rectangle輸出一個矩形的周

/*
 * 1.建立一個Rectangle類,新增width和height兩個成員變數。
 * 2.在Rectangle中新增兩種方法分別計算矩形的周長和麵積
 * 3.程式設計利用Rectangle輸出一個矩形的周長和麵積
 */
public class Rectangle {// 建立一個Rectangle類


double width, length;// 定義width和height兩個成員變數。
double area, zhou;


Rectangle(double x) { // 把兩個變數初始化為相同傳入值
width = x;
length = x;
}


Rectangle(double w, double len) { // 分別對兩個變數初始化不同的值
width = w;
length = len;
}


public double zhouRectangle() {
zhou = (width + length) * 2;
return zhou;
}
public double areaRectangle() {
area = width * length;
return area;
}
public static void main(String[] args) { // 宣告物件和建立物件
double c, s;
Rectangle Rectangle1 = new Rectangle(10, 20);
Rectangle Rectangle2 = new Rectangle(7);
c = Rectangle1.zhouRectangle();
s = Rectangle2.areaRectangle();
System.out.println("周長是:" + c);
System.out.println("面積是:" + s);


}
}