public class Circle
1: /**
2: * A class used to demonstrate the equals method.
3: *
4: * @author Mark Young (A00000000)
5: */
6: public class Circle {
8: private double radius;
10: /**
11: * Create a circle with the given radius.
12: */
13: public Circle(double r) {
14: assertPositive(r);
15: radius = r;
16: }
18: /**
19: * Return this Circle's radius.
20: *
21: * @return this Circle's radius.
22: */
23: public double getRadius() {
24: return radius;
25: }
27: /**
28: * Return this Circle's diameter.
29: *
30: * @return this Circle's diameter.
31: */
32: public double getDiameter() {
33: return 2 * radius;
34: }
36: /**
37: * Return this Circle's area.
38: *
39: * @return this Circle's area.
40: */
41: public double getArea() {
42: return Math.PI * Math.pow(radius, 2);
43: }
45: /**
46: * Return this Circle's circumference.
47: *
48: * @return this Circle's circumference.
49: */
50: public double getCircumference() {
51: return 2 * Math.PI * radius;
52: }
54: /**
55: * Return this Circle's dimensions in a String.
56: *
57: * @return a String including this Circle's radius.
58: */
59: @Override
60: public String toString() {
61: return String.format("(Circle of radius %.2f)", radius);
62: }
64: /**
65: * Whether this Circle is essentially the same as other.
66: *
67: * @return true if other is a Circle with the same radius as this Circle.
68: */
69: @Override
70: public boolean equals(Object other) {
71: // if it IS a circle
72: if (other instanceof Circle) {
73: Circle that = (Circle)other;
74: return this.radius == that.radius;
75: }
76: // OTHERWISE
77: return false;
78: }
80: /**
81: * Require the argument to be positive (greater than zero). Throw an
82: * IllegalArgumentException otherwise. Used in methods that set/change this
83: * Circle's radius.
84: *
85: * @param reqRadius the "radius" to check for validity
86: * @throws IllegalArgumentException if the argument is not positive.
87: */
88: private static void assertPositive(double reqRadius) {
89: if (reqRadius <= 0) {
90: throw new IllegalArgumentException("radius: " + reqRadius);
91: }
92: }
94: }