13.7(TheColorableinterface) Design an interface named Colorable with a voidmethod named howToColor(). Every class of a colorable object must imple-ment 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-14 09:15:08 浏览: 84
How to decide whether a planar graph is regularly 3-colorable
Sorry, I'm an AI language model and I cannot draw UML diagrams. However, I can provide you with the code implementation of the problem you mentioned. Here's the solution:
```
// Colorable interface
public interface Colorable {
void howToColor();
}
// GeometricObject class
public abstract class GeometricObject {
// abstract methods
public abstract double getArea();
}
// Square class
public class Square extends GeometricObject implements Colorable {
private double side;
// constructor
public Square(double side) {
this.side = side;
}
// implementation of abstract method
public double getArea() {
return side * side;
}
// implementation of Colorable interface method
public void howToColor() {
System.out.println("Color all four sides.");
}
}
// Test program
public class TestProgram {
public static void main(String[] args) {
GeometricObject[] objects = new GeometricObject[5];
objects[0] = new Square(5);
objects[1] = new Square(7);
objects[2] = new Square(3);
objects[3] = new GeometricObject() {
public double getArea() {
return 10;
}
};
objects[4] = new GeometricObject() {
public double getArea() {
return 15;
}
};
for (int i = 0; i < objects.length; i++) {
System.out.println("Area of object " + (i+1) + ": " + objects[i].getArea());
if (objects[i] instanceof Colorable) {
((Colorable)objects[i]).howToColor();
}
}
}
}
```
Output:
```
Area of object 1: 25.0
Color all four sides.
Area of object 2: 49.0
Color all four sides.
Area of object 3: 9.0
Color all four sides.
Area of object 4: 10.0
Area of object 5: 15.0
```
阅读全文