Eric Miller
Eric Miller

Reputation: 1

Project to add and return days

I have a final project that is posted below. I have the program to where it compiles correctly, but I'm not getting the correct results. No matter what day I input for the user it returns Monday as the next day and Sunday as the previous day. For the section where it adds days to the current day it seems like it has the day set and stuck at Sunday so adding 8 returns Monday, adding 6 returns Saturday, etc. This is regardless of what the user input day is.

After asking what's wrong to my instructor I got the following reply: "The problem I am seeing is that addDays returns an int that is not being captured. You add to the days, and return a day, but do nothing with it." I'm not sure where to go from here, please help!

HERE IS THE PROJECT:

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. 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 the definitions of the methods to implement the operations for the class Day, as defined in A through G.

I. Write a program to test various operations on the class Day. I have my entire code posted below:

HERE IS MY CODE:

//**************************************************************************
//  Author: Eric Miller
//  IT-145 Module Eight, Final Project
//  February 28, 2015
//
//  This program will provide a variety of functions on a day of the week
//  entered by a user.  Among these will be the function to return the day,
//  return the next day, the previous day, and also calculate future days by
//  adding a number to the current day.
//**************************************************************************

import java.util.*;

public class Day 

{
    //Day Constants
   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;

    //Stores the day of the week.
   private int day;

    //Sets the day.
   public void setDay(int day) 
   {
        this.day = day;
    }

    //Prints the day.
    public void print() 
   {
        System.out.println(this.toString());
    }

   //Converts int to string
   public String toString() 
   {
        switch (this.day) 
      {
        case SUNDAY:
            return "Sunday";
        case MONDAY:
            return "Monday";
        case TUESDAY:
            return "Tuesday";
        case WEDNESDAY:
            return "Wednesday";
        case THURSDAY:
            return "Thursday";
        case FRIDAY:
            return "Friday";
        case SATURDAY:
            return "Saturday";
        }
        return "";
    }

    //Returns the day.
    public int getDay() 
   {
        return day;
    }

    //Returns the next day.
    public int getNextDay() 
   {
        return (day + 1) % 7;
    }

    //Returns the previous day.
    public int getPrevDay() 
   {
        return (day - 1) % 7;
    }

    //Calculates future day by adding a number of days to current day.
    public int addDays(int days) 
   {
        return (day + days) % 7;
    }

    //Constructors.
    public Day() 
   {
        this.day = day;
    }

    public Day(int day) 
   {
        this.day = day;
    }

    //Main method. 

   static Scanner console = new Scanner(System.in);

   public static void main(String[] args) 

{
      Day d = new Day();

        System.out.println("Enter a day of the week: ");
        String day = console.nextLine();
        System.out.println("You entered: " + day);

        System.out.print("The day after the day you entered is: ");
      d.setDay(d.getNextDay());
      System.out.println(d);

        System.out.print("The day before the day you entered is: ");
        d.setDay(d.getPrevDay());
      System.out.println(d);

        System.out.print("Enter a number of days to add to the current day: ");
        int days = console.nextInt();
      d.setDay(d.addDays(days));
      System.out.print("The day you entered plus the added days is: ");
        System.out.println(d);

    }
}

Upvotes: 0

Views: 425

Answers (2)

owenrb
owenrb

Reputation: 535

I think the string "day" variable is unused and did not change the state of you Day (d) instance.

String day = console.nextLine();
System.out.println("You entered: " + day);

You may need to add the following code if you wish to apply the change.

d.setDay(Integer.parseInt(day));

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

You never seed Day with the value entered by the user, so it always defaults to SUNDAY (0)

    System.out.println("Enter a day of the week: ");
    String day = console.nextLine();
    System.out.println("You entered: " + day);
    d.setDay(Integer.parseInt(day));

This assumes that the user is meant to enter 0-6

Now, this will print the next day, but it won't print the previous day (it will print the day the user entered), because you reset the day to the next day's value...

d.setDay(d.getNextDay());

Better to just print the result, but you don't have a method to do that, so instead...

System.out.println(new Day(d.getNextDay());

and

System.out.println(new Day(d.getPrevDay());

Upvotes: 1

Related Questions