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);
    }



}

There are a bunch of different ways to accomplish that. Below is one of the easier ways. Just overload the getDay method and pass it a number of days in advance to get.

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 a number of days from the day
    public String getDay(int numOfDays)
    {
    	Day d = new Day(getDay());
    	
    	for(int i=0; i<numOfDays; i++)
    	{
    		d.setDay(d.getNextDay());
    	}
    	
    	return d.getDay();
    }

    //Method to return next day.
    public String getNextDay()
    {    	    	
    	if (day.equals("Sun"))
    	       return "Mon";
	else if (day.equals("Mon"))
		return "Tue";
	else if (day.equals("Tue"))
		return "Wed";
	else if (day.equals("Wed"))
		return "Thu";
	else if (day.equals("Thu"))
		return "Fri";
	else if (day.equals("Fri"))
		return "Sat";
	else
		return "Sun";
    }
    
    //Method to return the previous day.
    public String getPreviousDay()
    {
        if (day.equals("Sun"))
		return "Sat";
	else if (day.equals("Mon"))
		return "Sun";
	else if (day.equals("Tue"))
		return "Mon";
	else if (day.equals("Wed"))
		return "Tue";
	else if (day.equals("Thu"))
		return "Wed";
	else if (day.equals("Fri"))
		return "Thu";
	else
		return "Fri";
    }

    //Method to print the day
    public void printDay()
    {
        System.out.println(day);
    }
    
    public static void main(String[] args)
    {
    	Day d = new Day("Mon");
    	System.out.println(d.getDay(3)); // gets 3rd day from day d
    	
    }
}

Well to calculate days I would prefer a more simpler method like this :-

// Current day would store the index of the day in the Array
// 0 points to Sunday.

int currentDay = 0 
String days = {"Sun", "Mon", "Tue", "Wed", "Thr",  "Fri", "Sat" };

// Now if you want the day 10 days 
// after current day just do

String afterNextTenDays = days[(currentDay + 10) % 7];

String nextDay =  days[(currentDay + 1) % 7];

Similarly you can go on for all your combinations.
Also it is not exactly foolproof, you will need to take into account the special case when the currentDay is Sunday (and so set to 0) and the User asks for the previous day. In that case the formula in the square bracket would become [(7-(currentDay - noOfDays)%7)%7], this formula would be needed whenever you want to calculate a previous day. Looks kind off complicated but I guess there should be an easier way.

Thanks so much for the input. When I use h3xc0de's solution I get an error.

66: getNextDay(java.lang.String) in assignment5_3.Day cannot be applied to ()
d.setDay(d.getNextDay());

Any ideas? I would rather not use the array solution since I do not fully understand arrays yet.

Thanks so much for the input. When I use h3xc0de's solution I get an error.

66: getNextDay(java.lang.String) in assignment5_3.Day cannot be applied to ()
d.setDay(d.getNextDay());

Any ideas? I would rather not use the array solution since I do not fully understand arrays yet.

I don't know what line 66 is, but it looks like it may be different from what h3xc0de's line 66 was? You may want to repost your updated code and highlight the line that gave you the error.

I like stephen84s's solution myself. I think I would change h3xc0de's solution so that it did not use the private class variable called day , but rather passed a String to the methods getNextDay and getPreviousDay. You could then use those variables with variables other than day to make it more versatile, and perhaps make it a static method.

I suspect that the error you are getting is that you are combining his code with your function specification. His code expects no parameter when you call getNextDay, but yours does, so if you used his function call with your function specification, or vice versa, you'll get an error.

Sorry, here is the code. Line 60 is line with the error code.

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 a number of days from the day
    public String getDay(int numOfDays)
    {
        Day d = new Day(getDay());

        for(int i=0; i<numOfDays; i++)
        {
           d.setDay(d.getNextDay());
        }
    
    return d.getDay();
    }

    //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);
    }



}

Sorry, I should have looked more closely at your code. It compiled and ran when I had the parameter in getNextDay, what is the benefit of taking that out? Was I wrong to have it in there or is it personal preference. I am not sure when parameters are needed (obviously).

It depends on how you want to design your code. So more of a personal preference

This question has already been answered. Start a new discussion instead.