Specify and Design a class for an abstract data type that models a day of the ye
ID: 3619781 • Letter: S
Question
Specify and Design a class for an abstract data type that models a day of the year. (note that the actual code to implement the class is not needed)The Date object should store three int values with the month between 1 and 12, day between 1 and 31 (Or, for SOME months, 28, 29, or 30), and year between 0 and 2100. Provide AT LEAST the following:
• default constructor should set dat to Jan 1, 2000
• constructor with three ints (M, D, Y)
• constructor with string and 2 ints - Month (as “January”) should be converted to an integer, and the day and the year from the 2 ints
• three accessors for the three data items
• displayMonth returns the Month as a String
• displayDate returns the entire Date as a string, using displayMonth for the month
• compareDates returns -1 if the Date object it’s applied to comes before the argument Date, 0 if the two dates are equal, and +1 if the Date object is after the argument Date
• validMonth returns true is the value is a valid month
Explanation / Answer
This is a simple possibility (sorry I wrote in code, it's just easier for me to express): abstract class Date {//fields
private int day, month, year;
//constructors
//default constructor
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
//with all int values
public Date(int d, int m, int y) {
this.day = d;
this.month = m;
this.year = y;
}
//with month string value
public Date(int d, String m, int y) {
this.day = d;
this.year = y;
if(m.compareToIgnoreCase("january") == 0) {
this.month = 1;
}
else if (m.compareToIgnoreCase("february") == 0) {
this.month = 2;
}
else if (m.compareToIgnoreCase("march") == 0) {
this.month = 3;
}
else if (m.compareToIgnoreCase("april") == 0) {
this.month = 4;
}
else if (m.compareToIgnoreCase("may") == 0) {
this.month = 5;
}
else if (m.compareToIgnoreCase("june") == 0) {
this.month = 6;
}
else if (m.compareToIgnoreCase("july") == 0) {
this.month = 7;
}
else if (m.compareToIgnoreCase("august") == 0) {
this.month = 8;
}
else if (m.compareToIgnoreCase("september") == 0) {
this.month = 9;
}
else if (m.compareToIgnoreCase("october") == 0) {
this.month = 10;
}
else if (m.compareToIgnoreCase("november") == 0) {
this.month = 11;
}
else if (m.compareToIgnoreCase("december") == 0) {
this.month = 12;
}
else { // invalid month entered
this.month = -1;
}
}
//end of constructors
//accessors for day month and year
public void setDay(int d) {
this.day = d;
}
public int getDay() {
return this.day;
}
public void setMonth(int m) {
this.month = m;
}
public int getMonth() {
return this.month;
}
public void setYear(int y) {
this.year = y;
}
public int getYear() {
return this.year;
}
//end of accessors
public String displayMonth(){
switch(this.month) {
case 1:
return "January";
break;
case 2:
return "February";
break;
case 3:
return "March";
break;
case 4:
return "April";
break;
case 5:
return "May";
break;
case 6:
return "June";
break;
case 7:
return "July";
break;
case 8:
return "August";
break;
case 9:
return "September";
break;
case 10:
return "October";
break;
case 11:
return "November";
break;
case 12:
return "December";
break;
default:
return "Unknown";
break;
}
} // end of displayMonth
//display date as " month_string day year "
public String displayDate () {
String fullDate = this.displayMonth() + " " + this.day + " " + this.year; return fullDate;
}
//compareDates
public int compareDates (Date date){
//we could keep if-elsing forever here or pick a shortcut: use String comparison for "y-m-d"
// the compareTo() method compares the 2 strings lexicographically and returns -1, 0 or 1 with
// the same conditions
String thisDate = this.year + "-" + this.month + "-" + this.day;
String argDate = date.year + "-" + date.month + "-" + date.day;
return thisDate.compareTo(argDate);
}
//validMonth int between 1 and 12
public Boolean validMonth(int m){
if ((m > 0) && (m < 13))
return true;
else
return false;
}
//other methods to implement on concrete classes
abstract void method1();
abstract Object method2();
} // end of abs class Day you could do the month string work more elegantly with an enum, like this: public enum textMonth {
JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER
} abstract class Date {
//fields
private int day, month, year;
//constructors
//default constructor
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
//with all int values
public Date(int d, int m, int y) {
this.day = d;
this.month = m;
this.year = y;
}
//with month string value
public Date(int d, String m, int y) {
this.day = d;
this.year = y;
if(m.compareToIgnoreCase("january") == 0) {
this.month = 1;
}
else if (m.compareToIgnoreCase("february") == 0) {
this.month = 2;
}
else if (m.compareToIgnoreCase("march") == 0) {
this.month = 3;
}
else if (m.compareToIgnoreCase("april") == 0) {
this.month = 4;
}
else if (m.compareToIgnoreCase("may") == 0) {
this.month = 5;
}
else if (m.compareToIgnoreCase("june") == 0) {
this.month = 6;
}
else if (m.compareToIgnoreCase("july") == 0) {
this.month = 7;
}
else if (m.compareToIgnoreCase("august") == 0) {
this.month = 8;
}
else if (m.compareToIgnoreCase("september") == 0) {
this.month = 9;
}
else if (m.compareToIgnoreCase("october") == 0) {
this.month = 10;
}
else if (m.compareToIgnoreCase("november") == 0) {
this.month = 11;
}
else if (m.compareToIgnoreCase("december") == 0) {
this.month = 12;
}
else { // invalid month entered
this.month = -1;
}
}
//end of constructors
//accessors for day month and year
public void setDay(int d) {
this.day = d;
}
public int getDay() {
return this.day;
}
public void setMonth(int m) {
this.month = m;
}
public int getMonth() {
return this.month;
}
public void setYear(int y) {
this.year = y;
}
public int getYear() {
return this.year;
}
//end of accessors
public String displayMonth(){
switch(this.month) {
case 1:
return "January";
break;
case 2:
return "February";
break;
case 3:
return "March";
break;
case 4:
return "April";
break;
case 5:
return "May";
break;
case 6:
return "June";
break;
case 7:
return "July";
break;
case 8:
return "August";
break;
case 9:
return "September";
break;
case 10:
return "October";
break;
case 11:
return "November";
break;
case 12:
return "December";
break;
default:
return "Unknown";
break;
}
} // end of displayMonth
//display date as " month_string day year "
public String displayDate () {
String fullDate = this.displayMonth() + " " + this.day + " " + this.year; return fullDate;
}
//compareDates
public int compareDates (Date date){
//we could keep if-elsing forever here or pick a shortcut: use String comparison for "y-m-d"
// the compareTo() method compares the 2 strings lexicographically and returns -1, 0 or 1 with
// the same conditions
String thisDate = this.year + "-" + this.month + "-" + this.day;
String argDate = date.year + "-" + date.month + "-" + date.day;
return thisDate.compareTo(argDate);
}
//validMonth int between 1 and 12
public Boolean validMonth(int m){
if ((m > 0) && (m < 13))
return true;
else
return false;
}
//other methods to implement on concrete classes
abstract void method1();
abstract Object method2();
} // end of abs class Day you could do the month string work more elegantly with an enum, like this: public enum textMonth {
JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER
} public enum textMonth {
JANUARY, FEBRUARY, MARCH, APRIL,
MAY, JUNE, JULY, AUGUST, SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.