logo Practice-It logo

BJP3 Exercise 10.20: ComparableCalendarDate

Related Links:
Author: Marty Stepp (on 2013/04/01)

Suppose that a class CalendarDate has been defined for storing a calendar date with month, day and year components. (In our section handout, the class was called Date, but we have renamed it here because Practice-It uses a class named Date for other purposes.) The class includes the following members:

private int year the year component
private int month the month component
private int day the day component
public CalendarDate(int year, int month, int day) constructs a date with given year, month, day
public int getYear() returns the year component
public int getMonth() returns the month component
public int getDay() returns the day component
public String toString() returns the date in yyyy/mm/dd format

Your task is to modify the class to be Comparable by adding an appropriate compareTo method. Dates that occur chronologically earlier should be considered "less" than dates that occur later.

You may assume that dates are constructed with appropriate values: Months will be between 1 and 12, days will be between 1 and 31 and years will be four-digit numbers. Assume that the toString method produces a standard format with two digits for the month and two digits for the day, including leading zeros if necessary. For example, January 7th, 2005, would return the string "2005/01/07".

Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.