fosterbl

Magpie2.java

Nov 29th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 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.      * Get a default greeting  
  16.      * @return a greeting
  17.      */
  18.     public String getGreeting(){
  19.         return "Hello, let's talk.";
  20.     }
  21.    
  22.     /**
  23.      * Gives a response to a user statement
  24.      *
  25.      * @param statement
  26.      *            the user statement
  27.      * @return a response based on the rules given
  28.      */
  29.     public String getResponse(String statement){
  30.         String response = "";
  31.         if (statement.indexOf("no") >= 0){
  32.             response = "Why so negative?";
  33.         }
  34.         else if (statement.indexOf("mother") >= 0
  35.                 || statement.indexOf("father") >= 0
  36.                 || statement.indexOf("sister") >= 0
  37.                 || statement.indexOf("brother") >= 0){
  38.             response = "Tell me more about your family.";
  39.         }
  40.         else{
  41.             response = getRandomResponse();
  42.         }
  43.         return response;
  44.     }
  45.  
  46.     /**
  47.      * Pick a default response to use if nothing else fits.
  48.      * @return a non-committal string
  49.      */
  50.     private String getRandomResponse(){
  51.         final int NUMBER_OF_RESPONSES = 4;
  52.         double r = Math.random();
  53.         int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  54.         String response = "";
  55.        
  56.         if (whichResponse == 0){
  57.             response = "Interesting, tell me more.";
  58.         }
  59.         else if (whichResponse == 1){
  60.             response = "Hmmm.";
  61.         }
  62.         else if (whichResponse == 2){
  63.             response = "Do you really think so?";
  64.         }
  65.         else if (whichResponse == 3){
  66.             response = "You don't say.";
  67.         }
  68.  
  69.         return response;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment