I was assigned a problem where I have to create a class called Day. The program is to store the day such as Sun for Sunday, Mon for Monday, etc. The program then preform the following steps:
- Set the Day
- Print the Day
- Return the Day
- Return the Next Day
- Return the Previous Day
- 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.
I have wrote some code for the Day class and the Daytester class but it isn't working very well and I can't figure out how I'm going to implement the part where it adds an integer and returns the Day.
I'm just not very good with creating my own classes just yet.
Here is the Day class that I have written so far:
public class Day { private int Day; private String Sun,Mon,Tues,Wed,Thurs,Fri,Sat; public Day() { Sun = ""; Mon = ""; Tues = ""; Wed = ""; Thurs = ""; Fri = ""; Sat = ""; } public Day(int Day) { if(Day == 1) Sun = "Sunday"; else if(Day == 2) Mon = "Monday"; else if(Day == 3) Tues = "Tuesday"; else if(Day == 4) Wed = "Wednesday"; else if(Day == 5) Thurs = "Thursday"; else if(Day == 6) Fri = "Friday"; else if(Day == 7) Sat = "Saturday"; } public void setDay(int Day) { this.Day = Day; } public int getDay() { return this.Day; } public void printDay(int Day) { if(Day == 1) System.out.println("Today is " + Sun); else if(Day == 2) System.out.println("Today is " + Mon); else if(Day == 3) System.out.println("Today is " + Tues); else if(Day == 4) System.out.println("Today is " + Wed); else if(Day == 5) System.out.println("Today is " + Thurs); else if(Day == 6) System.out.println("Today is " + Fri); else if(Day == 7) System.out.println("Today is " + Sat); } public void printYesterday(int Day) { if(Day == 1) System.out.println("Yesterday was " + Sat); else if(Day == 2) System.out.println("Yesterday was " + Sun); else if(Day == 3) System.out.println("Yesterday was " + Mon); else if(Day == 4) System.out.println("Yesterday was " + Tues); else if(Day == 5) System.out.println("Yesterday was " + Wed); else if(Day == 6) System.out.println("Yesterday was " + Thurs); else if(Day == 7) System.out.println("Yesterday was " + Fri); } public void printTomorrow(int Day) { if(Day == 1) System.out.println("Tomorrow is " + Mon); else if(Day == 2) System.out.println("Tomorrow is " + Tues); else if(Day == 3) System.out.println("Tomorrow is " + Wed); else if(Day == 4) System.out.println("Tomorrow is " + Thurs); else if(Day == 5) System.out.println("Tomorrow is " + Fri); else if(Day == 6) System.out.println("Tomorrow is " + Sat); else if(Day == 7) System.out.println("Tomorrow is " + Sun); } }
And here is the Daytester so far:
I appreciate any help guys!import java.util.*; public class DayTester { public static void main(String[] args) { Scanner input = new Scanner(System.in); int Day,x; Day Dayweek; System.out.println("Enter a number 1-7 corresponding to the day of the week, for example, Sunday is 1, Monday is 2, .. Saturday is 7"); Day = input.nextInt(); Dayweek = new Day(Day); Dayweek.setDay(Day); System.out.println(Dayweek); } }