Source of Rectangle.java


  2: /**
  3:  * A class to represent the dimensions of a Rectangle.
  4:  * Now with a toString method.
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  */
  8: public class Rectangle {

 10:     // ----- Instance variables / Fields ------------------------------- //

 12:     private double height;
 13:     private double width;


 16:     // ----- Constructor ----------------------------------------------- //

 18:     /**
 19:      * Create a Rectangle with the given dimensions.
 20:      * Negative dimensions are replaced with zeroes,
 21:      * and a warning message is printed.
 22:      *
 23:      * @param reqHgt  the requested height
 24:      * @param reqWid  the requested width
 25:      */
 26:     public Rectangle(double reqHgt, double reqWid) {
 27:         requirePositive("height", reqHgt);
 28:         requirePositive("width", reqWid);

 30:         // know now that the requested dimensions are positive
 31:         height = reqHgt;
 32:         width = reqWid;
 33:     }


 36:     // ----- Getters --------------------------------------------------- //

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

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

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


 66:     // ----- Setters --------------------------------------------------- //

 68:     /**
 69:      * Change the height of this Rectangle.
 70:      * Request to change to a negative height will be rejected
 71:      * with a warning message.
 72:      *
 73:      * @param reqHgt  the requested new height for this Rectangle
 74:      */
 75:     public void setHeight(double reqHgt) {
 76:         requirePositive("height", reqHgt);
 77:         height = reqHgt;
 78:     }

 80:     /**
 81:      * Change the width of this Rectangle.
 82:      * Request to change to a negative width will be rejected
 83:      * with a warning message.
 84:      *
 85:      * @param reqWid  the requested new width for this Rectangle
 86:      */
 87:     public void setWidth(double reqWid) {
 88:         requirePositive("width", reqWid);
 89:         width = reqWid;
 90:     }

 92:     /**
 93:      * Return a String representing this Rectangle.  
 94:      *
 95:      * NOTE TO STUDENTS: toString does not print anything.  That's not its job.
 96:      *
 97:      * NOTE TO STUDENTS: don't worry about the @Override.  We'll explain it in
 98:      * CSCI 1228.
 99:      *
100:      * @return a String in the form HxW Rectangle, where H and W are this 
101:      *         Rectangle's height and width.
102:      */
103:     @Override
104:     public String toString() {
105:         return height + "x" + width + " Rectangle";
106:     }

108:     // ----- Private methods ------------------------------------------ //

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

124: }