这个color到底用来干啥的?java编写
2.编写程序模拟几何图形、圆、矩形和三角形,请分别定义Geometry、Circle、Rectangle和Trianlge 4个类,请画出类图,设计要求如下:(1)设计一几何图形类Geometry,该类有一个域color,一个有参的构造函数,属性的getter和setter方法,以及计算几何图形的周长和面积(周长和面积的返回值为0)。
(2)设计Geometry类的三个子类(Circle、Rectangle、Trianlge),Circle类有一个半径域radius,一个有参的构造函数;Rectangle类有长(length)和宽(width)两个域,一个有参构造函数;Trianlge类有三条边长域(a,b,c),一个有参构造函数;三个子类都分别重写父类Geometry计算周长和面积的方法。
(3)请编写测试类分别测试上述4个类的功能。
color是用来表示几何图形的颜色的属性。在这个问题中,color是Geometry类的一个域,用来表示几何图形的颜色。
以下是相应的类图:
+----------------+
| Geometry |
+----------------+
| - color: String|
+----------------+
| + Geometry(color: String)|
| + getColor(): String|
| + setColor(color: String)|
| + getPerimeter(): double|
| + getArea(): double|
+----------------+
+--------------+
| Circle |
+--------------+
| - radius: double|
+--------------+
| + Circle(color: String, radius: double)|
| + getRadius(): double|
| + setRadius(radius: double)|
| + getPerimeter(): double|
| + getArea(): double|
+--------------+
+----------------+
| Rectangle |
+----------------+
| - length: double|
| - width: double|
+----------------+
| + Rectangle(color: String, length: double, width: double)|
| + getLength(): double|
| + setLength(length: double)|
| + getWidth(): double|
| + setWidth(width: double)|
| + getPerimeter(): double|
| + getArea(): double|
+----------------+
+----------------+
| Triangle |
+----------------+
| - a: double|
| - b: double|
| - c: double|
+----------------+
| + Triangle(color: String, a: double, b: double, c: double)|
| + getA(): double|
| + setA(a: double)|
| + getB(): double|
| + setB(b: double)|
| + getC(): double|
| + setC(c: double)|
| + getPerimeter(): double|
| + getArea(): double|
+----------------+
在测试类中,你可以创建Geometry、Circle、Rectangle和Triangle的对象,并调用它们的方法来测试它们的功能。例如:
public class Test {
public static void main(String[] args) {
Geometry geometry = new Geometry("red");
System.out.println("Geometry color: " + geometry.getColor());
System.out.println("Geometry perimeter: " + geometry.getPerimeter());
System.out.println("Geometry area: " + geometry.getArea());
Circle circle = new Circle("blue", 5.0);
System.out.println("Circle color: " + circle.getColor());
System.out.println("Circle radius: " + circle.getRadius());
System.out.println("Circle perimeter: " + circle.getPerimeter());
System.out.println("Circle area: " + circle.getArea());
Rectangle rectangle = new Rectangle("green", 3.0, 4.0);
System.out.println("Rectangle color: " + rectangle.getColor());
System.out.println("Rectangle length: " + rectangle.getLength());
System.out.println("Rectangle width: " + rectangle.getWidth());
System.out.println("Rectangle perimeter: " + rectangle.getPerimeter());
System.out.println("Rectangle area: " + rectangle.getArea());
Triangle triangle = new Triangle("yellow", 3.0, 4.0, 5.0);
System.out.println("Triangle color: " + triangle.getColor());
System.out.println("Triangle a: " + triangle.getA());
System.out.println("Triangle b: " + triangle.getB());
System.out.println("Triangle c: " + triangle.getC());
System.out.println("Triangle perimeter: " + triangle.getPerimeter());
System.out.println("Triangle area: " + triangle.getArea());
}
}
这样你就可以测试这些类的功能了。 isdkz 发表于 2023-10-24 20:01
color是用来表示几何图形的颜色的属性。在这个问题中,color是Geometry类的一个域,用来表示几何图形的颜色 ...
Geometry、Circle、Rectangle和Triangle这几个类怎么编写呢?
页:
[1]