*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 Colorable. Implement howToColor to display the message Color all four sides. Draw a UML diagram that involves Colorable, Square, and GeometricObject. Write a test program that creates an array of five GeometricObjects. For each object in the array, display its area and invoke its howToColor method if it is colorable.
时间: 2024-02-24 12:58:32 浏览: 75
下面是Colorable接口的代码:
```java
public interface Colorable {
void howToColor();
}
```
下面是Square类的代码:
```java
public class Square extends GeometricObject implements Colorable {
private double side;
public Square(double side) {
this.side = side;
}
public double getSide() {
return side;
}
@Override
public double getArea() {
return side * side;
}
@Override
public void howToColor() {
System.out.println("Color all four sides.");
}
}
```
下面是GeometricObject类的代码:
```java
public abstract class GeometricObject {
public abstract double getArea();
}
```
下面是测试程序的代码:
```java
public class TestColorable {
public static void main(String[] args) {
GeometricObject[] geometricObjects = new GeometricObject[5];
geometricObjects[0] = new Square(2);
geometricObjects[1] = new Square(3);
geometricObjects[2] = new Circle(2);
geometricObjects[3] = new Circle(3);
geometricObjects[4] = new Rectangle(2, 3);
for (GeometricObject geometricObject : geometricObjects) {
System.out.println("Area: " + geometricObject.getArea());
if (geometricObject instanceof Colorable) {
((Colorable) geometricObject).howToColor();
}
}
}
}
```
下面是UML图:
```
+--------------+
| Colorable |
+--------------+
| +howToColor()|
+--------------+
^
|
+--------------+
| Square |
+--------------+
| -side: double|
+--------------+
^
|
+---------------+
| GeometricObject|
+---------------+
| +getArea() |
+---------------+
^
|
+---------------+
| Circle |
+---------------+
| -radius: double|
+---------------+
^
|
+---------------+
| Rectangle |
+---------------+
| -width: double |
| -height: double|
+---------------+
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)