Design and implement the class Day that implements the day of the week in a prog
ID: 3757244 • Letter: D
Question
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day: a) Set the day. b) Print the day. e) Return the day. d) Return the next day. e) Return the previous day. f Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.Explanation / Answer
public class Day
{
final static int SUN = 0;
final static int MON = 1;
final static int TUE = 2;
final static int WED = 3;
final static int THU = 4;
final static int FRI = 5;
final static int SAT = 6;
public static int ourDay;
public void setDay()
{this.ourDay = ourDay;}
public void DayType()
{this.ourDay = ourDay;}
public int nextDay()
{
if (ourDay == SAT)
{return SUN;}
else
{ourDay = (ourDay + 1) % 7;}
return ourDay;
}
public int previousDay()
{
if (ourDay == SUN)
{return SAT;}
else
{ourDay = (ourDay - 1) % 7;}
return ourDay;
}
public int futureDay(int ourDays)
{return (ourDay + ourDays) % 7;}
public String toString()
{
switch (this.ourDay)
{
case SUN:
return "Sunday";
case MON:
return "Monday";
case TUE:
return "Tuesday";
case WED:
return "Wednesday";
case THU:
return "Thursday";
case FRI:
return "Friday";
case SAT:
return "Saturday";
}
return "";
}
public static void main(String[] args)
{
DayType outDay = new DayType(SUN);
System.out.print("The current day: " + outDay);
System.out.println();
outDay.setDay(outDay.previousDay());
System.out.print("The previous day: " + outDay);
System.out.println();
outDay.setDay(outDay.nextDay());
outDay.setDay(outDay.nextDay());
System.out.print("The next day: " + outDay);
System.out.println();
outDay.setDay(outDay.futureDays(10));
System.out.print("10 days later: " + outDay);
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.