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

Design and implement the class Day that implements the day of the week in a prog

ID: 3760740 • 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. C. 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 four 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. G. Add the appropriate constructors. H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G. I. Write a program to test various operations on the class Day.

Explanation / Answer

import java.util.*;
public class Day
{
private static final int SUNDAY = 0;
private static final int MONDAY = 1;
private static final int TUESDAY = 2;
private static final int WEDNESDAY = 3;
private static final int THURSDAY = 4;
private static final int FRIDAY = 5;
private static final int SATURDAY = 6;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
Day dy = new Day(Day.WEDNESDAY);
int choice;
do
{
System.out.println("Welcome, please select an option. ");
menu2();
choice = keyboard.nextInt();
System.out.println();
switch (choice)
{
case 1:
do
{
menu1();
choice = keyboard.nextInt();
System.out.println();
switch (choice)
{
case 1:
day = SUNDAY;
break;
case 2:
day = MONDAY;
break;
case 3:
day = TUESDAY;
break;
case 4:
day = WEDNESDAY;
break;
case 5:
day = THURSDAY;
break;
case 6:
day = FRIDAY;
break;
case 7:
day = SATURDAY;
break;
case 99:
System.exit(0);
break;
default:
System.out.println("Invalid Input");
}
System.out.print("The day you selected as your starting day is: ");
dy.print();
System.out.println();
System.out.print("The next day to your selected day is: ");
dy.setDay(dy.getNextDay());
dy.print();
System.out.println();
System.out.print("The previous day to your selected day is: ");
dy.setDay(dy.getPreviousDay());
dy.setDay(dy.getPreviousDay());
dy.print();
System.out.println();
System.out.print("How many days would you like to add? ");
int days = keyboard.nextInt();
dy.setDay(days);
System.out.print(" Adding " + days + " day(s) makes your new day: ");
dy.print();
System.out.println();
}
while (choice != 99);
break;
case 2:
System.out.println("Test Data For Day Class");
System.out.print(" Initial day: ");
dy = new Day(Day.SUNDAY);
dy.print();
System.out.print(" Next day: ");
dy.setDay(dy.getNextDay());
dy.print();
System.out.print(" Add 12 Days: ");
dy.setDay(dy.addDays(12));
dy.print();
System.out.print(" Previous day: ");
dy.setDay(dy.getPreviousDay());
dy.print();
System.out.print(" Add 3 days: ");
dy.setDay(dy.addDays(3));
dy.print();
System.out.println(" ");
break;
case 99:
System.exit(0);
break;
default:
System.out.println("Invalid Input");
}
}
while (choice != 99);
}
public static void menu1()
{
System.out.println("Please Select Your Initial Day (or 99 to quit):");
System.out.println(" 1: Sunday.");
System.out.println(" 2: Monday.");
System.out.println(" 3: Tuesday.");
System.out.println(" 4: Wednesday.");
System.out.println(" 5: Thursday.");
System.out.println(" 6: Friday.");
System.out.println(" 7: Saturday.");
System.out.println("99: To quit the program.");
}
public static void menu2()
{
System.out.println("Enter: ");
System.out.println("1: To Enter Data into the program.");
System.out.println("2: For Test Data.");
System.out.println("99: To quit the program.");
}
public static int day;
public void setDay(int day)
{
this.day = day;
}
public void print()
{
System.out.println(this.toString());
}
public int getDay()
{
return day;
}
public int getNextDay()
{
return (day + 1) % 7;
}
public String toString()
{
switch (Day.day)
{
case SUNDAY:
return "Sunday";
case MONDAY:
return "Monday";
case TUESDAY:
return "Tuesday";
case WEDNESDAY:
return "Wednesday";
case THURSDAY:
return "Thursday";
case FRIDAY:
return "Friday";
case SATURDAY:
return "Saturday";
}
return "";
}
public int getPreviousDay()
{
if (day = 0)
return (day + 6) % 7;
else
return (day - 1) % 7;
}
public int addDays(int days)
{
return (day + days) % 7;
}
public Day()
{
this.day = SUNDAY;
}
public Day(int day)
{
this.day = day;
}
}

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