Source of Rectangle.java


  1: package exceptions;

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

 11:     // ----- Instance variables / Fields ------------------------------- //

 13:     private double height;
 14:     private double width;


 17:     // ----- Constructor ----------------------------------------------- //

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


 35:     // ----- Getters --------------------------------------------------- //

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

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

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


 65:     // ----- Setters --------------------------------------------------- //

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

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


 92:     // ----- Other observers ------------------------------------------- //

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


105:     // ----- Helper methods -------------------------------------------- //

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

121: }