Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 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. String trimStatement = statement.trim();
  35. if (statement.indexOf("no") >= 0)
  36. {
  37. response = "Why so negative?";
  38. }
  39. else if (statement.indexOf("mother") >= 0
  40. || statement.indexOf("father") >= 0
  41. || statement.indexOf("sister") >= 0
  42. || statement.indexOf("brother") >= 0)
  43. {
  44. response = "Tell me more about your family.";
  45. }
  46. else if (statement.indexOf("Dog") >= 0
  47. || statement.indexOf("dog") >= 0
  48. || statement.indexOf("Cat") >= 0
  49. || statement.indexOf("cat") >= 0)
  50. {
  51. response = "Tell me more about your pets.";
  52. }
  53. else if (statement.indexOf("Mr.") >= 0
  54. || statement.indexOf("mr.") >= 0)
  55. {
  56. response = "He sounds like a good teacher.";
  57. }
  58. else if (trimStatement.length() == 0)
  59. {
  60. response = "Please say something";
  61. }
  62. else
  63. {
  64. response = getRandomResponse();
  65. }
  66. return response;
  67. }
  68.  
  69. /**
  70. * Pick a default response to use if nothing else fits.
  71. * @return a non-committal string
  72. */
  73. private String getRandomResponse()
  74. {
  75. final int NUMBER_OF_RESPONSES = 4;
  76. double r = Math.random();
  77. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  78. String response = "";
  79.  
  80. if (whichResponse == 0)
  81. {
  82. response = "Interesting, tell me more.";
  83. }
  84. else if (whichResponse == 1)
  85. {
  86. response = "Hmmm.";
  87. }
  88. else if (whichResponse == 2)
  89. {
  90. response = "Do you really think so?";
  91. }
  92. else if (whichResponse == 3)
  93. {
  94. response = "You don't say.";
  95. }
  96.  
  97. return response;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement