public class Rectangle implements Measurable
1: /**
2: * A class of rectangles.
3: */
4: public class Rectangle implements Measurable {
6: /** the width of this rectangle */
7: private double myWidth;
8: /** the height of this rectangle */
9: private double myHeight;
11: /**
12: * Create a rectangle with the given dimensions.
13: *
14: * @param width the width of the new rectangle
15: * @param height the height of the new rectangle
16: */
17: public Rectangle (double width, double height) {
18: requirePositive("Width", width);
19: requirePositive("Height", height);
20: myWidth = width;
21: myHeight = height;
22: }
24: /**
25: * Get this rectangle's height.
26: *
27: * @return the height of this rectangle
28: */
29: public double getHeight() {
30: return myHeight;
31: }
33: /**
34: * Get this rectangle's width.
35: *
36: * @return the width of this rectangle
37: */
38: public double getWidth() {
39: return myWidth;
40: }
42: /**
43: * Get this rectangle's perimeter.
44: *
45: * @return the perimeter of this rectangle
46: */
47: @Override
48: public double getPerimeter() {
49: return 2 * (myWidth + myHeight);
50: }
52: /**
53: * Get this rectangle's area.
54: *
55: * @return the area of this rectangle
56: */
57: @Override
58: public double getArea () {
59: return myWidth * myHeight;
60: }
62: /**
63: * Create a String representation of this Rectangle.
64: *
65: * @return a String showing this Rectangle's dimensions
66: */
67: @Override
68: public String toString() {
69: return "Rectangle (" + myWidth + "x" + myHeight + ")";
70: }
72: /**
73: * Throw an exception if the given value is not positive.
74: *
75: * @param label the label to include in the exception
76: * @param value the value to check
77: * @throws IllegalArgumentException if value ≤ 0.0.
78: */
79: private static void requirePositive(String label, double value) {
80: if (value <= 0.0) {
81: throw new IllegalArgumentException(label + ": " + value);
82: }
83: }
85: }