
/**
 * A class to represent the dimensions of a Rectangle.
 * Now with a toString method.
 *
 * @author Mark Young (A00000000)
 */
public class Rectangle {

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

    private double height;
    private double width;


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

    /**
     * Create a Rectangle with the given dimensions.
     * Negative dimensions are replaced with zeroes,
     * and a warning message is printed.
     *
     * @param reqHgt  the requested height
     * @param reqWid  the requested width
     */
    public Rectangle(double reqHgt, double reqWid) {
        requirePositive("height", reqHgt);
        requirePositive("width", reqWid);

        // know now that the requested dimensions are positive
        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.
     * Request to change to a negative height will be rejected
     * with a warning message.
     *
     * @param reqHgt  the requested new height for this Rectangle
     */
    public void setHeight(double reqHgt) {
        requirePositive("height", reqHgt);
        height = reqHgt;
    }

    /**
     * Change the width of this Rectangle.
     * Request to change to a negative width will be rejected
     * with a warning message.
     *
     * @param reqWid  the requested new width for this Rectangle
     */
    public void setWidth(double reqWid) {
        requirePositive("width", reqWid);
        width = reqWid;
    }

    /**
     * Return a String representing this Rectangle.  
     *
     * NOTE TO STUDENTS: toString does not print anything.  That's not its job.
     *
     * NOTE TO STUDENTS: don't worry about the @Override.  We'll explain it in
     * CSCI 1228.
     *
     * @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";
    }

    // ----- Private methods ------------------------------------------ //

    /**
     * Throw an exception if the given value is not greater than zero. Include
     * a description of the value in the exception's message.
     *
     * @param descr a name or description to include in the exception message
     * @param value the value to test
     * @throws IllegalArgumentException if {@code value} is not positive
     */
    private static void requirePositive(String descr, double value) {
        if (value <= 0.0) {
            throw new IllegalArgumentException(descr + ": " + value);
        }
    }

}
