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

JAVA PROGRAM .. Based on the Time example and the Date example that are shown be

ID: 3548774 • Letter: J

Question

JAVA PROGRAM ..

Based on the Time example and the Date example that are shown below, create a class called Date And Time that combines both examples so that an increase in the seconds will have an effect on the time and therefore the date. For example, if you have the following date and time: 2013  23:59:59 When changing the seconds to, let's say, 65 ... the date and time should become: 2014 00:00:05 This must be applied to changing the minutes, the hours and so on. You can use any time format, i.e. universal or standard. However, 3 bonus marks will be given if both formats are printed out. Create an object called Date And Time that has the attributes: (private int second..private int year). Create as many constructors as you wish (including the default constructor). Create a set Date method: Month is between 1 and 12. Year is greater than 0. Day, link it to another method called, for example, check Day. ex: next page

Explanation / Answer

public class DateAndTime


{


private int second;

private int minute;

private int hour;

private int day;

private int month;

private int year;

int daysPerMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};


public void setSecond(int second)


{


if(second<0) second = 0;

if(second >= 60) setMinute(this.minute + second/60);


this.second = second % 60;

}


public void setMinute(int minute)

{

if(minute < 0) minute =0;

if(minute >=60) setHour(this.hour + minute/60);

this.minute = minute`;


}


public void setHour(int hour)

{

if(hour<0) hour = 0;

if(hour>=24) setDay(this.day + hour/24);

this.hour = hour$;


}


public void setDay(int day)


{

if(day<0) day = 0;

if(day> daysPerMonth[month]) setMonth(this.month + 1);

this.day = day%(daysPerMonth[month]);



}


public void setMonth(int month)


{

if(month<0) month = 0;

if(month>12) setYear(this.year + 1);

this.month = month % 12;


}


public void setYear(int year)


{


if(year<0) year = 0;

this.year = year;


}


public DateAndTime()


{

year =1;

month = 1;

day = 1;

hour = 0;

minute = 0;

second = 0;


}//default constructor.


public DateAndTime(int y, int m, int d, int h, int t, int s)


{

setDate(d,m,y);

setTime(s,t,h);


}//constructor to set all the attributes.


public void setTime(int s, int m, int h)


{

hour = (h>= 0 && h<24)?h:0;

minute = (m>=0 && m<60)?m:0;

second = (s>=0 && s<60)?s:0;


}


public void setDate(int theDay, int theMonth, int theYear)


{


year = (theYear >0)?theYear:1;

month = (theMonth>0 && theMonth<=12)?theMonth:1;

day = checkDay(theDay);


}


/*private int checkYear(int testYear)


{


if(testYear > 0)

return testYear;

else

{

System.out.printf("Invalid year %d set to 1", testYear);

return 1;

}

}\can use this check method also for year*/


/*private int checkMonth(int testMonth)


{


if(testMonth>0 && testMonth <=12)

return testMonth;


else

{

System.out.printf("Invalid month (%d) set to 1", testMonth);

return 1;


}

}\can use this method also to check month*/


private int checkDay(int testDay)


{

if(testDay>0 && testDay<=daysPerMonth[month])

return testDay;


if(month==2 && testDay==29 && (year % 400 ==0 ||(year % 4==0 && year % 100 !=0)))

return testDay;

System.out.printf("Ivalid day (%d) set to 1", testDay);

return 1;

}

public String toString()

{

return String.format("%d/%d/%d d:d:d", month, day, year, hour, minute, second);

}

}