Advertisement
cgorrillaha

Magpie2

Oct 14th, 2021
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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.         else
  46.         {
  47.             response = getRandomResponse();
  48.         }
  49.         return response;
  50.     }
  51.  
  52.     /**
  53.      * Pick a default response to use if nothing else fits.
  54.      * @return a non-committal string
  55.      */
  56.     private String getRandomResponse()
  57.     {
  58.         final int NUMBER_OF_RESPONSES = 4;
  59.         double r = Math.random();
  60.         int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  61.         String response = "";
  62.        
  63.         if (whichResponse == 0)
  64.         {
  65.             response = "Interesting, tell me more.";
  66.         }
  67.         else if (whichResponse == 1)
  68.         {
  69.             response = "Hmmm.";
  70.         }
  71.         else if (whichResponse == 2)
  72.         {
  73.             response = "Do you really think so?";
  74.         }
  75.         else if (whichResponse == 3)
  76.         {
  77.             response = "You don't say.";
  78.         }
  79.  
  80.         return response;
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement