Advertisement
StefanTobler

TERM 2 MAGPIE2

Jan 19th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. /**
  2.  * A program to carry on conversations with a human user.
  3.  * This is the initial version that:  
  4.  * <ul><li>
  5.  *       Uses indexOf to find strings
  6.  * </li><li>
  7.  *       Handles responding to simple words and phrases
  8.  * </li></ul>
  9.  * This version uses a nested if to handle default responses.
  10.  * @author Laurie White
  11.  * @version April 2012
  12.  */
  13. public class Magpie2
  14. {
  15.  /**
  16.   * Get a default greeting  
  17.   * @return a greeting
  18.   */
  19.  public String getGreeting()
  20.  {
  21.   return "Hello, let's talk.";
  22.  }
  23.  
  24.  /**
  25.   * Gives a response to a user statement
  26.   *
  27.   * @param statement
  28.   *            the user statement
  29.   * @return a response based on the rules given
  30.   */
  31.  public String getResponse(String statement)
  32.  {
  33.   String response = "";
  34.   if (statement.indexOf("no") >= 0)
  35.   {
  36.    response = "Why so negative?";
  37.   }
  38.   else if (statement.indexOf("mother") >= 0
  39.     || statement.indexOf("father") >= 0
  40.     || statement.indexOf("sister") >= 0
  41.     || statement.indexOf("brother") >= 0)
  42.   {
  43.    response = "Tell me more about your family.";
  44.   }
  45.   // Change what is in the red then delete this line.
  46.   else if (statement.indexOf("dog") >= 0
  47.     || statement.indexOf("cat") >= 0
  48.     || statement.indexOf("hampster") >= 0
  49.     || statement.indexOf("ferret") >= 0)
  50.   {
  51.    response = "Tell me more about your pets.";
  52.   }
  53.   // Just the stuff between these two comments. Delete
  54.   // Change what is in the red then delete this line.
  55.   else if (statement.indexOf("Davis") >= 0
  56.     || statement.indexOf("Horton") >= 0)
  57.   {
  58.    response = "What teachers are those?";
  59.   }
  60.   // Just the stuff between these two comments. Delete
  61.   // Change what is in the red then delete this line.
  62.   else if (statement.trim().length() == 0)
  63.   {
  64.    response = "Only quite?";
  65.   }
  66.   // Just the stuff between these two comments. Delete
  67.   // Change what is in the red then delete this line.
  68.   else if (statement.indexOf("sailing") >= 0
  69.           || statement.indexOf("sail") >= 0
  70.           || statement.indexOf("boat") >= 0)
  71.   {
  72.    response = "Do you like sailing too?";
  73.   }
  74.   // Just the stuff between these two comments. Delete
  75.   // Change what is in the red then delete this line.
  76.   else if (statement.indexOf("sailing") >= 0
  77.           || statement.indexOf("sail") >= 0
  78.           || statement.indexOf("boat") >= 0)
  79.   {
  80.    response = "Do you like sailing too?";
  81.   }
  82.   // Just the stuff between these two comments. Delete
  83.   else
  84.   {
  85.    response = getRandomResponse();
  86.   }
  87.   return response;
  88.  }
  89.  
  90.  /**
  91.   * Pick a default response to use if nothing else fits.
  92.   * @return a non-committal string
  93.   */
  94.  private String getRandomResponse()
  95.  {
  96.   final int NUMBER_OF_RESPONSES = 4;
  97.   double r = Math.random();
  98.   int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  99.   String response = "";
  100.  
  101.   if (whichResponse == 0)
  102.   {
  103.    response = "Interesting, tell me more.";
  104.   }
  105.   else if (whichResponse == 1)
  106.   {
  107.    response = "Hmmm.";
  108.   }
  109.   else if (whichResponse == 2)
  110.   {
  111.    response = "Do you really think so?";
  112.   }
  113.   else if (whichResponse == 3)
  114.   {
  115.    response = "You don't say.";
  116.   }
  117.  
  118.   return response;
  119.  }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement