001/** 002 * This class represents a Rectangle. 003 * 004 * <p>There are two new fields: 005 * <br><code>private double height</code> 006 * <br><code>private double width</code> 007 * 008 * @version 3.0, March 2003 009 * @author Bob Myers 010 */ 011public class Rectangle 012{ 013 /** 014 * define constant for shortest height 015 */ 016 public static final double SHORTEST_HEIGHT = 0.0; 017 018 /** 019 * define constant for shortest width 020 */ 021 public static final double SHORTEST_WIDTH = 0.0; 022 023 /** 024 * define constant for default height 025 */ 026 public static final double DEFAULT_HEIGHT = 0.0; 027 028 /** 029 * define constant for default width 030 */ 031 public static final double DEFAULT_WIDTH = 0.0; 032 033 /** 034 * height 035 */ 036 private double height; 037 038 /** 039 * width 040 */ 041 private double width; 042 043 /** 044 * Set this object's height value. 045 * @param height set this object height value to the parameter, 046 * with a minimum size of <code>SHORTEST_HEIGHT</code> 047 */ 048 private void privateSetHeight(double height) 049 { 050 this.height = (height < SHORTEST_HEIGHT) ? SHORTEST_HEIGHT : height; 051 } 052 053 /** 054 * Set this object's width value. 055 * @param width set this object width value to the parameter, 056 * with a minimum size of <code>SHORTEST_WIDTH</code> 057 */ 058 private void privateSetWidth(double width) 059 { 060 this.width = (width < SHORTEST_WIDTH) ? SHORTEST_WIDTH : width; 061 } 062 063 /** 064 * Complete constructor. 065 * @param height the height 066 * @param width the width 067 */ 068 public Rectangle(double height, double width) 069 { 070 privateSetHeight(height); 071 privateSetWidth(width); 072 } 073 074 /** 075 * Empty constructor. 076 * <p> <code>height</code> and <code>width</code> set to 077 * <code>DEFAULT_HEIGHT</code> and <code>DEFAULT_WIDTH</code> respectivly 078 */ 079 public Rectangle() 080 { 081 this(DEFAULT_HEIGHT, DEFAULT_WIDTH); 082 } 083 084 /** 085 * Copy constructor. 086 * @param rectangle this object will have <code>height</code> and 087 * <code>width</code> set to parameter's values 088 */ 089 public Rectangle(Rectangle rectangle) 090 { 091 this(rectangle.height, rectangle.width); 092 } 093 094 /** 095 * This object height value. 096 * @return this object height value 097 */ 098 public double getHeight() 099 { 100 return (height); 101 } 102 103 /** 104 * This object width value. 105 * @return this object width value 106 */ 107 public double getWidth() 108 { 109 return (width); 110 } 111 112 /** 113 * Set this object height value. 114 * @param height set this object height value to the parameter 115 */ 116 public void setHeight(double height) 117 { 118 privateSetHeight(height); 119 } 120 121 /** 122 * Set this object width value. 123 * @param width set this object width value to the parameter 124 */ 125 public void setWidth(double width) 126 { 127 privateSetHeight(height); 128 } 129 130 /** 131 * Complete set. 132 * @param height height 133 * @param width width 134 */ 135 public void setRectangle(double height, double width) 136 { 137 setHeight(height); 138 setWidth(width); 139 } 140 141 /** 142 * Empty set. 143 * <p> <code>height</code> and <code>width</code> set to 144 * <code>DEFAULT_HEIGHT</code> and <code>DEFAULT_WIDTH</code> respectivly 145 */ 146 public void setRectangle() 147 { 148 setRectangle(DEFAULT_HEIGHT, DEFAULT_WIDTH); 149 } 150 151 /** 152 * Copy set. 153 * @param rectangle this object will have <code>height</code> and 154 * <code>width</code> set to parameter's values 155 */ 156 public void setRectangle(Rectangle rectangle) 157 { 158 setRectangle(rectangle.height, rectangle.width); 159 } 160 161 /** 162 * The area of this object. 163 * @return the area of this object 164 */ 165 public double area() 166 { 167 return (height * width); 168 } 169 170 /** 171 * The perimeter of this object. 172 * @return the perimeter of this object 173 */ 174 public double perimeter() 175 { 176 return (2.0 * (height + width)); 177 } 178 179 /** 180 * A String representation of this object. 181 * @return Rectangle[height = value, width = value] 182 */ 183 public String toString() 184 { 185 return("Rectangle[height = " + height + ", width = " + width + "]"); 186 } 187} 188 189