Source of Rectangle.java


  2: /**
  3:  * A class to represent the dimensions of a Rectangle.
  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.
 19:      * Negative dimensions are replaced with zeroes,
 20:      * and a warning message is printed.
 21:      *
 22:      * @param reqHgt  the requested height
 23:      * @param reqWid  the requested width
 24:      */
 25:     public Rectangle(double reqHgt, double reqWid) {
 26:         if (reqHgt >= 0) {
 27:             height = reqHgt;
 28:         } else {
 29:             height = 0;
 30:             System.out.println("Illegal height for Rectangle: " + reqHgt);
 31:         }
 32:         if (reqWid >= 0) {
 33:             width = reqWid;
 34:         } else {
 35:             width = 0;
 36:             System.out.println("Illegal width for Rectangle: " + reqWid);
 37:         }
 38:     }


 41:     // ----- Getters --------------------------------------------------- //

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

 52:     /**
 53:      * Get the width of this Rectangle.
 54:      *
 55:      * @return the width of this Rectangle
 56:      */
 57:     public double getWidth() {
 58:         return width;
 59:     }

 61:     /**
 62:      * Get the area of this Rectangle
 63:      *
 64:      * @return the area of this Rectangle
 65:      */
 66:     public double getArea() {
 67:         return height * width;
 68:     }


 71:     // ----- Setters --------------------------------------------------- //

 73:     /**
 74:      * Change the height of this Rectangle.
 75:      * Request to change to a negative height will be rejected
 76:      * with a warning message.
 77:      *
 78:      * @param reqHgt  the requested new height for this Rectangle
 79:      */
 80:     public void setHeight(double reqHgt) {
 81:         if (reqHgt >= 0.0) {
 82:             height = reqHgt;
 83:         } else {
 84:             System.out.println("Illegal height for a Rectangle: " + reqHgt);
 85:         }
 86:     }

 88:     /**
 89:      * Change the width of this Rectangle.
 90:      * Request to change to a negative width will be rejected
 91:      * with a warning message.
 92:      *
 93:      * @param reqWid  the requested new width for this Rectangle
 94:      */
 95:     public void setWidth(double reqWid) {
 96:         if (reqWid >= 0.0) {
 97:             width = reqWid;
 98:         } else {
 99:             System.out.println("Illegal width for a Rectangle: " + reqWid);
100:         }
101:     }

103: }