第十三章第七题(Colorable类)(Colorable class)
- *13.7(Colorable类)创建名为Colorable的接口,其中有名为howToColor的void方法。可着色对象的每个类必须实现Colorable接口。设计一个名为Square的类,继承自GeometriObject类并实现Colorable接口。实现howToColor方法,显示一个消息Color all four sides(给所有的四条边着色)。Squaer类具有一个私有的命名为side的double数据域及其设置方法和获取方法。他具有一个无参的构造方法来构建边为0的Square,以及另一个使用指定边来构建Square的构造方法。
画出包含Colorable、Square和GeometriObject的UML图。编写一个测试程序,创建有五个GeometriObject对象的数组。对于数组中的每个对象而言,如果对象是可着色的,则调用其howToColor方法。
*13.7(Colorable class)Create an interface called colorable with a void method named howtocolor. Each class of a colorable object must implement the colorable interface. Design a class named square, which inherits from the geometriobject class and implements the colorable interface. Implement the howtocolor method and display a message color all four sides. The square class has a private double data field named side and its setting and obtaining methods. He has a construction method without parameters to build square with edge 0, and another method to construct square with specified edge.
Draw a UML diagram containing colorable, square and geometriobject. Write a test program to create an array of five geometriobject objects. For each object in the array, if the object is colorable, its howtocolor method is called.
- 参考代码:
package chapter13;
import java.util.Scanner;
public class Code_07 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
GeometricObject1[] squares = new Square[5];
for (int i = 0; i < 5; i++) {
System.out.println(i + " : Square ");
System.out.print("\tEnter width: ");
double width = input.nextDouble();
System.out.print("\tEnter height: ");
double height = input