
/**
 * A class implementing the Measurable interface. This class represents a
 * rectangle.
 *
 * @see Measurable
 *
 * @author Mark Young (A00000000)
 */
public class Rectangle implements Measurable {

    private double length, width;

    /**
     * Create a rectangle of the given dimensions.
     *
     * @param reqLen the length
     * @param reqWid the width
     */
    public Rectangle(double reqLen, double reqWid) {
        requirePositive("length", reqLen);
        requirePositive("width", reqWid);
        length = reqLen;
        width = reqWid;
    }

    /**
     * Get this rectangle's length.
     *
     * @return this rectangle's length
     */
    public double getLength() {
        return length;
    }

    /**
     * Get this rectangle's width.
     *
     * @return this rectangle's width
     */
    public double getWidth() {
        return width;
    }

    @Override
    public double getArea() {
        return length * width;
    }

    @Override
    public double getPerimeter () {
        return 2 * (length + width);
    }

    /**
     * Get the length of this rectangle's diagonal.
     *
     * @return this rectangle's diagonal
     */
    public double getDiagonal() {
        return Math.hypot(length, width);
    }

    @Override
    public String toString() {
        return "Rectangle(" + length + "x" + width + ")";
    }

    // ---------- private methods ---------- //
    /**
     * Require that the given dimension be positive.
     *
     * @param dim the name of the dimension
     * @param value the value requested for that dimension
     * @throws IllegalArgumentException if {@code value} is not positive
     */
    private static void requirePositive(String dim, double value) {
        if (value <= 0.0) {
            throw new IllegalArgumentException(dim + ": " + value);
        }
    }

}
