6.5. Activity 2: Running Simplified Magpie Code

The activity asks you to enter the following as input using the Scanner class and record the responses. But, instead you can run this simplified version below and just call the getResponse method which each string as input as shown in the main method below. You can print the result.

You can also step through this simplified code here. Use the forward button to execute the next statement.

As you can see the getResponse method of Magpie2 looks for certain keywords like "mother" and "brother". Why do you think the response to “Do you know my brother?” isn’t “Tell me more about your family.”? See if you can modify the code above to respond correctly.

The response to “The weather is nice.” is one of the random responses. Modify the code above to add other random responses.

Look at the code. See how the if statement assigns a value to the response and returns that response. The method getRandomResponse generates a random number and uses that to assign the response.

6.6. Exercises

Alter the code above to do the following.

lab-1b-1: What happens when a keyword is included in another word? Consider statements like “I know all the state capitals” and “I like vegetables smothered in cheese.” Explain the problem with the responses to these statements.

6.7. Activity 2: Actual Code - (Optional)

Here is the actual code for MagpieRunner2.java. It uses the Scanner class to read input from the user. The Scanner class is not on the AP CS A exam.

If you want to run the actual code go to MagpieActivityStarterCode/activity2/ on your computer and open and compile MagpieRunner2.java and Magpie2.java in an Integrated Development Environment (IDE) like DrJava or JGrasp. Then run the main method in MagpieRunner2.

You can do all of Activity 2 with the actual code instead if you prefer.

import java.util.Scanner;

/**
 * A simple class to run the Magpie class.
 * @author Laurie White
 * @version April 2012
 */
public class MagpieRunner2
{

   /**
        * Create a Magpie, give it user input, and print its replies.
        */
       public static void main(String[] args)
       {
          Magpie2 maggie = new Magpie2();

              System.out.println (maggie.getGreeting());
              Scanner in = new Scanner (System.in);
              String statement = in.nextLine();

              while (!statement.equals("Bye"))
              {
                      System.out.println (maggie.getResponse(statement));
                      statement = in.nextLine();
              }
       }
}

Here is the code for Magpie2.java.

public class Magpie2
{
      /**
   * Get a default greeting
       * @return a greeting
       */
      public String getGreeting()
      {
              return "Hello, let's talk.";
      }

      /**
       * Gives a response to a user statement
       *
       * @param statement
       *            the user statement
       * @return a response based on the rules given
       */
      public String getResponse(String statement)
      {
              String response = "";
              if (statement.indexOf("no") >= 0)
              {
                      response = "Why so negative?";
              }
              else if (statement.indexOf("mother") >= 0
                              || statement.indexOf("father") >= 0
                              || statement.indexOf("sister") >= 0
                              || statement.indexOf("brother") >= 0)
              {
                      response = "Tell me more about your family.";
              }
              else
              {
                      response = getRandomResponse();
              }
              return response;
      }

      /**
       * Pick a default response to use if nothing else fits.
       * @return a non-committal string
       */
      private String getRandomResponse()
      {
              final int NUMBER_OF_RESPONSES = 4;
              double r = Math.random();
              int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
              String response = "";

              if (whichResponse == 0)
              {
                      response = "Interesting, tell me more.";
              }
              else if (whichResponse == 1)
              {
                      response = "Hmmm.";
              }
              else if (whichResponse == 2)
              {
                      response = "Do you really think so?";
              }
              else if (whichResponse == 3)
              {
                      response = "You don't say.";
              }

              return response;
       }
 }
Next Section - 6.8. Activity 3: Better Keyword Detection