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

Write a program that defines a class Date and implement it as required. The clas

ID: 673806 • Letter: W

Question

Write a program that defines a class Date and implement it as required. The class Date should consist of three private member variables: year of type int, month of type int, and day of type int. The class Date should also include the following public member functions:

A constructor to initialize year, month, and day with default parameters: The default value of year, month, and day is 1900, 1, and 1, respectively.

setDate to set the year, month and day according to the parameters. Data validation should be provided.
A valid year is an integer between 1900 and 2015 inclusively.
A valid month is an integer between 1 and 12 inclusively.

A valid day should be an integer between
1 and 31 when month is 1, 3, 5, 7, 8, 10, or 12;
1 and 30 when month is 4, 6, 9, or 11;
1 and 28 when month is 2, i.e., simply make 28 days for February.

Use 1900 for the year, 1 for the month, and 1 for the day, respectively, if a value is invalid.

print to output the year, month, and day in the format of yyyy-mm-dd.

getYear to return the year.

getMonth to return the month.

getDay to return the day.

equalDate to compare two Date objects’ values. Return true if they are the same, otherwise, return false. equalDate has a formal parameter which is a Date object.

You should put the class definition in a header file Date.h and put the class implementation in an implementation file Date.cpp. Write a test program to test your class implementation.

Explanation / Answer

public class Date

{

private int year;

private int month;

private int day;

private boolean shortDisplay=true;

public Date(){

setDate(1900,1,1);

}

public Date(int yyyy,int mm,int dd){

setDate();

}

public Date(int yyyy, int mm){

set(yyyy,mm);

}

public Date(int yy){

setDate(yyyy);

}

public Date(String dateStr){

setDate(dateStr);

}

public void setDate(int yyyy,int mm,int dd){

setYear(yyyy);

setMonth(mm);

setDay(dd);

}

public void setDate(Date otherDate){

int yyyy=getYear();

int mm=getMonth();

int dd=getDay();

setYear();

setMonth();

setDay();

}

public void setDate(String dateStr ){

int length=dateStr.length();

index indexCount=0;

if(length>=0) {

if(dateStr.indexOf('/')>=0){

int index=dateStr.indexOf('/');

indexCount++;

String dd=dateStr.substring(index+1, dateStr.length());

if(dd.substring(1,dd.length()).lastIndexOf('/')>=0){

int index2=dd.lastIndexOf('/');

indexCount++;

String yyyy=dd.substring(index2+1,dd.length());

if(yy.lastIndexOf('/')>=0){

indexCount++;

}

String mm=dateStr.substring(0,index);

dd=dd.substring(0,index2);

yyyy=yyyy.substring(0,yyyy.length());

System.out.println(mm+" "+dd+" "+yyyy);

int y=Integer.parseInt(yyyy);

int m=Integer.parseInt(mm);

int d=Integer.parseInt(dd);

}

}
}

}

public void setYear(int yyyy){

if(yyyy>=1900 && yyyy<=2015){

year=yyyy;

}else{

throw new IllegalArgumentException("error") ;

}

}

public void setMonth(int mm){

if(mm>=1 && mm>=12){

month=mm;

}else{

throw new IllegalArgumentException("Error");

}

}

public void setDay(int dd){

if(dd>=1 && dd<=31){

day=dd;

} else {

throw IllegalArgumentException("Error");

}

}

public int getYear(){

return year;

}

public int getMonth(){

return month;

}

public int getDay(){

return day;

}

public void setLongDisplay(){

shortDisplay=false;

}

public boolean isShortDisplay(){

if(shortDisplay){

return true;

} else [ return false;

}
}

public void incrementDay(){

day++;

if(day>31){

day=1;

incrementMonth();

}

}

public void incrementMonth(){

month++;

if(day>12){

month=1;

incrementYear();

}

}

public void incrementYear(){

year++;

}

public boolean equals(Object other){

Date that=(Date) other;

if(this.day==that.day && this.monthy==that.month && this.year==that.year){

return true;

} else {

return false;

}

}

public boolean before(Date otherDate){

Date that =other Date;

if(this.year<that.year) return true;

if(this.month<that.month) return true;

if(this.day<that.day) return true;

else return false;

}

public String toString(){

boolean check=isShortDisplay();

if(check){

if(month<10 && day>10)

{

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

}

if(month <10 && day>10){

return "0"+month+""+day+""+year;

}

if(month>10 || day<10){

return month+""+"0"+day+""+year;

}

} else { return monthString(month)+" "+day+","+year;

}

}

public static String monthString(int month){

if(month>=1 && month<=12){

String[] stringMonth={" ","January","February","March","April","May","June","July","August","September","October","November","December"};

return stringMonth[month];

}

else

{

throw new IllegalArgumentException();

}

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote