设计一个名为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();
}
}
import java.util.Scanner;
class Rectangle{
private double width = 1.0; //矩形的宽,默认值为1.0
private double height = 1.0; //矩形的长,默认值为1.0
public Rectangle(double width, double height) {
this.width = width;
this.height = height; //创建width和height为指定值的矩形
}
public double getArea() {
return this.height * this.width; //返回矩形的面积
}
public double getPerimeter() {
return 2 * (this.height + this.width); //返回矩形的周长
}
}