Jump to content

Java - Multiple Classes and Method Calling

tomoki

Okay I've posted this in another thread and I fixed the error that I was looking for initially but it didn't turn out to do what I wanted it to do. The objected created newDayDisplay (int theDayNumber) I think works completely fine but there's probably something wrong with the .getValue() method that I did not write.... unless I'm using it wrong. I'm not supposed to be touching the .getValue() method in the other class and only be working on this one.  In the code below, I've eliminated some of the stuff that I don't think is necessary to solve the issue. 

 

getDayOfTheWeek() returns Sunday even though I put a 5 into test it. It should be returning Friday... hence getWhatDayIsTomorrow() is also buggy. 

 

 


/**
 * This is a class for Day Display.
 * @author: tomoki
 * @version: 1.0
 */
class DayDisplay{
    private NumberDisplay dayNumber;

    public static final int NUMBER_OF_DAYS_IN_WEEK = 7;
    public static final int SUNDAY                 = 0;
    public static final int MONDAY                 = 1;
    public static final int TUESDAY                = 2;
    public static final int WEDNESDAY              = 3;
    public static final int THURSDAY               = 4;
    public static final int FRIDAY                 = 5;
    public static final int SATURDAY               = 6;

    public static final String SUNDAY_STRING        = "Sunday";
    public static final String MONDAY_STRING        = "Monday";
    public static final String TUESDAY_STRING       = "Tuesday";
    public static final String WEDNESDAY_STRING     = "Wednesday";
    public static final String THURSDAY_STRING      = "Thursday";
    public static final String FRIDAY_STRING        = "Friday";
    public static final String SATURDAY_STRING      = "Saturday";

    /**
     * This is a no arg constructor. 
     */
    public DayDisplay(){}

    /**
     * This is the first constructor that expects the integer between 0 to 6 representing the day of the week to be stored
     * in dayNumber 
     */
    public DayDisplay(int theDayNumber){
        if(theDayNumber >= 0 && theDayNumber <= 6){
            dayNumber = new NumberDisplay(theDayNumber);
        }else{
            dayNumber = new NumberDisplay(0);
        }
    }

