Source of Rectangle.java


  1: /**
  2:  * A class to represent the dimensions of a Rectangle. This version throws
  3:  * an exception if a requested value is illegal.
  4:  *
  5:  * @author Mark Young (A00000000)
  6:  */
  7: public class Rectangle {

  9:     // ----- Instance variables / Fields ------------------------------- //

 11:     private double height;
 12:     private double width;


 15:     // ----- Constructor ----------------------------------------------- //

 17:     /**
 18:      * Create a Rectangle with the given dimensions. Dimensions must be
 19:      * strictly positive, or an exception is thrown.
 20:      *
 21:      * @param reqHgt the requested height
 22:      * @param reqWid the requested width
 23:      * @throws IllegalArgumentException if either dimension ≤ 0.0
 24:      */
 25:     public Rectangle(double reqHgt, double reqWid) {
 26:         requirePositive("height", reqHgt);
 27:         requirePositive("width", reqWid);
 28:         height = reqHgt;
 29:         width = reqWid;
 30:     }


 33:     // ----- Getters --------------------------------------------------- //

 35:     /**
 36:      * Get the height of this Rectangle.
 37:      *
 38:      * @return the height of this Rectangle
 39:      */
 40:     public double getHeight() {
 41:         return height;
 42:     }

 44:     /**
 45:      * Get the width of this Rectangle.
 46:      *
 47:      * @return the width of this Rectangle
 48:      */
 49:     public double getWidth() {
 50:         return width;
 51:     }

 53:     /**
 54:      * Get the area of this Rectangle
 55:      *
 56:      * @return the area of this Rectangle
 57:      */
 58:     public double getArea() {
 59:         return height * width;
 60:     }


 63:     // ----- Setters --------------------------------------------------- //

 65:     /**
 66:      * Change the height of this Rectangle. The new height must be strictly
 67:      * positive, or an exception is thrown.
 68:      *
 69:      * @param reqHgt the requested new height for this Rectangle
 70:      * @throws IllegalArgumentException if reqHgt ≤ 0.0
 71:      */
 72:     public void setHeight(double reqHgt) {
 73:         requirePositive("height", reqHgt);
 74:         height = reqHgt;
 75:     }

 77:     /**
 78:      * Change the width of this Rectangle. The new width must be strictly
 79:      * positive, or an exception is thrown.
 80:      *
 81:      * @param reqWid the requested new width for this Rectangle
 82:      * @throws IllegalArgumentException if reqWid ≤ 0.0
 83:      */
 84:     public void setWidth(double reqWid) {
 85:         requirePositive("width", reqWid);
 86:         width = reqWid;
 87:     }


 90:     // ----- Other observers ------------------------------------------- //

 92:     /**
 93:      * Return a String representing this Rectangle.  
 94:      * @return a String in the form HxW Rectangle, where H and W are this 
 95:      *         Rectangle's height and width.
 96:      */
 97:     @Override
 98:     public String toString() {
 99:         return height + "x" + width + " Rectangle";
100:     }


103:     // ----- Helper methods -------------------------------------------- //

105:     /**
106:      * Require the given dimension to be greater than zero.
107:      *
108:      * @param dim the name of the dimension
109:      * @param reqVal the requested value for that dimension
110:      * @throws IllegalArgumentException if reqVal ≤ 0
111:      */
112:     private static void requirePositive(String dim, double reqVal) {
113:         if (reqVal <= 0.0) {
114:             throw new IllegalArgumentException("Illegal " + dim + ": " 
115:                     + reqVal);
116:         }
117:     }

119: }