In this exercise, you’ll make some changes to Magpie.java to improve how it understands…
- User
In this exercise, you’ll make some changes to Magpie.java to improve how it understands and responds.
Exercises
Teach Magpie to ask about your pets. Any time a statement contains “dog,” “cat,” or “pet,” have Magpie ask a followup question, like “What do your pets eat?”
If you say something about your teacher, Magpie should say something nice about them.
Make the code check that you give it any input. You can do this by using the trim method to remove spaces from the beginning and end of the input, then checking the length. If there are no characters in the input, you should have Magpie say something like “Speak up,” or “I can’t hear you.”
Add two more non-committal responses to the randomResponse method.
MagpieRunner.java import java.util.Scanner;
public class MagpieRunner { public static void main(String[] args) { Magpie magpie = new Magpie(); System.out.println(magpie.greeting()); Scanner scanner = new Scanner(System.in); String statement = scanner.nextLine();
while (!statement.equals("Bye")) { System.out.println(magpie.getResponse(statement)); statement = scanner.nextLine(); } scanner.close(); }}
Magpie.java public class Magpie { /** * Gets a default greeting. * @return String */ public String greeting() { return "Hey, what's up?"; }
/** * Resturns a response to a user statement * * @param statement * @return String */ public String getResponse(String statement) { String response = ""; if (statement.indexOf("no") >= 0) { response = "Don't be so negative!"; } else if ( statement.indexOf("mother") >= 0 || statement.indexOf("brother") >= 0 || statement.indexOf("sister") >= 0 || statement.indexOf("father") >= 0 ) { response = "Tell me more about your family!"; } else if ( statement.indexOf("weather") >= 0 || statement.indexOf("sun") >= 0 || statement.indexOf("rain") >= 0 ) { response = "The weather here is really nice."; } else { response = randomResponse(); } return response; } /** * Pick a default response to use if nothing else fits. * @return String */ private String randomResponse() { int NUMBER_OF_RESPONSES = 6; double responseIndex = Math.random(); int whichResponse = (int)(responseIndex * NUMBER_OF_RESPONSES); String response = ""; if (whichResponse == 0) { response = "Very cool!"; } else if (whichResponse == 1) { response = "Tell me more about that."; } else if (whichResponse == 2) { response = "That's really interesting!"; } else if (whichResponse == 3) { response = "Can we talk about something else?"; } else if (whichResponse == 4) { response = "Booooring."; } else if (whichResponse == 5) { response = "You really like to talk, don't you?"; } return response; }}
- Assistant
- User
- Assistant
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.