Variable Scope


Instance Variables, Local Variables, and Parameters




















 
/** 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

    // getTime and sayAPhrase methods not shown here...

    /** Responds to a user question with a random response.
     *      @param question  the user question
     *      @return the response
     */
    public String respondTo(String question)
    {
        // Print a lunchtime-related random phrase
        //    or a more general random phrase.
        int randNum = this.generator.nextInt(3);
        if ( question.contains("lunch") )
        {
            if ( randNum == 0 )
                return "Lunch... Not hungry yet.";
            else if ( randNum == 1 )
                return "Is it lunchtime already?";
            else
                return "I always like pizza.";
        }
        else
        {
            if ( randNum == 0 )
                return "No idea!";
            else if ( randNum == 1 )
                return "Your guess is as good as mine.";
            else
                return "What do you think?";

        }

    }

}

Alyce Brady, Kalamazoo College