 |
|

|
 |

11/03/09 - More on Classes and Objects
|
 |
 |
 |
  | Create a class called Date that represents a date consisting of a year, month and day.
|
 |
 |
 |
 |
  | A Date object should have the following methods:
|
 |
 |
 |
 |
  | public Date(int year, int month, int day)
|
 |
 |
 |
 |
  | This method constructs a new Date object to represent the given date.
|
 |
 |
 |
 |
  | public void addDays(int days)
|
 |
 |
 |
 |
  | This method moves this Date object forward in time by the given number of days.
|
 |
 |
 |
 |
  | public void addWeeks(int weeks)
|
 |
 |
 |
 |
  | This method moves this Date object forward in time by the given number of 7-day weeks.
|
 |
 |
 |
 |
  | public int daysTo(Date other)
|
 |
 |
 |
 |
  | This method returns the number of days that would need to be added to this Date to make it equal to the given other Date.
|
 |
 |
 |
 |
  | public boolean equals(Object other)
|
 |
 |
 |
 |
  | This method returns true if two Date objects represent the same calendar date, otherwise it returns false.
|
 |
 |
 |
 |
  | This method returns the day of the month for this Date object.
|
 |
 |
 |
 |
  | public String getDayName()
|
 |
 |
 |
 |
  | This method returns the name of the day of the week for this Date object (e.g., "Thursday"). Use the fact that January 1, 1900, was a Monday.
|
 |
 |
 |
 |
  | This method returns the month number (January = 1, ..., December = 12) for this Date object.
|
 |
 |
 |
 |
  | This method returns the the 4-digit year (e.g., 2009) for this Date object.
|
 |
 |
 |
 |
  | public boolean isLeapYear()
|
 |
 |
 |
 |
  | This method returns true if this Date object's year is a leap year. (Leap years are divisible by 400, or they are divisible by 4 but not 100.)
|
 |
 |
 |
 |
  | This method returns a String representation of this Date object in DayName, month/day/year order, such as "Tuesday, 11/03/2009".
|
 |
 |
 |
 |
  | Read pp. 437-474 of the text.
|
 |
 |
 |
 |
  | Finish the Date class and create a test program with a main that does the following:
1. Creates a Date object to represent the date of the Loma Prieta earthquake (October 17, 1989). 2. Calls the Loma Prieta Date object's toString method and prints the result. 3. Creates a Date object to represent today, November 3, 2009. 4. Calls today's Date object's toString method and prints the result. 5. Calls the daysTo method using the Loma Prieta Date object, passes today's Date object as a parameter and prints the result. 6. Calls the addDays method using the Loma Prieta Date object with 2345 as a parameter and then prints the information for that date. 7. Creates a Date object to represent April 1, 1994, and prints the information for that date. 8. Uses the April 1 Date object to call the addWeeks method with a parameter of 59 weeks and prints the information for that date. 9. Calls the daysTo method using the Date object from #8 with the Date object from #6 as a parameter and prints the results. 10. Calls the equals method for the two Date objects from #9 and prints the results.
|
 |
 |
|


 |
 |
 |