如何在HistoricalMoment类中调用increment方法()?

时间:2015-11-04 18:50:42

标签: java class methods 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 void addMinuteToTimeOfEvent()的方法,该方法调用timeOfEvent的increment()方法向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;

        }
    }
}

1 个答案:

答案 0 :(得分:2)

timeOfEvent是您的班级ClockDisplay的一个实例。您正在调用方法increment(),该方法实际上不属于ClockDisplay,而是属于NumberDisplay。在我看来,您希望拨打timeTick()来调用increment() minutes NumberDisplay ClockDisplay属于public void addMinuteToTimeOfEvent(){ timeOfEvent.timeTick(); } 的实例

试试这个。

viewDidLoad