Question
Design and implement the class Day_yourname that implements the day of the week in a program. It should be your own implementation. The class Day_yourname
Design and implement the class Day_yourname that implements the day of the week in a program. It should be your own implementation.
The class Day_yourname should store the day(eg: Sunday,Monday...etc). 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 a test/driver program to test various operations on the class Day. Make sure you test all your methods .
This is my code so far but it does not work:
import java.util.*;
import java.time.temporal.*;
public class Day_Awebb {
String dayString;
int dayInt;
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;
private int day;
Scanner input= new Scanner(System.in);
public static void main(String[] args)
{
Day_Awebb sunday = new Day_Awebb("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("Four days from now will be "+sunday.addDays(4));
Day_Awebb tuesday = new Day_Awebb(2);
System.out.println("Now the day is "+tuesday);
}
public Day_Awebb(String day) {
dayString = day;
dayInt = dayToInt(day);
}
public Day_Awebb(int day) {
dayInt = day;
dayString = SUNDAY.get(day);
}
public String toString() {
return dayString;
}
public void setDay(String day) {
dayString = day;
dayInt = dayToInt(day);
}
public void setDay_Awebb(int day) {
this.day = day;
}
public String getDay_Awebb() {
return dayString;
}
public Day_Awebb getNextDay() {
return new Day_Awebb((dayInt+1) % 7);
}
public Day_Awebb getPreviousDay() {
int prevDay = (dayInt - 1) % 7;
if (prevDay < 0) prevDay += 7;
return new Day_Awebb(prevDay);
}
public Day_Awebb addDays(int daysToAdd) {
return new Day_Awebb((dayInt+daysToAdd) % 7);
}
private int dayToInt(String day) {
return DAYS.indexOf(day.toUpperCase());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started