    /**
     * This second constructor takes a String parameter for the day of the week
     */
    public DayDisplay(String nameOfTheDay){
        if(nameOfTheDay != null){
            if(nameOfTheDay.equalsIgnoreCase(SUNDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(SUNDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(MONDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(MONDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(TUESDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(TUESDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(WEDNESDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(WEDNESDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(THURSDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(THURSDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(FRIDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(FRIDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(SATURDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(SATURDAY);
            }else if(nameOfTheDay.equalsIgnoreCase(SUNDAY_STRING)){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                dayNumber.setValue(SUNDAY);
            }else{
                return;
            }
        }
    }

    /**
     * @return the day of the week when dayNumber has an int value.
     */
    public String getDayOfTheWeek(){
        if(dayNumber.getValue() == SUNDAY){
            return SUNDAY_STRING;
        }else if(dayNumber.getValue() == MONDAY){
            return MONDAY_STRING;
        }else if(dayNumber.getValue() == TUESDAY){
            return TUESDAY_STRING;
        }else if(dayNumber.getValue() == WEDNESDAY){
            return WEDNESDAY_STRING;
        }else if(dayNumber.getValue() == THURSDAY){
            return THURSDAY_STRING;
        }else if(dayNumber.getValue() == FRIDAY){
            return FRIDAY_STRING;
        }else if(dayNumber.getValue() == SATURDAY){
            return SATURDAY_STRING;
        }else{
            return "Invalid input for day number.";
        }
    }

    /**
     * @return a String corresponding to the next day. 
     */
    public String getWhatDayIsTomorrow(){
        if(dayNumber.getValue() == SUNDAY){
            return MONDAY_STRING;
        }else if(dayNumber.getValue() == MONDAY){
            return TUESDAY_STRING;
        }else if(dayNumber.getValue() == TUESDAY){
            return WEDNESDAY_STRING;
        }else if(dayNumber.getValue() == WEDNESDAY){
            return THURSDAY_STRING;
        }else if(dayNumber.getValue() == THURSDAY){
            return FRIDAY_STRING;
        }else if(dayNumber.getValue() == FRIDAY){
            return SATURDAY_STRING;
        }else if(dayNumber.getValue() == SATURDAY){
            return SUNDAY_STRING;
        }else{
            return "Invalid";
        }
    }

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, elpiop said:

can you post the getValue method?

Yup! 


/**
 * The NumberDisplay class represents a digital number display that can hold
 * values from zero to a given limit. The limit can be specified when creating
 * the display. The values range from zero (inclusive) to limit-1. If used,
 * for example, for the seconds on a digital clock, the limit would be 60, 
 * resulting in display values from 0 to 59. When incremented, the display 
 * automatically rolls over to zero when reaching the limit.
 * 
 * @author Michael Kölling and David J. Barnes
 * @version 2011.07.31
 */
public class NumberDisplay
{
    private int limit;
    private int value;

    /**
     * Constructor for objects of class NumberDisplay.
     * Set the limit at which the display rolls over.
     */
    public NumberDisplay(int rollOverLimit)
    {
        limit = rollOverLimit;
        value = 0;
    }

    /**
     * Return the current value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Return the display value (that is, the current value as a two-digit
     * String. If the value is less than ten, it will be padded with a leading
     * zero).
     */
    public String getDisplayValue()
    {
        if(value < 10) {
            return "0" + value;
        }
        else {
            return "" + value;
        }
    }

    /**
     * Set the value of the display to the new specified value. If the new
     * value is less than zero or over the limit, do nothing.
     */
    public void setValue(int replacementValue)
    {
        if((replacementValue >= 0) && (replacementValue < limit)) {
            value = replacementValue;
        }
    }

    /**
     * Increment the display value by one, rolling over to zero if the
     * limit is reached.
     */
    public void increment()
    {
        value = (value + 1) % limit;
    }
}

It simply returns the value. I didn't make this code. I'm just supposed to call from this class. 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, elpiop said:

the NumberDisplay method takes an integer rollOverLimit as a parameter. The way you have written your DayDisplay(int theDayNumber) is incorrect because of this.  If you change NumberDisplay(int rollOverLimit) to something like this: 


public NumberDisplay(int val){
value = val;
}

it will also work. The rollOverLimit doesn't seem to be important for what you're doing, but if it is then you need to fix your DayDisplay method instead. 

Oh hmm.. 

My assignment told me to do it like that so I did haha 

"

The first constructor will expect an integer representing the day of the week to store in dayNumber (e.g. 5): public DayDisplay(int theDayNumber). Choose a descriptive name for the parameter but remember it cannot be exactly the same as the field name. Use the parameter to initialize the dayNumber instance variable, but only if is an integer between 0 and 6; otherwise set the day to 0. NOTE: consider carefully how you can store the parameter (which is an int!) into dayNumber (which is a NumberDisplay!)... try something like 
        dayNumber = new NumberDisplay(theDayNumber)
Note: this calls the NumberDisplay constructor and constructs the object. 
 

"

Link to comment
Share on other sites

Link to post
Share on other sites

public DayDisplay(int theDayNumber){
        if(theDayNumber >= 0 && theDayNumber <= 6){
            dayNumber = new NumberDisplay(theDayNumber);
        }else{
            dayNumber = new NumberDisplay(0);
        }
    }

This does not set the value of the day to whatever you input as theDayNumber. It creates a new NumberDisplay with that input, which gets set to the limit variable in the NumberDisplay class. So, you need another line to set dayNumber to the value that it is supposed to be. In which case what I said above is not necessary. You actually do not need to change the NumberDisplay method, sorry. 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, elpiop said:

public DayDisplay(int theDayNumber){
        if(theDayNumber >= 0 && theDayNumber <= 6){
            dayNumber = new NumberDisplay(theDayNumber);
        }else{
            dayNumber = new NumberDisplay(0);
        }
    }

This does not set the value of the day to whatever you input as theDayNumber. It creates a new NumberDisplay with that input, which gets set to the limit variable in the NumberDisplay class. So, you need another line to set dayNumber to the value that it is supposed to be. 

Oh ... hmm.. I didn't think that would've been the case hahaha thank you. Hitting one wall after another. 

dayNumber = theDayNumber; // to replace dayNumber = new NumberDisplay(theDayNumber);

else

return 0;

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, tomoki said:

Oh ... hmm.. I didn't think that would've been the case hahaha thank you. Hitting one wall after another. 

dayNumber = theDayNumber; // to replace dayNumber = new NumberDisplay(theDayNumber);

else

return 0;

That won't work, since dayNumber(NumberDisplay) is not the same type as theDayNumber(Integer). You do have a method to set the value of a NumberDisplay, so you should call that on dayNumber instead. 

Link to comment
Share on other sites

Link to post
Share on other sites

Using enumerations would make the code a lot more manageable

 

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×