Created
October 2, 2018 00:49
-
-
Save memish/949b768178754b76ae10fe122d721459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A program to carry on conversations with a human user. | |
* This is the initial version that: | |
* <ul><li> | |
* Uses indexOf to find strings | |
* </li><li> | |
* Handles responding to simple words and phrases | |
* </li></ul> | |
* This version uses a nested if to handle default responses. | |
* @author Laurie White | |
* @version April 2012 | |
*/ | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment