题目描述
现有一块画布上(Canvas)它有如下功能:
定义addShape(Shape s)在画布上新增并绘制出其形状;
定义removeShape(Shape s)删除画布上已存在的形状
定义clone(Shape s)克隆一个已存在的形状增加到画布上
定义Shape Max()方法返回画布上面积最大的形状(如果有相等的几个返回第一个)
定义public double getArea()方法求画布上所有形状的面积和
定义printAllShapes()方法打印画布上所有形状信息
每一个形状(Shape)类,都有创建时的日期数据域(createdDate),创建的颜色(color),求面积和绘制图形的方法等等,每一个形状都是能被比较和复制(复制的时候,形状的时间应该是被复制的时间而不是被复制的形状的创建时间)。长方形和圆形是形状类的子类,它们会重写父类求面积和绘制图形的方法。为了程序实现方便,我们这里只定义两种图形。圆形(circle)和长方形(rectangle)。
uml图如下:
简要分析
画布和形状是聚合的关系,画布类应该定义add,remove,print等方法。shape类应该为抽象类,他的子类circle和rectangle要实现Comparable和Cloneable接口,实现Comparable接口是为啦方便对象数组排序,需要重写ComparTo()方法;实现Cloneable接口是为啦方便clone对象,需要重写Clone()方法。
Canvas类
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
Canvans类:
package Canvans;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
public class canvas {
private ArrayList<Shape> shapes = new ArrayList<>();
private double Area;
public void addShape(Shape shape) {
shapes.add(shape);
shape.draw();
}
public void removeShape(Shape shape) {
shape.del();
shapes.remove(shape);
}
public double getArea() {
for (Shape shape : shapes) {
Area += shape.getArea();
}
return Area;
}
public Shape ShowMax() {
if(shapes == null)
{
return null;
}
// Collections.sort(shapes,new compare());
Collections.sort(shapes);
return shapes.get(0);
}
public void printAllShapes()
{
for(Shape shape:shapes)
{
System.out.println(shape.toString());
}
}
}
//注释部分代码是之前编写的指定比较器,可忽略
//class compare implements Comparator<Shape>{
//
// public int compare(Shape a,Shape b)
// {
// if(a.getArea()<b.getArea())
// {
// return -1;
// }
// if(a.getArea()>b.getArea())
// {
// return 1;
// }
// return 0;
// }
//}
Shape类
public abstract class Shape implements Comparable<Shape>,Cloneable{
private java.util.Date whenBuilt;
private String color;
public abstract double getArea();
public abstract void draw();
public abstract void del();
public abstract int compareTo(Shape shape);
}
Circle类
import java.util.Date;
import static java.lang.Math.PI;
public class Circle extends Shape implements Comparable<Shape>,Cloneable{
private double radius;
private String color;
private java.util.Date whenBuilt;
Circle(String color,double radius)
{
this.radius = radius;
this.color = color;
this.whenBuilt = new Date();
}
public double getRadius()
{
return radius;
}
public String getColor()
{
return color;
}
public java.util.Date getWhenBuilt()
{
return whenBuilt;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public void setColor( String color)
{
this.color = color;
}
public void setWhenBuilt(Date whenBuilt)
{
this.whenBuilt = whenBuilt;
}
@Override
public double getArea() {
return radius*radius*PI;
}
@Override
public void draw() {
System.out.println("正在画一个圆形...Circle [ radius = "+radius+"getCreatedDate() = "+whenBuilt+", getColor()= "+color);
}
public void del()
{
System.out.println("正在删除一个圆形...Circle [ radius = "+radius+"getCreatedDate() = "+whenBuilt+", getColor()= "+color);
}
@Override
public int compareTo(Shape shape) {
if(shape.getArea()>this.getArea())
{return 1;}
else if(shape.getArea()<this.getArea())
{return -1;}
else
{return 0;}
}
public Object clone() throws CloneNotSupportedException{
Circle circleclone = (Circle)super.clone();
circleclone.whenBuilt = new Date();
return circleclone;
}
public String toString()
{
return "Circle [ radius = "+radius+", getCreatedDate() = "+ whenBuilt+", getColor()= "+color+"\n";
}
}
Rectangle类
import java.util.Date;
public class Rectangle extends Shape implements Comparable<Shape>,Cloneable{
private java.util.Date whenBuilt;
private double width;
private double length;
private String color;
Rectangle(String color,double width,double length)
{
this.color = color;
this.length = length;
this.width = width;
this.whenBuilt = new Date();
}
public double getWidth()
{
return width;
}
public double getLength()
{
return length;
}
public String getColor()
{
return color;
}
public java.util.Date getWhenBuilt()
{
return whenBuilt;
}
public void setWidth(double width)
{
this.width = width;
}
public void setLength(double length)
{
this.length = length;
}
public void setColor( String color)
{
this.color = color;
}
public void setWhenBuilt(Date whenBuilt)
{
this.whenBuilt = whenBuilt;
}
@Override
public double getArea() {
return width*length;
}
@Override
public void draw() {
System.out.println("正在画一个方形...Rectangle [ width = "+width+", length = "+length+" getCreatedDate() = "+whenBuilt+", getColor()= "+color);
}
public void del() {
System.out.println("正在删除一个方形...Rectangle [ width = "+width+", length = "+length+" getCreatedDate() = "+whenBuilt+", getColor()= "+color);
}
@Override
public int compareTo(Shape shape) {
if(shape.getArea()>this.getArea())
{return 1;}
else if(shape.getArea()<this.getArea())
{return -1;}
else
{return 0;}
}
public Object clone() throws CloneNotSupportedException{
Rectangle rectangleclone = (Rectangle) super.clone();
rectangleclone.whenBuilt = new Date();
return rectangleclone;
}
public String toString()
{
return "Rectangle [ width = "+width+", length = "+length+" getCreatedDate() = "+whenBuilt+", getColor()= "+color+"\n";
}
}
##test类
package Canvans;
public class testmain {
public static void main(String[] args) throws CloneNotSupportedException{
//创建c1圆的对象
Circle c1 = new Circle("yellow",1.0);
//创建c2圆的对象
Circle c2 = new Circle("white",2.0);
//创建r1矩形的对象
Rectangle r1 = new Rectangle("pick", 3, 4);
//创建r2矩形的对象
Rectangle r2 = new Rectangle("black", 2, 5);
//创建c画布的对象
canvas c = new canvas();
//在画布上加入并绘制c1,c2,r1和r2
c.addShape(c1);
c.addShape(c2);
c.addShape(r1);
c.addShape(r2);
System.out.println();
//在画布上删除c1对象
c.removeShape(c1);
c.printAllShapes();
System.out.println();
//复制r1对象赋值给r3并加入在画布中
Rectangle r3 =(Rectangle)(r1.clone());
c.addShape(r3);
//修改r1对象的属性
r1.setColor("green");
r1.setLength(20);
r1.setWidth(10);
// 注意r3和r1输出结果的不同,长,宽和日期
c.printAllShapes();
System.out.println();
//求所有形状中面积最大的并打印其信息
Shape p = c.ShowMax();
System.out.println(p.toString());
}
}