/**
 * A class to represent the dimensions of a Rectangle. This version throws
 * an exception if a requested value is illegal.
 *
 * @author Mark Young (A00000000)
 */
public class Rectangle {

    // ----- Instance variables / Fields ------------------------------- //

    private double height;
    private double width;


    // ----- Constructor ----------------------------------------------- //

    /**
     * Create a Rectangle with the given dimensions. Dimensions must be
     * strictly positive, or an exception is thrown.
     *
     * @param reqHgt the requested height
     * @param reqWid the requested width
     * @throws IllegalArgumentException if either dimension &le; 0.0
     */
    public Rectangle(double reqHgt, double reqWid) {
        requirePositive("height", reqHgt);
        requirePositive("width", reqWid);
        height = reqHgt;
        width = reqWid;
    }


    // ----- Getters --------------------------------------------------- //

    /**
     * Get the height of this Rectangle.
     *
     * @return the height of this Rectangle
     */
    public double getHeight() {
        return height;
    }

    /**
     * Get the width of this Rectangle.
     *
     * @return the width of this Rectangle
     */
    public double getWidth() {
        return width;
    }

    /**
     * Get the area of this Rectangle
     *
     * @return the area of this Rectangle
     */
    public double getArea() {
        return height * width;
    }


    // ----- Setters --------------------------------------------------- //

    /**
     * Change the height of this Rectangle. The new height must be strictly
     * positive, or an exception is thrown.
     *
     * @param reqHgt the requested new height for this Rectangle
     * @throws IllegalArgumentException if reqHgt &le; 0.0
     */
    public void setHeight(double reqHgt) {
        requirePositive("height", reqHgt);
        height = reqHgt;
    }

    /**
     * Change the width of this Rectangle. The new width must be strictly
     * positive, or an exception is thrown.
     *
     * @param reqWid the requested new width for this Rectangle
     * @throws IllegalArgumentException if reqWid &le; 0.0
     */
    public void setWidth(double reqWid) {
        requirePositive("width", reqWid);
        width = reqWid;
    }


    // ----- Other observers ------------------------------------------- //

    /**
     * Return a String representing this Rectangle.  
     * @return a String in the form HxW Rectangle, where H and W are this 
     *         Rectangle's height and width.
     */
    @Override
    public String toString() {
        return height + "x" + width + " Rectangle";
    }


    // ----- Helper methods -------------------------------------------- //

    /**
     * Require the given dimension to be greater than zero.
     *
     * @param dim the name of the dimension
     * @param reqVal the requested value for that dimension
     * @throws IllegalArgumentException if reqVal &le; 0
     */
    private static void requirePositive(String dim, double reqVal) {
        if (reqVal <= 0.0) {
            throw new IllegalArgumentException("Illegal " + dim + ": " 
                    + reqVal);
        }
    }

}
