public class Rectangle implements Measurable
2: /**
3: * A class implementing the Measurable interface. This class represents a
4: * rectangle.
5: *
6: * @see Measurable
7: *
8: * @author Mark Young (A00000000)
9: */
10: public class Rectangle implements Measurable {
12: private double length, width;
14: /**
15: * Create a rectangle of the given dimensions.
16: *
17: * @param reqLen the length
18: * @param reqWid the width
19: */
20: public Rectangle(double reqLen, double reqWid) {
21: requirePositive("length", reqLen);
22: requirePositive("width", reqWid);
23: length = reqLen;
24: width = reqWid;
25: }
27: /**
28: * Get this rectangle's length.
29: *
30: * @return this rectangle's length
31: */
32: public double getLength() {
33: return length;
34: }
36: /**
37: * Get this rectangle's width.
38: *
39: * @return this rectangle's width
40: */
41: public double getWidth() {
42: return width;
43: }
45: @Override
46: public double getArea() {
47: return length * width;
48: }
50: @Override
51: public double getPerimeter () {
52: return 2 * (length + width);
53: }
55: /**
56: * Get the length of this rectangle's diagonal.
57: *
58: * @return this rectangle's diagonal
59: */
60: public double getDiagonal() {
61: return Math.hypot(length, width);
62: }
64: @Override
65: public String toString() {
66: return "Rectangle(" + length + "x" + width + ")";
67: }
69: // ---------- private methods ---------- //
70: /**
71: * Require that the given dimension be positive.
72: *
73: * @param dim the name of the dimension
74: * @param value the value requested for that dimension
75: * @throws IllegalArgumentException if {@code value} is not positive
76: */
77: private static void requirePositive(String dim, double value) {
78: if (value <= 0.0) {
79: throw new IllegalArgumentException(dim + ": " + value);
80: }
81: }
83: }