設計一個名為Rectangle的類表示矩形。這個類包括: 兩個名為width和height的double型數據域,它們分別表示矩形的寬和高。width和height的默認值都為1. 一個無參構造方法。 一個為width和height指定值的矩形構造方法。 一個名為getArea()的方法返回這個矩形的面積。 一個名為getPerimeter()的方法返回這個矩形的周長。
類名為:
Rectangle
裁判測試程序樣例:
import java.util.Scanner; /* 你的代碼將被嵌入到這里 */ public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double w = input.nextDouble(); double h = input.nextDouble(); Rectangle myRectangle = new Rectangle(w, h); System.out.println(myRectangle.getArea()); System.out.println(myRectangle.getPerimeter()); input.close(); } }
輸入樣例:
3.14 2.78
輸出樣例:
8.7292
11.84
class Rectangle{
double width;
double height;
Rectangle()
{
width=-1;
height=-1;
}
Rectangle(double w , double h){
width=w;
height=h;
}
double getArea()
{
return width*height;
}
double getPerimeter()
{
return (width+height)*2;
}
}