This book is now obsolete Please use CSAwesome instead.

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-2: 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
       }
 }
You have attempted of activities on this page