|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
/** Is this Date valid? * @return true iff the year,month,day correspond to an actual Julian Date. */ boolean isValid() { // Remember the month & day in a variable w/ a short name, for future use: int m = this.getMonth(); int d = this.getDay(); if ((this.getYear() == 0) || (d <= 0) || (m <= 0) || (m > 12)) { return false; } // year and m are valid; check d not too large: else if (m == 2) { if (this.isLeapYear(this.getYear())) { return (d <= 29); } else { return (d <= 28); } } else if ((m==4) || (m==6) || (m==9) || (m==11)) { return (d <= 30); } else { return (d <= 31); } /* The version above checks for Feb, then Apr,Jun,Sep,Nov, * without mentioning the other seven months explicitly. * While doing so would certainly be reasonable, our version actually * reflects the old1 mnemonic: * * Thirty days hath November, * April, June, and September; * All the rest have thirty-one, * Except for February alone, * Which hath twenty-eight days clear, * And twenty-nine in each leap year, * * Our code hews surprisingly close to this mnemonic -- having code * which corresponds to how we think about the problem in real life * is the 0'th of The Laws of Programming! */ } /** Is a given year a leap-year? * @param y The year to inquire about (e.g. 2008). * @return whether yr is a leap-year. */ boolean isLeapYear(int y) { return ((y%4) == 0) && ( ((y%100) != 0) || ((y%400)==0) ); } /** Is this Date the same as another? (Same year, month, and day?) * @param other The Date to compare this Date to. * @return whether this Date represents the same day as `other`. * The result is only guaranteed when ((this.isValid()) && (other.isValid())). */ boolean sameDayAs( Date other ) { // REPLACE THE STUB WITH YOUR ACTUAL CODE } /** Does this date come before another? * @param other The Date to compare this Date to. * @return whether this Date comes before `other`. * The result is only guaranteed when ((this.isValid()) && (other.isValid())). */ boolean comesBefore( Date other ) { return false; // REPLACE THE STUB WITH YOUR ACTUAL CODE } /** Return the day after this one. * @return a Date object corresponding to the day after this Date. * `this` must be valid. The return date will be valid. */ Date nextDay() { Date dayPlus = new Date( this.getYear(), this.getMonth(), this.getDay()+1 ); return dayPlus; // REPLACE THE STUB WITH YOUR ACTUAL CODE // We must check whether dayPlus is a valid day, and if not, // make a *different* new Date ... } /** Return a Date representing the current date (in real time). * @return a Date representing the current date (in real time). */ Date today() { java.util.Calendar c = java.util.Calendar.getInstance(); return new Date( c.get(java.util.Calendar.YEAR), c.get(java.util.Calendar.MONTH)+1, // Calendar starts w/ month 0. c.get(java.util.Calendar.DAY_OF_MONTH) ); } |
Look at the new source you pasted in. Note that some methods have been written for you (isValid, isLeapYear).
However, some methods are left for you to write: sameDayAs, comesBefore, nextDay.
1Versions are documented back to the 1400s! (source: Wikipedia) ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Feb.27 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |