Search code examples
javaclassmethodsincrementbluej

How to call an increment method() in HistoricalMoment class?


I'm stuck on this method since this morning and I hope that someone can help me with it.

I have the following:

public class HistoricalMoment{
    private String eventName;
    private ClockDisplay timeOfEvent;
    public static final int MIDNIGHT_HOUR = 00;
    public static final int MINUTE_ZERO = 00;
}

How can I create a method called public void addMinuteToTimeOfEvent() which calls the timeOfEvent's increment() method to add one minute to the timeOfEvent?

This is what I have:

public void addMinuteToTimeOfEvent(){
    timeOfEvent.increment();
}

But I get this error message when I hit the compile button on BlueJ that sates: "cannot find symbol - method increment()"

Thanks in advance for your help!

Here is my full code for the class:

public class HistoricalMoment{

    private String eventName;
    private ClockDisplay timeOfEvent;

    public static final int MIDNIGHT_HOUR = 00;
    public static final int MINUTE_ZERO = 00;

    public static final String EQUINOX = "March 2013 Equinox!";
    public static final String TITANIC = "Titanic hit an iceberg!";

    /**
     * Default Constructor
     */
    public HistoricalMoment(){
        eventName = "untitled event";
        timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
    }

    /**
     * @param nameOfTheEvent the name of the event; "untitled event" if the name of the event is null or ""
     */
    public HistoricalMoment(String nameOfTheEvent){
        if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
            eventName = "untitled event";
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
        else {
            eventName = nameOfTheEvent;
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
    }

    /**
     * @param nameOfTheEvent the name of the event;
     * @param ClockDisplay, the time of the event
     */
    public HistoricalMoment(String nameOfTheEvent, ClockDisplay theTime){
        if ((nameOfTheEvent == null) || (nameOfTheEvent.equals(""))){
            eventName = "untitled event";
            timeOfEvent = new ClockDisplay(MIDNIGHT_HOUR, MINUTE_ZERO);
        }
        else {
            eventName = nameOfTheEvent;
            timeOfEvent = theTime;
        }
    }

    public void addMinuteToTimeOfEvent(){
        timeOfEvent.increment();
    }

    /**
     * the print details of time and event
     */
    public void printDetails()
    {
        System.out.println("At" + getTime() + "," + eventName);
    }
}

This is what I have for the ClockDisplay class:

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;    // simulates the actual display

    public static final int FIRST_MORNING_HOUR  = 0;
    public static final int LAST_MORNING_HOUR   = 11;
    public static final int FIRST_EVENING_HOUR      = 12;
    public static final int LAST_EVENING_HOUR       = 23;
    public static final int MINUTES_PER_HOUR        = 60;
    public static final String MORNING_SUFFIX       = "a.m.";
    public static final String EVENING_SUFFIX       = "p.m.";
    public static final int MIDNIGHT_HOUR       = 0;
    public static final int HOURS_PER_DAY       = 0;
    private boolean isAM;

    /**
     * Constructor for ClockDisplay objects. This constructor 
     * creates a new clock set at 00:00.
     */
    public ClockDisplay()
    {
        hours = new NumberDisplay(12);
        minutes = new NumberDisplay(60);
        updateDisplay();
        setMorn();
    }

    /**
     * Constructor for ClockDisplay objects. This constructor
     * creates a new clock set at the time specified by the 
     * parameters.
     */
    public ClockDisplay(int hour, int minute)
    {
        hours = new NumberDisplay(12);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);
        setMorn();
    }

    /**
     * This method should get called once every minute - it makes
     * the clock display go one minute forward.
     */
    public void timeTick()
    {
        minutes.increment();
        if(minutes.getValue() == 0) {  // it just rolled over!
            hours.increment();
        }
        if (hours.getValue() == 12)
        {
            isAM = !isAM;
        }

        updateDisplay();
    }

    private void setMorn()
    {
        isAM = true;
    }

    private void setAft()
    {
        isAM = false;   
    }

    /**
     * Set the time of the display to the specified hour and
     * minute.
     */
    public void setTime(int hour, int minute)
    {   
        hours.setValue(hour);
        minutes.setValue(minute);
        updateDisplay();
    }

    /**
     * Return the current time of this display in the format HH:MM.
     */
    public String getTime()
    {
        return displayString;
    }

    /**
     * Update the internal string that represents the display.
     */
    private void updateDisplay()
    {
        int hour = hours.getValue();
        String daynight;
        if (isAM = true)
        {
            daynight = "AM (midnight)";
            if (hour == 0) 
            {
                hour = 12;   
            }
            else if(hour > 0 && hour < 12){
                daynight ="AM";
            }
            else
            {
                isAM = false;
                daynight = "PM (noon)";
                if (hour == 0) 
                {
                    hour = 12;   
                }
                else if(hour < 0 && hour > 12)
                    daynight ="PM";
            }
            displayString = hour + ":" + 
            minutes.getDisplayValue() + daynight;

        }
    }
}

Solution

  • timeOfEvent is an instance of your class ClockDisplay. You are calling the method increment(), which does not actually belong to ClockDisplay but rather to NumberDisplay. It looks to me like you'll want to call timeTick() instead, which calls increment() on minutes (an instance of NumberDisplay belonging to ClockDisplay).

    Try this instead.

    public void addMinuteToTimeOfEvent(){
        timeOfEvent.timeTick();
    }