Question: Using the code below add the compareTo method to your Date class. It has one parameter of Date type. If this date is before the

Using the code below add the compareTo method to your Date class. It has one parameter of Date type. If this date is before the given date in the parameter, return -1; if it is after the given date, return 1; otherwise return 0 (meaning they are the same date). You also need to add implements Comparable in the class heading.

package activity4;

public class Date{

private int month, day, year;

//default constructor date

public Date() {

this.month = 1;

this.day = 1;

this.year = 2000;

}

//the three parameter constructor include the month, day and year

public Date(int month, int day, int year) {

this.month = month;

this.day = day;

this.year = year;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

//returns the date into a string

public String toString() {

return month + "/" + day + "/" + year;

}

public void printDate() {

System.out.println(month + "/" + day + "/"+ year);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!