*13.7(Colorable class)Create an interface called colorable with a void method named howtocolor. Each

  • *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("--------------------");
			}
		}
	}

}

运行截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WaitIKnowYou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值