第十三章第七题(Colorable类)(Colorable class)

第十三章第七题(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值