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

I will need the coding to be broken into parts A, B so on when answered. Also if

ID: 3663816 • Letter: I

Question

I will need the coding to be broken into parts A, B so on when answered. Also if you can add commects so I can understand the coding thank you.

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

Program:

public class Day {
   public String day;
   public String[] Days={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
   Day()
   {
       day="Sun";
   }
   Day(String d)
   {
       day=d;
   }
   void setDay(String d)
   {
       day=d;
   }
   void printDay()
   {
       System.out.println("Current Day : "+day);
   }
   String Today()
   {
       return day;
   }
   String nextDay()
   {
       int i=0;
       for(i=0;i<7;i++)
       {
           if(Days[i].equals(day))
               break;
       }
       return Days[(i+1)%7];
   }
   String previousDay()
   {
       int i=0;
       for(i=0;i<7;i++)
       {
           if(Days[i].equals(day))
               break;
       }
       return Days[(i-1+7)%7];
   }
   String addInCurrent(int k)
   {
       int i=0;
       for(i=0;i<7;i++)
       {
           if(Days[i].equals(day))
               break;
       }
       return Days[(i+k)%7];
   }
   public static void main(String arg[])
   {
       Day obj1=new Day();
       Day obj2=new Day("Mon");
       System.out.println("Object 1");
       obj1.setDay("Wed");
       System.out.println("Current Day is "+obj1.Today());
       System.out.println("Next day of "+obj1.day+" is "+obj1.nextDay());
       System.out.println("Previous day of "+obj1.day+" is "+obj1.previousDay());
       System.out.println("3 day after the current day is "+obj1.addInCurrent(3));
       System.out.println(" Object 2 ");
       System.out.println("Current Day is "+obj2.Today());
       System.out.println("Next day of "+obj2.day+" is "+obj2.nextDay());
       System.out.println("Previous day of "+obj2.day+" is "+obj2.previousDay());
       System.out.println("5 day after the current day is "+obj2.addInCurrent(5));
      
   }
}

Result:

Object 1
Current Day is Wed
Next day of Wed is Thu
Previous day of Wed is Tue
3 day after the current day is Sat

Object 2
Current Day is Mon
Next day of Mon is Tue
Previous day of Mon is Sun
5 day after the current day is Sat