Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with the following Java assignment. I need a code that is basic and

ID: 2247576 • Letter: I

Question

I need help with the following Java assignment. I need a code that is basic and easy to understand. All I need is for the program to return the values stated.

Modify the Date class so that it includes a compareTo method with signature

int compareTo(Date anotherDate)

This method should return the value 0 if this date (the date of the object upon which the method is invoked) is equal to the argument date; a value less than 0 if this date is a date earlier than the argument date; and a value greater than 0 if this date is a date later than the argument date. Create a test driver that shows that your method performs correctly.

Explanation / Answer

Date.java

public class Date {

//Declaring instance variables

private int day;

private int month;

private int year;

//Parameterized constructor

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

super();

this.day = day;

this.month = month;

this.year = year;

}

//getters and setters

public int getDay() {

return day;

}

public void setDay(int day) {

this.day = day;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

//This method will compares two dates and return a value based on that

int compareTo(Date anotherDate)

{

int val=0 ;

  

if(this.day==anotherDate.day && this.month==anotherDate.month && this.year==anotherDate.year)

{

val= 0;

}

else if(this.day>anotherDate.day && this.month==anotherDate.month && this.year==anotherDate.year)

{

val= 1;

}

else if(this.day>anotherDate.day && this.month==anotherDate.month && this.year==anotherDate.year)

{

val= 1;

}

else if(this.day>=anotherDate.day && this.month>=anotherDate.month && this.year>anotherDate.year)

{

val= 1;

}

else if(this.day>=anotherDate.day && this.month>anotherDate.month && this.year<=anotherDate.year)

{

val= -1;

}

else if(this.day<anotherDate.day && this.month==anotherDate.month && this.year==anotherDate.year)

{

val= -1;

}

else if(this.day<=anotherDate.day && this.month<anotherDate.month && this.year==anotherDate.year)

{

val= -1;

}

else if(this.day<=anotherDate.day && this.month<=anotherDate.month && this.year<anotherDate.year)

{

val= -1;

}

else if(this.day<=anotherDate.day && this.month<=anotherDate.month && this.year>anotherDate.year)

{

val= 1;

}

return val;

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

return "Date ["+day + "-" + month + "-" + year + "]";

}

}

____________________

Test.java

public class Test {

public static void main(String[] args) {

//Creating Parameterized constructors

Date d1=new Date(11, 9,1994);

Date d2=new Date(11, 9,1997);

//Calling the method

int res=(d1.compareTo(d2));

//Based on the res values the corresponding statement will be executed

if(res==0)

{

System.out.println(d1.toString()+" is equal to "+d2.toString());

}

else if(res<0)

{

System.out.println(d1.toString()+" comes before "+d2.toString());

}

else if(res>0)

{

System.out.println(d1.toString()+" comes after "+d2.toString());

}

}

}

____________________

Output:

Date [11-9-1994] comes before Date [11-9-1997]

______________Thank You