root/trunk/Time.java
| Revision 3, 8.9 kB (checked in by dpaola2, 1 month ago) |
|---|
| Line | |
|---|---|
| 1 | /*-************************************************************************************** |
| 2 | * Time.java |
| 3 | * |
| 4 | * CLASS: Time Andres J. Tack |
| 5 | * February, 2005 |
| 6 | * SIGSoft, ACM @ UIUC |
| 7 | * |
| 8 | * This is an object to store time; it may be relative, it can be absolute. At the |
| 9 | * core, it tracks minutes, hours, and seconds. |
| 10 | * |
| 11 | * METHODS AVAILABLE: |
| 12 | * Constructors: - Time() Defaults to zero time. |
| 13 | * - Time(hours, minutes, seconds) Initializes |
| 14 | * |
| 15 | * Operations: |
| 16 | * equalsTime(Time) doesNotEqualTime(Time) |
| 17 | * getSumWithTime(Time) sumTogetherWithTime(Time) |
| 18 | * assignTime(Time) |
| 19 | * isLessThanTime(Time) isLessThanOrEqualToTime(Time) |
| 20 | * isGreaterThanTime(Time) isGreaterThanOrEqualToTime(Time) |
| 21 | * |
| 22 | * Accessors: - getSeconds() |
| 23 | * - getMinutes() |
| 24 | * - getHours() |
| 25 | * |
| 26 | * Other: - timeDifferenceBetween(time2) Get a Time object |
| 27 | * representing the difference between the |
| 28 | * current object and time2. |
| 29 | ****************************************************************************************/ |
| 30 | public class Time |
| 31 | { |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * DEFAULT CONSTRUCTOR |
| 36 | * |
| 37 | * The default time is zeros all across. |
| 38 | */ |
| 39 | public Time() |
| 40 | { |
| 41 | hours = 0; |
| 42 | minutes = 0; |
| 43 | seconds = 0; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * CONSTRUCTOR |
| 48 | * @param givenHour Of type int, represents hours of time. |
| 49 | * @param givenMinute Of type int, represents minutes of time. |
| 50 | * @param givenSecond Of type int, represents seconds... |
| 51 | * |
| 52 | * This constructor will create a Time object with valid value. |
| 53 | */ |
| 54 | public Time(int givenHours, int givenMinutes, int givenSeconds) |
| 55 | { |
| 56 | // Initialize everything to zero. |
| 57 | hours = minutes = seconds = 0; |
| 58 | // Check for bad values; note that we assume positives. |
| 59 | for ( this.seconds = givenSeconds; |
| 60 | this.seconds >= 60; |
| 61 | seconds -= 60 ) |
| 62 | ++minutes; |
| 63 | |
| 64 | for ( this.minutes += givenMinutes; |
| 65 | this.minutes >= 60; |
| 66 | this.minutes -= 60 ) |
| 67 | ++hours; |
| 68 | |
| 69 | this.hours += givenHours; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * METHOD timeDifferenceFrom |
| 75 | * @param time2 The other end of the time difference. |
| 76 | * |
| 77 | * Returns a Time object with value equivalent to the time difference |
| 78 | * between the parameter and the current instance. The order of the time does not matter. |
| 79 | */ |
| 80 | public Time timeDifferenceFrom(Time time2) |
| 81 | { |
| 82 | // We always want the lesser time to be the parameter! |
| 83 | // This because our algorithm works this way. |
| 84 | if ( time2.isGreaterThanTime(this) ) |
| 85 | return time2.timeDifferenceFrom(this); |
| 86 | else |
| 87 | { |
| 88 | // Start by subtracting the hours simply |
| 89 | int hoursDiff = this.hours - time2.hours; |
| 90 | |
| 91 | |
| 92 | // Now we check to see if minutes are inverted. |
| 93 | // We don't have to worry about taking the hours less than |
| 94 | // zero because we know THIS is the greater time. |
| 95 | int minutesDiff; |
| 96 | |
| 97 | if ( time2.minutes > this.minutes ) { |
| 98 | hoursDiff--; |
| 99 | minutesDiff = 60 - ( time2.minutes - this.minutes ); |
| 100 | } |
| 101 | else |
| 102 | minutesDiff = this.minutes - time2.minutes; |
| 103 | |
| 104 | |
| 105 | // Repeat the above for seconds. |
| 106 | int secondsDiff; |
| 107 | |
| 108 | if ( time2.seconds > this.seconds ) { |
| 109 | minutesDiff--; |
| 110 | secondsDiff = 60 - ( time2.seconds - this.seconds ); |
| 111 | } |
| 112 | else |
| 113 | secondsDiff = this.seconds - time2.seconds; |
| 114 | |
| 115 | // We return the difference. |
| 116 | Time diffTime = new Time( hoursDiff, minutesDiff, secondsDiff ); |
| 117 | return diffTime; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * equalsTime(Time otherTime) |
| 123 | * |
| 124 | * @param otherTime This is the other time considered in the test. |
| 125 | * |
| 126 | * Two times are equal if all of their components are equal (hours, |
| 127 | * minutes, seconds). In all other cases, the comparison is false. |
| 128 | */ |
| 129 | public boolean equalsTime(Time otherTime) |
| 130 | { |
| 131 | // Check for any difference in time members. |
| 132 | if ( otherTime.hours != this.hours || |
| 133 | otherTime.minutes != this.minutes || |
| 134 | otherTime.seconds != this.seconds ) |
| 135 | return false; |
| 136 | |
| 137 | // If we get this far, these Times are equal |
| 138 | else |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * doesNotEqualTime( Time operand2 ) |
| 147 | * |
| 148 | * @param operand2 The other time to be considered in this test. |
| 149 | * |
| 150 | * Form: Time != Time |
| 151 | */ |
| 152 | public boolean doesNotEqualTime(Time otherTime) |
| 153 | { |
| 154 | return !(this.equalsTime(otherTime)); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | /*- |
| 161 | * getSumWithTime( Time operand2 ) |
| 162 | * |
| 163 | * Equivalent to the + operator, this performs the addition of 'this' time and |
| 164 | * the parameterized time. The result is not assigned to 'this'. |
| 165 | * |
| 166 | * @param operand2 This is the other time being summed with the current. |
| 167 | * |
| 168 | * Seconds are kept less than sixty, always. Minutes are kept less than sixty. |
| 169 | * Hours are NOT capped. You can have as many hours as you want. |
| 170 | */ |
| 171 | public Time getSumWithTime( Time operand2 ) |
| 172 | { |
| 173 | // These three ints hold the sums of the different time components |
| 174 | int secondsAdded = 0; |
| 175 | int minutesAdded = 0; |
| 176 | int hoursAdded = 0; |
| 177 | |
| 178 | // We start out at the smallest level, adding seconds. |
| 179 | secondsAdded = this.seconds + operand2.seconds; |
| 180 | |
| 181 | // Now see if we need to normalize below 60 again for SECONDS |
| 182 | if (secondsAdded >= 60) { |
| 183 | // We depend on the fact that every Time object should be normalized. |
| 184 | minutesAdded += 1; |
| 185 | secondsAdded -= 60; |
| 186 | } |
| 187 | |
| 188 | // Repeat above for minutes |
| 189 | minutesAdded += this.minutes + operand2.minutes; |
| 190 | if ( minutesAdded >= 60 ) { |
| 191 | hoursAdded += 1; |
| 192 | minutesAdded -= 60; |
| 193 | } |
| 194 | |
| 195 | // Hours are simple; we can have as many as necessary. |
| 196 | hoursAdded += this.hours + operand2.hours; |
| 197 | |
| 198 | |
| 199 | // We return the time object which we got. |
| 200 | Time sumTime = new Time(hoursAdded, minutesAdded, secondsAdded); |
| 201 | return sumTime; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * sumTogetherWithTime( Time operand2 ) |
| 208 | * |
| 209 | * @param operand2 This is the time with which 'this' will be summed. |
| 210 | * |
| 211 | * @returns this |
| 212 | * |
| 213 | * This performs the correct addition of two Time objects. Seconds |
| 214 | * are kept less than sixty, always. Minutes are kept less than sixty. |
| 215 | * Hours are NOT capped. You can have as many hours as you want. |
| 216 | * |
| 217 | * The result of the addition, in this case, is stored in THIS object. |
| 218 | */ |
| 219 | public Time sumTogetherWithTime ( Time operand2 ) |
| 220 | { |
| 221 | this.seconds = this.getSumWithTime(operand2).seconds; |
| 222 | this.minutes = this.getSumWithTime(operand2).minutes; |
| 223 | this.hours = this.getSumWithTime(operand2).hours; |
| 224 | |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | /*- |
| 231 | * assignTime( Time newTime ) |
| 232 | * |
| 233 | * @param newTime The new time which will be assigned to 'this' |
| 234 | * |
| 235 | * The assignment is a simple copy of one Time to another. |
| 236 | */ |
| 237 | public void assignTime( Time newTime ) |
| 238 | { |
| 239 | this.seconds = newTime.seconds; |
| 240 | this.minutes = newTime.minutes; |
| 241 | this.hours = newTime.hours; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | /*_ |
| 247 | * isLessThanTime( Time operand2 ) |
| 248 | * |
| 249 | * @param operand2 The other time which is being tested against this one. |
| 250 | * |
| 251 | * @returns true if 'this' is less than operand2 |
| 252 | * @returns false otherwise |
| 253 | * |
| 254 | * Relational operator, works like the times of day. (16:50:04 is less than 17:05:06) |
| 255 | */ |
| 256 | public boolean isLessThanTime ( Time operand2 ) |
| 257 | { |
| 258 | if ( this.hours < operand2.hours ) |
| 259 | return true; |
| 260 | else if ( this.hours == operand2.hours ) { |
| 261 | if ( this.minutes < operand2.minutes ) |
| 262 | return true; |
| 263 | else if ( this.minutes == operand2.minutes ) { |
| 264 | if ( this.seconds < operand2.seconds ) |
| 265 | return true; |
| 266 | else |
| 267 | return false; |
| 268 | } |
| 269 | else |
| 270 | return false; |
| 271 | } |
| 272 | else |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | |
| 278 | /*_ |
| 279 | * isLessThanOrEqualToTime( Time operand2 ) |
| 280 | * |
| 281 | * @param operand2 the other time which is being tested against this one. |
| 282 | * |
| 283 | * @returns true if 'this' is less than or equal to operand2 |
| 284 | * @returns false otherwise. |
| 285 | * |
| 286 | * Relational operator, works like the times of day. |
| 287 | */ |
| 288 | public boolean isLessThanOrEqualToTime( Time operand2 ) |
| 289 | { |
| 290 | if ( this.isLessThanTime(operand2) || |
| 291 | this.equalsTime(operand2) ) |
| 292 | return true; |
| 293 | else |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /*_ |
| 299 | * isGreaterThanTime( Time operand2 ) |
| 300 | * |
| 301 | * @param operand2 The other time that 'this' is being compared to. |
| 302 | * |
| 303 | * @returns true if 'this' is strictly greater than operand2 |
| 304 | * @returns false otherwise |
| 305 | * |
| 306 | * Relational operator, works like the times of day. (18:50:04 is greater than 17:05:06) |
| 307 | */ |
| 308 | public boolean isGreaterThanTime( Time operand2 ) |
| 309 | { |
| 310 | if ( !(this.isLessThanTime(operand2) ) && |
| 311 | !(this.equalsTime(operand2) ) ) |
| 312 | return true; |
| 313 | else |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | /*_ |
| 320 | * isGreaterThanOrEqualToTime( Time operand2 ) |
| 321 | * |
| 322 | * @param operand2 The other time that 'this' is being compared to. |
| 323 | * |
| 324 | * @returns true if 'this' is either greater than or equal to operand2. |
| 325 | * @returns false otherwise |
| 326 | * |
| 327 | * Relational operator, works like the times of day. (16:50:04 is less than 17:05:06) |
| 328 | */ |
| 329 | public boolean isGreaterThanOrEqualToTime( Time operand2 ) |
| 330 | { |
| 331 | if ( !( this.isLessThanTime(operand2) ) ) |
| 332 | return true; |
| 333 | else |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | |
| 339 | /*_ |
| 340 | * METHOD: getSeconds() |
| 341 | * |
| 342 | * Accessor Function. |
| 343 | */ |
| 344 | public int getSeconds() |
| 345 | { |
| 346 | return seconds; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /*_ |
| 351 | * METHOD: getMinutes() |
| 352 | * |
| 353 | * Accessor Function. |
| 354 | */ |
| 355 | public int getMinutes() |
| 356 | { |
| 357 | return minutes; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /*_ |
| 362 | * METHOD: getHours() |
| 363 | * |
| 364 | * Accessor Function. |
| 365 | */ |
| 366 | public int getHours() |
| 367 | { |
| 368 | return hours; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | /***********************************/ |
| 373 | /****** DATA MEMBERS BELOW!!! ******/ |
| 374 | /***********************************/ |
| 375 | private int hours; |
| 376 | private int minutes; |
| 377 | private int seconds; |
| 378 | } |
Note: See TracBrowser for help on using the browser.
