-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.java
79 lines (51 loc) · 1.35 KB
/
Shape.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public abstract class Shape implements Comparable {
private Point center;
private String color;
public Shape() {
center = new Point(0,0);
color = "White";
}
public Shape(Point p, String s) throws BadColorException {
if (s.equalsIgnoreCase("White") || s.equalsIgnoreCase("Blue") || s.equalsIgnoreCase("Green") || s.equalsIgnoreCase("Yellow"))
color = s;
else
throw new BadColorException(s + " is not a valid color.");
center = new Point(p);
color = s;
}
public Shape(Shape s) {
center = new Point(s.center);
color = s.color;
}
public void move(double x, double y) {
erase();
center.x += x;
center.y += y;
draw();
}
protected Point getCenter() {
return new Point(center);
}
public abstract Shape clone();
public abstract void erase();
public abstract void draw();
public abstract double getArea();
public int compareTo(Object o) {
Shape s = (Shape)o;
if (this.getArea() > s.getArea()) return 1;
if (this.getArea() < s.getArea()) return -1;
return 0;
}
public boolean equals(Object o) {
if (o == null) return false;
if(!(o instanceof Shape)) return false;
Shape s = (Shape)o;
return center.equals(s.center) && color.equals(s.color);
}
}
class BadColorException extends Exception {
public BadColorException() {};
public BadColorException(String message) {
super(message);
}
}