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

  • Post author:
  • Post category:其他




第十三章第七题(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.nextDouble();
            System.out.print("\tEnter Color: ");
            String color = input.next();
            System.out.print("\tIs Filled: ");
            boolean filled = input.nextBoolean();
            squares[i] = new Square(width, height, color, filled);
        }

        for (int i = 0; i < 5; i ++) {
            System.out.println(squares[i].toString());
            System.out.println();
        }
    }
}
interface Colorable {
    public abstract void howToColor();
}
abstract class GeometricObject1 {
    private String color;
    private boolean filled;
    private java.util.Date dateCreated;

    protected GeometricObject1() {
        dateCreated = new java.util.Date();
    }

    protected GeometricObject1(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
        dateCreated = new java.util.Date();
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFuilled(boolean filled) {
        this.filled = filled;
    }

    @Override
    public String toString() {
        return "created on " + dateCreated + "\ncolor: " + color + "\nFilled " + filled;
    }

    public abstract double getArea();

    public abstract double getPerimeter();
}
class Square extends GeometricObject1 implements Colorable {
    private double width;
    private double height;

    public Square () {
    }

    public Square (double width, double height) {
        this(width, height, "white", false);
    }

    public Square(double width, double height, String color, boolean filled) {
        super(color, filled);
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }

    @Override
    public double getPerimeter() {
        return 2 * (width + height);
    }

    @Override
    public void howToColor() {
        System.out.println("Color all four sides");
    }

    @Override
    public String toString() {
        if (super.isFilled()) {
            howToColor();
        }
        return super.toString();
    }
}
  • 结果显示:
0 : Square 
	Enter width: 3
	Enter height: 4
	Enter Color: red
	Is Filled: true
1 : Square 
	Enter width: 2
	Enter height: 3
	Enter Color: white
	Is Filled: true
2 : Square 
	Enter width: 4
	Enter height: 5
	Enter Color: black
	Is Filled: false
3 : Square 
	Enter width: 4
	Enter height: 5
	Enter Color: pink
	Is Filled: false
4 : Square 
	Enter width: 5
	Enter height: 6
	Enter Color: green
	Is Filled: false
Color all four sides
created on Sat Nov 07 16:29:58 CST 2020
color: red
Filled true

Color all four sides
created on Sat Nov 07 16:30:07 CST 2020
color: white
Filled true

created on Sat Nov 07 16:30:16 CST 2020
color: black
Filled false

created on Sat Nov 07 16:30:25 CST 2020
color: pink
Filled false

created on Sat Nov 07 16:30:36 CST 2020
color: green
Filled false


Process finished with exit code 0

在这里插入图片描述



版权声明:本文为jxh1025_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。