-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exam2.java
103 lines (68 loc) · 1.54 KB
/
Exam2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
abstract class Shape {
private Point center;
public Shape() {
center = new Point(0,0);
}
public Shape(Point p) {
center = new Point(p);
}
public Shape(Shape s) {
center = new Point(s.center);
}
public void move(double x, double y) {
center.setPoint(center.getX()+x, center.getY()+y);
}
public Point getCenter() {
return new Point(center);
}
public boolean equals(Object o) {
if (o == null) return false; // null check is needed before getClass call
if (this.getClass() != o.getClass()) return false;
Shape s = (Shape)o;
return center.equals(s.center);
}
public abstract double getArea();
public abstract double getPerimeter();
}
class Circle extends Shape {
private double radius;
public Circle() {
super(); // optional
radius = 1;
}
public Circle(Point p, double r) {
super(p);
radius = r;
}
public Circle(Circle c) {
super(c);
radius = c.radius;
}
public void setRadius(double r) {
radius = r;
}
public double getArea() {
return Math.pi*radius*radius;
}
public double getPerimeter() {
return Math.pi*radius*2;
}
public boolean equals(Object o) {
if (super.equals(o)) {
if (this.getClass() == o.getClass() && radius == ((Circle)o).radius)
return true;
else
return false;
}
else
return false;
}
}
public static double totalCircleArea(Shape[] s) {
double total = 0;
for (int i = 0; i < s.length; i++) {
if(s[i] != null && s[i] instanceof Circle) // != null is redundant with instance of
total += s[i].getArea();
}
return total;
}