design a class Date to store a date. Define the method compareTo for the class D
ID: 3652999 • Letter: D
Question
design a class Date to store a date. Define the method compareTo for the class Date. Also write a program to test your method. now i have done most of this program but now i want to add to where you ask the for the two dates to compare. unfortunately i got called into work till 10 and i have to have this in by midnight. here is the code i've done so far package date; import java.util.*; public class Date implements Comparable { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //The instance variables dMonth, dDay, and dYear are set to //the default values. //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //The instance variables dMonth, dDay, and dYear are set //according to the parameters. //Postcondition: dMonth = month; dDay = day; // dYear = year; public Date(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to set the date //The instance variables dMonth, dDay, and dYear are set //according to the parameters. //Postcondition: dMonth = month; dDay = day; // dYear = year; public void setDate(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to return the month //Postcondition: The value of dMonth is returned. public int getMonth() { return dMonth; } //Method to return the day //Postcondition: The value of dDay is returned. public int getDay() { return dDay; } //Method to return the year //Postcondition: The value of dYear is returned. public int getYear() { return dYear; } //Method to return the date in the form mm-dd-yyyy public String toString() { return (dMonth + "-" + dDay + "-" + dYear); } public boolean equals(Object otherDate) { Date temp = (Date) otherDate; return (dYear == temp.dYear && dMonth == temp.dMonth && dDay == temp.dDay); } public int compareTo(Object otherDate) { Date temp = (Date) otherDate; int yrDiff = dYear - temp.dYear; if (yrDiff != 0) { return yrDiff; } int monthDiff = dMonth - temp.dMonth; if (monthDiff != 0) { return monthDiff; } return dDay - temp.dDay; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int date1; int date2; System.out.print("Enter Date1: " + date1); System.out.print("Enter Date2: " + date2); if (date1.compareTo(date2) == 0) { System.out.println("Both the dates are the same."); } else if (date1.compareTo(date2) < 0) { System.out.println("date1 is less than date2."); } else { System.out.println("date1 is greater than date2."); } } } any help would be appriciatedExplanation / Answer
please rate - thanks
package date;
import java.util.*;
public class Date implements Comparable
{ private int dMonth; //variable to store the month private
int dDay; //variable to store the day private
int dYear; //variable to store the year
//Default constructor
//The instance variables dMonth, dDay, and dYear are set to
//the default values.
//Postcondition: Month = 1; dDay = 1; dYear = 1900;
public Date()
{ dMonth = 1;
dDay = 1;
dYear = 1900;
}
//Constructor to set the date
//The instance variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public Date(int month, int day, int year)
{ dMonth = month; dDay = day; dYear = year;
}
//Method to set the date
//The instance variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day;
// dYear = year;
public void setDate(int month, int day, int year)
{ dMonth = month;
dDay = day;
dYear = year;
}
//Method to return the month
//Postcondition: The value of dMonth is returned.
public int getMonth()
{ return dMonth; }
//Method to return the day
//Postcondition: The value of dDay is returned.
public int getDay()
{ return dDay; }
//Method to return the year
//Postcondition: The value of dYear is returned.
public int getYear() { return dYear; }
//Method to return the date in the form mm-dd-yyyy
public String toString()
{ return (dMonth + "-" + dDay + "-" + dYear); }
public boolean equals(Object otherDate)
{ Date temp = (Date) otherDate;
return (dYear == temp.dYear && dMonth == temp.dMonth && dDay == temp.dDay); }
public int compareTo(Object otherDate)
{ Date temp = (Date) otherDate;
int yrDiff = dYear - temp.dYear;
if (yrDiff != 0) { return yrDiff; }
int monthDiff = dMonth - temp.dMonth;
if (monthDiff != 0) { return monthDiff; }
return dDay - temp.dDay; }
public static void main(String[] args)
{ Scanner sc = new Scanner(System.in);
Date date1=new Date();
Date date2=new Date();
int m,d,y;
System.out.println("Enter Date1: ");
System.out.print("Enter month: ");
m=sc.nextInt();
System.out.print("Enter day: ");
d=sc.nextInt();
System.out.print("Enter year: ");
y=sc.nextInt();
date1.setDate(m,d,y);
System.out.println("Enter Date2: " );
System.out.print("Enter month: ");
m=sc.nextInt();
System.out.print("Enter day: ");
d=sc.nextInt();
System.out.print("Enter year: ");
y=sc.nextInt();
date2.setDate(m,d,y);
if (date1.compareTo(date2) == 0)
{ System.out.println("Both the dates are the same.");
}
else if (date1.compareTo(date2) < 0)
{ System.out.println("date1 is less than date2.");
}
else
{ System.out.println("date1 is greater than date2.");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.