I need help completing the compareTo method in the following class: class Date21
ID: 3599470 • Letter: I
Question
I need help completing the compareTo method in the following class:
class Date212{
private String monthsInYear[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
private int month;
private String monthString;
private int day;
private int year;
public Date212 (int y, int m, int d) {
//Validating month
if(m < 1 || m > 12)
throw new IllegalArgumentException("Month cannot be less than 1 or more than 12.");
//Validating day
if(d < 1 || d > 31)
throw new IllegalArgumentException("Day cannot be less than 1 or more than 32.");
{
month=m;
day=d;
year=y;
monthString = monthsInYear[m];
}
}
public int getday() {
return day;
}
public int getmonth() {
return month;
}
public int getyear() {
return year;
}
public void setday(int d) {
if(d<1 || d>31)
throw new IllegalArgumentException("Day must be more than 0 and less than or equal to 31.");
day=d;
}
public void setmonth(int m) {
if(m<1 || m>12)
throw new IllegalArgumentException("Month must be more than 0 and less than 13.");
month=m;
}
public void setyear(int y) {
year=y;
}
public Date212 (String d)
{
//Calling three argument constructor
this(Integer.parseInt(d.substring(0, 4)), Integer.parseInt(d.substring(4, 6)), Integer.parseInt(d.substring(6, 8)));
}
//Method that returns date in the required format
public String toString()
{
return monthString + " " + String.format("%02d", day) + "," + String.format("%04d", year);
}
public static void main(String[] args) {
Date212 date1 = new Date212("20130531");
Date212 date2 = new Date212("20121231");
System.out.println(date1.year);
System.out.println(date1.month);
System.out.println(date1.day);
System.out.println(date1.toString());
System.out.println("compare: "+date1.compareTo(date2));
}
public int compareTo(Date212 other){
// compare this date212 with other date212
// compare the year first, then the month, then the day
// return -1 if this < other
// return 0 if this == other
// return 1 if this > other
return 1;
}
}
Explanation / Answer
Date212.java
public class Date212{
private String monthsInYear[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
private int month;
private String monthString;
private int day;
private int year;
public Date212 (int y, int m, int d) throws IllegalArgumentException {
//Validating month
if(m < 1 || m > 12)
throw new IllegalArgumentException("Month cannot be less than 1 or more than 12.");
//Validating day
if(d < 1 || d > 31)
throw new IllegalArgumentException("Day cannot be less than 1 or more than 32.");
{
month=m;
day=d;
year=y;
monthString = monthsInYear[m];
}
}
public int getday() {
return day;
}
public int getmonth() {
return month;
}
public int getyear() {
return year;
}
public void setday(int d) throws IllegalArgumentException {
if(d<1 || d>31)
throw new IllegalArgumentException("Day must be more than 0 and less than or equal to 31.");
day=d;
}
public void setmonth(int m) throws IllegalArgumentException {
if(m<1 || m>12)
throw new IllegalArgumentException("Month must be more than 0 and less than 13.");
month=m;
}
public void setyear(int y) {
year=y;
}
public Date212 (String d) throws NumberFormatException, IllegalArgumentException
{
//Calling three argument constructor
this(Integer.parseInt(d.substring(0, 4)), Integer.parseInt(d.substring(4, 6)), Integer.parseInt(d.substring(6, 8)));
}
//Method that returns date in the required format
public String toString()
{
return monthString + " " + String.format("%02d", day) + "," + String.format("%04d", year);
}
public static void main(String[] args) throws NumberFormatException, IllegalArgumentException {
Date212 date1 = new Date212("20130531");
Date212 date2 = new Date212("20121231");
System.out.println(date1.year);
System.out.println(date1.month);
System.out.println(date1.day);
System.out.println(date1.toString());
System.out.println("compare: "+date1.compareTo(date2));
}
public int compareTo(Date212 other){
// compare this date212 with other date212
// compare the year first, then the month, then the day
// return -1 if this < other
// return 0 if this == other
// return 1 if this > other
if(this.year < other.year) {
return -1;
} else if(this.year > other.year) {
return 1;
} else {
if(this.month < other.month) {
return -1;
} else if(this.month > other.month) {
return 1;
} else {
if(this.day < other.day) {
return -1;
} else if(this.day > other.day) {
return 1;
} else {
return 0;
}
}
}
}
}
Output:
2013
5
31
May 31,2013
compare: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.