Variable Scope


Instance and Local Variables

 
/** Class documentation goes here.
 */
public class TalkingRobot
{
  // State: instance variables go here.
    private Clock clock;        // used by getTime and sayAPhrase
    private Random generator;   // used by sayAPhrase and respondTo

  // Constructors

    // constructor not shown here...

  // Methods

    /** Gets the time from the robot's clock.
     *      @return   the time in HH:MM AM (or HH:MM PM) format
     */
    public String getTime()
    {
        // Use clock's getHour method to know whether AM or PM
        //    (getHour returns the hour between 0 and 23).
        String amPM;
        if ( this.clock.getHour() < 12 )
            amPM = " AM";
        else
            amPM = " PM";

        // Use clock's getHH and getMM methods to get the
        //    hour and minutes in two-digit String format.
        return this.clock.getHH() + ":" + this.clock.getMM() + amPM;
    }

    // other methods not shown here...

}

Alyce Brady, Kalamazoo College