13.7 (The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements ColorableImplement howToColor to display the message Color al1 four sides. Draw a UML diagram that involves Colorable, Square, and GeometricObject Write a test program that creates an array of five Geometricbiects. For each object in the array, display its area and invoke its howToColor method if it is colorable. 13.7(Colorable接口)设计一个名为Colorable的接口,并使用一个名为howToColor()的 void方法。可着色对象的每个类都必须实现Colorable接口。设计一个名为Square的类,扩展几何对象并实现Colorable。实现howToColor以在四面显示消息Color。 绘制一个包含Colorable、正方形和几何对象的UML图。编写一个测试程序,创建一个由五个geometricobject组成的数组。对于数组中的每个对象,显示其区域并调用其howToColor方法(如果它是可着色的)。
时间: 2023-11-27 11:51:45 浏览: 56
以下是Colorable、Square和GeometricObject的UML图:
```
+----------------+ +----------------+ +----------------+
| Colorable | | GeometricObject | | Square |
+----------------+ +----------------+ +----------------+
| void howToColor()| <------ | double area | <------ | double side |
+----------------+ +----------------+ |void howToColor()|
+----------------+
```
以下是测试程序:
```java
public class TestColorable {
public static void main(String[] args) {
GeometricObject[] shapes = new GeometricObject[5];
shapes[0] = new Circle(5);
shapes[1] = new Rectangle(4, 5);
shapes[2] = new Square(3);
shapes[3] = new Triangle(3, 4, 5);
shapes[4] = new Square(6);
for (GeometricObject shape : shapes) {
System.out.println("Area: " + shape.getArea());
if (shape instanceof Colorable) {
((Colorable) shape).howToColor();
}
}
}
}
```
其中,Circle、Rectangle和Triangle是GeometricObject的子类,而Square既是GeometricObject的子类,也实现了Colorable接口。在循环中,我们遍历数组中的每个几何对象,输出其面积,并检查它是否实现了Colorable接口。如果实现了,则调用howToColor方法。
相关推荐
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![crx](https://img-home.csdnimg.cn/images/20210720083646.png)