- *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.
UML图
GeometricObject-类
package homework06;
import java.util.Date;
/**
* 几何图形实体类
* @author whd
*
*/
public class GeometricObject{
private String color="white";
private boolean filled;
private Date dateCreated;
//无参构造
public GeometricObject() {
//super();
dateCreated=new Date();
}
//有参构造
public GeometricObject(String color, boolean filled) {
//super();
this.color= color;
this.filled = filled;
dateCreated=new Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
@Override
public String toString() {
return "create on " + dateCreated+"\ncolor: "+color+" and filled:"+filled;
}
}
Colorable interface -接口
package homework06;
public interface Colorable {
public abstract void howToColor();
}
Square正方形实体类继承GeometricObject类 实现Colorable 接口
package homework06;
public class Square extends GeometricObject implements Colorable{
private double side;//边长
private double area;//面积
public Square(double side,boolean filled,String color) {
super(color,filled);
this.side = side;
this.area=side*side;
}
//计算面积
public double getArea() {
return area;
}
@Override
public void howToColor() {
System.out.println("Color all four sides.");
}
@Override
public String toString() {
return "Square [side=" + side + ", area=" + area +", color=" + super.getColor() + "]";
}
}
测试类
package homework06;
public class Test {
public static void main(String[] args) {
GeometricObject[] geos = {
new Square(1, true,"green"),
new Square(2, true,"yellow"),
new Square(3, false,"white"), //默认白色
new Square(4, true,"blue"),
new Square(5, false,"white")
};
for (int i = 0; i < geos.length; i++) {
System.out.println("第"+(i+1)+"个正方形信息:\n"+geos[i]);
if(geos[i].isFilled()) {
((Square) geos[i]).howToColor();
System.out.println("--------------------");
}
}
}
}
运行截图