I have to create a class called Day that will carry out certain functions. One of the things that it needs to do is it needs to be able to 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 the day is Monday and we add ten days the day to be returned will be Thursday.
I have all of the other code finished and I am thinking I can use the getNextDay method to accomplish this, but I need a push in the right direction. Any ideas? Below is the code that I have so far.
(Keep in mind we have not covered arrays yet and I do not understand them.)
public class Day
{
private String day;
//Default Constructor
public Day()
{
setDay("Sun");
}
//constructor with a parameter
public Day(String myDay)
{
setDay(myDay);
}
//Method to set the day according to the parameter myDay.
//This also checks to make sure that the correct abbreviation is used
//and if it is not the day is set to the default day, which is "Sun".
public void setDay(String myDay)
{
if (myDay.equals("Sun"))
day = myDay;
else
if (myDay.equals("Mon"))
day = myDay;
else
if (myDay.equals("Tue"))
day = myDay;
else
if (myDay.equals("Wed"))
day = myDay;
else
if (myDay.equals("Thu"))
day = myDay;
else
if (myDay.equals("Fri"))
day = myDay;
else
if (myDay.equals("Sat"))
day = myDay;
else
day = "Sun";
}
//Method to return the day.
public String getDay()
{
return day;
}
//Method to return next day.
public String getNextDay(String myDay)
{
if (myDay.equals("Sun"))
return "Mon";
else
if (myDay.equals("Mon"))
return "Tue";
else
if (myDay.equals("Tue"))
return "Wed";
else
if (myDay.equals("Wed"))
return "Thu";
else
if (myDay.equals("Thu"))
return "Fri";
else
if (myDay.equals("Fri"))
return "Sat";
else
return "Sun";
}
//Method to return the previous day.
public String getPreviousDay(String myDay)
{
if (myDay.equals("Sun"))
return "Sat";
else
if (myDay.equals("Mon"))
return "Sun";
else
if (myDay.equals("Tue"))
return "Mon";
else
if (myDay.equals("Wed"))
return "Tue";
else
if (myDay.equals("Thu"))
return "Wed";
else
if (myDay.equals("Fri"))
return "Thu";
else
return "Fri";
}
//Method to print the day
public void printDay()
{
System.out.print(day);
}
}