You have been asked to extend the Date
class that represents calendar dates such as March 19th. The Date
class includes these constructors and methods:
Method/Constructor |
Description |
public Date(int month, int day) |
constructs a Date object with the given month /day |
public int getMonth() |
returns the month |
public int getDay() |
returns the day |
public void setMonth(int month) |
sets the month to a new value |
public void setDay(int day) |
sets the day to a new value |
public int daysInMonth(int month) |
returns the number of days in the given month (examples: 4->20 , 10->31 , 2->28 ) |
public void nextDay() |
advances to next date, wrapping month if needed (examples: 03/19->03/20 , 01/31->02/01 , 12/31->01/01 ) |
public String toString() |
returns String version of date, such as "03/19 " |
You are to define a new class called CalendarDate
that extends this class through inheritance. It should behave like a Date
except that it should also keep track of the year. You should provide the same methods as the superclass, as well as the following new behavior:
Method/Constructor |
Description |
public CalendarDate(int year, int month, int day) |
constructs a CalendarDate object with the given year /month /day |
public int getYear() |
returns the year |
public void setYear(int year) |
sets the year to a new value |
Some of the existing behaviors from Date
should behave differently on CalendarDate
objects:
- When a
CalendarDate
is printed with toString
, it should be returned in a year/month/day format such as "2009/03/19
". Note that the year comes first. There should be a leading 0
if necessary if the month and/or day is a single-digit number.
- When advancing a
CalendarDate
object to the next date using nextDay
, if this is the last date of the year (December 31st), you should wrap the object to the next year. For example, the next day after December 31st, 2009 is January 1st, 2010.
You must also make CalendarDate
objects comparable to each other using the Comparable
interface. Calendar dates are compared by year, then by month, then by day. In other words, a CalendarDate
object with a smaller year is considered to be "less than" one with a larger year. If two objects have the same year, the one with the lower month is considered "less". If they have the same year and month, the one with the lower day is considered "less". If the two objects have the same year, month, and day, they are considered to be "equal".
You may assume that all year/month/day values passed to your methods and constructors are valid. You should not worry about leap years for this problem, assume that February always has 28 days.