参照类图创建一个Rectangle类,包含以下属性和方法: 2) 属性:两个double成员变量,width和height; 3) 不带参数的构造方法:width和height的初始值分别是6和8:带参的构造方法 4) 方法:计算并输出矩形的周长方法名为fndPremeter 0: 5) 编写测试类,应用不同的构造方法创建至少三个Rectangle的对象,分别调用周 长和面积的方法。 类图如下:
时间: 2023-05-28 16:04:35 浏览: 225
```
+----------------+
| Rectangle |
+----------------+
| - width: double |
| - height: double|
+----------------+
| + Rectangle() |
| + Rectangle(w: double, h: double)|
| + findPerimeter(): void|
+----------------+
```
Rectangle.java
```
public class Rectangle {
private double width;
private double height;
public Rectangle() {
this.width = 6;
this.height = 8;
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public void findPerimeter() {
double perimeter = 2 * (width + height);
System.out.println("The perimeter of the rectangle is " + perimeter);
}
}
```
RectangleTest.java
```
public class RectangleTest {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
rectangle1.findPerimeter(); // Output: The perimeter of the rectangle is 28.0
Rectangle rectangle2 = new Rectangle(10, 20);
rectangle2.findPerimeter(); // Output: The perimeter of the rectangle is 60.0
Rectangle rectangle3 = new Rectangle(5.5, 7.5);
rectangle3.findPerimeter(); // Output: The perimeter of the rectangle is 26.0
}
}
```
阅读全文