This is the original College Board Magpie lab from 2014. There is a new Magpie 2.0 lab as of 2025 which is available in the AP Classroom. It is very similar to the following lab, but must be done within AP Classroom. You can use Runestone active codes below or in a Runestone assignment as your IDE to do Magpie 2.0 coding activities as described in the AP Classroom.
The Magpie lab creates a simple chatbot and provides practice with String methods and conditionals. A chatbot is a computer program that tries to hold a conversation with a user. The exercises below will walk you through the activities in the original Magpie chatbot lab.
Work with another student or group to have two chatbots chat with each other. Type the responses from one chatbot into the input area for the other and vice-versa.
Keywords: Some chatbots look for particular keywords and respond based on those keywords. What are some of the keywords that your chatbot seems to be responding to? Why do you think it responds to those keywords?
The College Board activity asks you to enter input using the Scanner class and record the responses. But, instead you can run this simplified version below and just call the getResponse method with each string as input as shown in the main method below.
In this lab, the main method creates a Magpie object called maggie, and calls its methods maggie.getGreeting() and maggie.getResponse(input) where the input can be one of the following strings as input and a response is printed out.
When different methods are called from the main method, the control flows to these methods and then comes back to main exactly where it was left when the methods finish. Click on the cool Java visualizer Chatbot below to step through the code. Click on the Forward button at the bottom of the code to step through the code to see the flow of control from the main method to the other methods and back.
You can also run a version of the Magpie lab on this JuiceMind IDE or on replit.com that uses the Scanner class for input so that you can type in your own input to interact with it.
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.β? Discuss this with partner in pairs and see if you can figure it out.
The response to βThe weather is nice.β is one of the random responses. Look at the code to 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. Modify the code above to add other random responses.
Have it respond βTell me more about your petsβ when the statement contains the word βdogβ or βcatβ. For example, a possible statement and response would be:
Have it respond favorably when it sees the name of your teacher. Be sure to use appropriate pronouns! For example, a possible statement and response would be:
Have the code check that the statement has at least one character. You can do this by using the trim method to remove spaces from the beginning and end, and then checking the length of the trimmed string. If there are no characters, the response should tell the user to enter something. For example, a possible statement and response would be:
What happens when more than one keyword appears in a string? Try the input My **mother** has a **dog** but **no** cat. Which response did you get β was it the one about family or the one about pets or the negative one for no? Change the order of your if-else-if statements to make it so that one of the other responses is selected and try running it again.
What happens when a keyword is included in another word? Consider statements like βI know all the state capitalsβ which contains no and βI like vegetables smothered in cheeseβ which contains mother. Explain the problem with the responses to these statements.
Here is the actual code for the Magpie lab on replit.com. It uses the Scanner class to read input from the user. The Scanner class is not on the AP CSA exam. You can log in to replit.com and use this code and change it to do this lab.
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();
}
}
}
This activity introduces you to some new String methods including some that are not on the exam, but are useful. Your teacher will tell you whether your class is doing this activity or not.
Run the StringExplorer below. It currently has code to illustrate the use of the indexOf and toLowerCase methods. Do they do what you thought they would? The method indexOf is on the exam and the method toLowerCase is not. Why do you think you might want to change the string to all lowercase characters? Why doesnβt the value of sample change?
Run the code below. Why do you think you might want to change the string to all lowercase characters? Why doesnβt the value of sample change? Do string methods change the string? Try some other string methods.
Open the API for String in Java documentation| in another tab. Scroll down to the Method Summary section and find the indexOf(String str) method. Follow the link and read the description of the indexOf method.
int notFoundPsn = sample.indexOf("slow");
System.out.println("sample.indexOf(\"slow\") = " + notFoundPsn);
Read the description of indexOf(String str, int fromIndex). Add lines to StringExplorer that illustrate how this version of indexOf differs from the one with one parameter.
In activity 2, you discovered that simply searching for collections of letters in a string does not always work as intended. For example, the word βcatβ is in the string βLetβs play catch!β, but the string has nothing to do with the animal. In this activity, you will trace a method that searches for a full word in the string. It will check the substring before and after the string to ensure that the keyword is actually found.
Take a look at the findKeyword method below. It has a while loop in it which we havenβt seen before. A while loop repeats the code in the block below it while a condition is true. A block is all the code inside of an open curly brace { and a close curly brace }.
private int findKeyword(String statement, String goal,
int startPos)
{
String phrase = statement.trim();
// The only change to incorporate the startPos is in
// the line below
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(),
startPos);
// Refinement--make sure the goal isn't part of a word
while (psn >= 0)
{
// Find the string of length 1 before and after
// the word
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring(psn - 1, psn).toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
after = phrase.substring(
psn + goal.length(),
psn + goal.length() + 1)
.toLowerCase();
}
/* determine the values of psn, before, and after at this point */
// If before and after aren't letters, we've
// found the word
if (((before.compareTo("a") < 0) ||
(before.compareTo("z") > 0)) // before is not a letter
&& ((after.compareTo("a") < 0) ||
(after.compareTo("z") > 0)))
{
return psn;
}
// The last position didn't work, so let's find
// the next, if there is one.
psn = phrase.indexOf(goal.toLowerCase(),psn + 1);
}
return -1;
}
Run the code below or this JuiceMind IDE or this replit.com version 3 to see this new method findKeyword in action. It is called from the getResponse method to print out an appropriate response based on a keyword. For example, looking for the word "no" to print out "Why so negative?", but it wonβt match no inside of another word like "another".
if (findKeyword(statement, "no") >= 0)
{
response = "Why so negative?";
}
You can also step through the code in the Java visualizer or using the CodeLens button below. It may take a minute or two to load. Click the forward button at the bottom of the code to execute the next statement.
Modify the code below to print the values of psn, before, and after right after the comment on line 100 in the findKeyword method below. Record each of the values in a table. The College Board student guide for the Magpie Chatbot Lab has a table on page 8 that can be printed. Use the CodeLens button to step through the code.
Repeat the changes you made to the program in Activity 2, using this new method to detect keywords. You can use the active code window above, or the JuiceMind IDE or the replit.com version 3 or your own IDE.
Subsection2.29.3.4Questions: Prepare for the next activity
Single keywords are interesting, but better chatbots look for groups of words. Consider statements like βI like cats,β βI like math class,β and βI like Spain.β All of these have the form βI like something.β The response could be βWhat do you like about something?β The next activity will expand on these groups. You will get to add one of your own, so itβs a good idea to start paying close attention to common phrases in your own conversations.
As stated previously, single keywords are interesting, but better chatbots look for groups of words. Statements like βI like catsβ, βI like math classβ, and βI like Spainβ all have the form βI like somethingβ. The response could be βWhat do you like about something?β This activity will respond to groupings of words.
Try each of the following as the value for the statement in the main method and see what they print. Or you can try it on JuiceMind IDE or on replit.com version 4.
You can also step through the code in the Java visualizer. It may take a minute or two to load. Click the forward button at the bottom of the code to execute the next statement.
In this activity, the chatbot is altered to look not only for keywords, but also specific phrases. Magpie4.java adds two new methods, transformIWantToStatement and transformYouMeStatement and getResponse has been modified to add tests to find βI want to somethingβ statements and βYou something meβ statements.
Then add two new methods, transformIWantStatement and transformIYouStatement, and calls to each as described below. Alter the code either above in the active code window or on JuiceMind IDE or on replit.com version 4 or in an IDE of your choice:
In a method transformIWantStatement, have it respond to βI want somethingβ statements with βWould you really be happy if you had something?β You can use the already written transformIWantToStatement method as a guide. In doing this, you need to be careful about where you place the call to the method so it calls the right one. Test with the following:
In a method transformIYouStatement, have it respond to statements of the form βI something youβ with the restructuring βWhy do you something me?β. You can use the transformYouMeStatement method as a guide. Test with the following:
The following program segment should print 4 random responses using if/else statements, but the blocks have been mixed up. Drag the blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.