forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircle.java
More file actions
38 lines (33 loc) · 912 Bytes
/
Copy pathCircle.java
File metadata and controls
38 lines (33 loc) · 912 Bytes
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
// David Anderson
public class Circle extends Shape{
double centerX, centerY;
double radius;
public Circle(double centerX, double centerY, double radius){
this.centerX = centerX;
this.centerY = centerY;
this.radius = radius;
}
public String toString() {
return "("+centerX+", "+centerY+") radius "+radius;
}
public boolean equals(Object object) {
if (object instanceof Circle) {
Circle c = (Circle)object;
return centerX == c.centerX && centerY == c.centerY && radius == c.radius;
}
return false;
}
public boolean intersects(Circle c) {
double dx = centerX - c.centerX;
double dy = centerY - c.centerY;
double d = Math.sqrt(dx*dx + dy*dy);
return d <= radius + c.radius;
}
public double area() {
double a = Math.PI * radius * radius;
return a;
}
public double perimeter() {
return 2 * Math.PI * radius;
}
}