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