Как вызвать метод приращения () в классе HistoricalMoment?

Я застрял на этом методе с сегодняшнего утра, и я надеюсь, что кто-то может мне помочь с ним.

У меня есть следующее:

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

Как я могу создать метод с именем public void addMinuteToTimeOfEvent(), который вызывает метод increment() timeOfEvent, чтобы добавить одну минуту к timeOfEvent?

Вот что у меня есть:

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

Но я получаю это сообщение об ошибке, когда нажимаю кнопку компиляции в BlueJ, которая гласит: «не удается найти символ - метод приращения ()»

Заранее спасибо за помощь!

Вот мой полный код для класса:

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

Это то, что у меня есть для класса ClockDisplay:

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;

        }
    }
}

person Tre    schedule 04.11.2015    source источник


Ответы (1)


timeOfEvent является экземпляром вашего класса ClockDisplay. Вы вызываете метод increment(), который на самом деле принадлежит не ClockDisplay, а скорее NumberDisplay. Мне кажется, что вместо этого вы захотите вызвать timeTick(), который вызывает increment() на minutes (экземпляр NumberDisplay, принадлежащий ClockDisplay).

Попробуйте это вместо этого.

public void addMinuteToTimeOfEvent(){
    timeOfEvent.timeTick();
}
person nathantspencer    schedule 04.11.2015
comment
спасибо, ваше предложение исправило сообщение об ошибке, которое я получил, ценю это. - person Tre; 05.11.2015