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 WEEN IN A PROG

ID: 3626759 • Letter: D

Question

DESIGN AND IMPLEMENT THE CLASS DAY THAT IMPLEMENTS THE DAY OF THE WEEN IN A PROGRAM. THE CLASS DAY SHOULD STORE THE DAY,SUCH AS sun FOR sunday. tHE PROGRAM SHOULD beable 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.retun the previous day
f.calculate and return the day by adding certain days to the current day.
g. add appropriate constructors.
write the definitions of the methonds to implement the opration for the class day,as defined thru g

Explanation / Answer

import java.util.*;
public class Day {

    private final List<String> DAYS = Arrays.asList(new String[] {"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"});
       
    String dayString;
    int dayInt;
   
    public Day(String day) {
        dayString = day;
        dayInt = dayToInt(day);
    }
   
    public Day(int day) {
        dayInt = day;
        dayString = DAYS.get(day);
    }
   
    public String toString() {
        return dayString;
    }
   
    public void setDay(String day) {
        dayString = day;
        dayInt = dayToInt(day);
    }
   
    public void setDay(int day) {
        dayInt = day;
        dayString = DAYS.get(day);
    }
   
    public String getDay() {
        return dayString;
    }
   
    public Day getNextDay() {
        return new Day((dayInt+1) % 7);
    }
   
    public Day getPreviousDay() {
        int prevDay = (dayInt - 1) % 7;
        if (prevDay < 0) prevDay += 7;
        return new Day(prevDay);
    }
   
    public Day addDays(int daysToAdd) {
        return new Day((dayInt+daysToAdd) % 7);
    }
       
    private int dayToInt(String day) {
        return DAYS.indexOf(day.toUpperCase());
    }
   
    public static void main(String[] args) {
        Day sunday = new Day("Sunday");
        System.out.println("Today is "+sunday);
        System.out.println("Yesterday was "+sunday.getPreviousDay());
        System.out.println("Tomorrow will be "+sunday.getNextDay());
        System.out.println("Nine days from now will be "+sunday.addDays(9));
        Day tuesday = new Day(2);
        System.out.println("Now the day is "+tuesday);
       
    }

}