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: 654695 • Letter: D

Question

Design and implement the class Day that implements the day of the week in a program. Please use comments!!! 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

Required solution:

/**
*
* @author raghunath
*
*/
//class Day implements the week in program
public class Day
{
   //instance variable that stores the name of day in a week
   String day;
   //constructor that initializes the day
   Day(String day){
       this.day=day;
   }
   String[] days={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
   //method that set the value of String day
   public void setDay(String day) {
       this.day = day;
   }
  
   //method that prints the day String
   public void printDay(){
       System.out.println(day);
   }
  
   //method that returns the day
   public String returnDay(){
       return day;
   }
  
   //method that returns next day
   public String nextDay(){
       int i=getDayNum();
       int j=(i==6)?0:i+1;//if day Sat give Sun else give nextday
       return days[j];
   }
   public String previousDay(){
       int i=getDayNum();
       int j=(i==0)?6:i-1;//if day Sun give sat else give prevday
       return days[j];
   }
   //method to calculate the after numdays
   public String calculatedDay(int numdays){
       int i=getDayNum();
       numdays=numdays%7;
       int j=(i==6)?0:i+numdays;
       return days[j];
   }
   public int getDayNum(){
       int i;
       for(i=0;i<days.length;i++){
           if(day.equalsIgnoreCase(days[i]))
               break;
       }
       return i;
   }
   //Test method to test the class Day
   public static void main(String[] args)
   {
       Day day=new Day("SUN");
       System.out.print("Today is:");
       day.printDay();
       System.out.print("Previous Day is:");
       System.out.println(day.previousDay());
      
       System.out.print("Next Day is:");
       System.out.println(day.nextDay());
      
       System.out.print("10th Day from now is:");
       System.out.println(day.calculatedDay(10));
   }//end of main method
}//end of class Day

sample OutPut

Today is:SUN
Previous Day is:Sat
Next Day is:Mon
10th Day from now is:Wed

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