I'm Eliza. Ask me a question. >Who are you? The Grinch who stole Christmas. >Who? Santa Claus. >What can you tell me about myself? I don't know the answer to that one. >Why not? I don't know. Let me think about it. >Where do we go from here? Anywhere. >Who do you think you are, anyway? Barack Obama. >I give up. I'm outta here. I don't understand your question. Ask me something else. > Goodbye
Note the try
and catch
blocks in the program.
In previous assignments we have written methods that throw exceptions.
The preferred way to handle
exceptions in a Java program follows this pattern:
try{ // some code containing a throw statement or a // method activation that contains a throw statement } catch(Exception-Class-Name catch-block-parameter){ // some code that will be executed if the named exception // is thrown in the try-block }If the code in the try-block throws the exception named in the catch-block, program control is passed to the catch-block; otherwise, the code in the catch-block is not executed. There may be multiple catch-blocks for a given try-block, each catch-block providing the statements to be executed for a given type of Exception. In a well-written program, every exception that is thrown should be caught by a catch-block in some method. If a thrown exception is not caught, the program terminates with an error message giving the name of the Exception class.
The catch-block-parameter provides a way for the thrown exception to
pass a message to the catch-block The message can then be accessed with
the
getMessage()
method (a method defined for every Exception
class).
In the Interactions
class, the nextLine()
method
of the
Scanner
class could throw a NoSuchElementException
(look at the JavaDocs for nextLine()),
so a
catch-block for that exception is provided.
Replies
. This class has two fields,
a String that represents a keyword and an ArrayList of possible answers to
a
question that starts with the keyword. When you create instances of the
Replies
class, create at least three answers for each
keyword.
randomAnswer
for the class
Replies
that produces one of the possible answers for
this Replies
' keyword. Make
sure the method works even if you add
new answers to your database later. Look at the JavaDocs for the
class Random
(import java.util.Random into your program).
The Random
method nextInt()
will be useful when
you implement
randomAnswer()
.
Eliza
that contains an ArrayList
of Replies
- one for each of the question keywords.
Eliza
design the helper method
firstWord()
that consumes a String that represents the
patient's question, and produces the first word in the String (the goal
is to determine the first word of the patient's question). Look at the
JavaDocs for the String class (the methods trim
,
toLowerCase
, startsWith
, and charAt
may be helpful). Your program should work if the user types all uppercase
letters, all lowercase letters, a mixture, etc. So, for example, if the
patient's question is any of the following:
Why do you think so? Why, when I ask, don't you answer? WHY? why am I so shy?the method should return "why" as the first word. When handling punctuation that comes immediately after the keyword, your program should be able to handle the chars '?' and ',', as indicated in the examples above.
Eliza
design the method
findAnswer()
that consumes a String representing the patient's question and
returns a (random) answer. If the first word of the patient's
question doesn't match any of the keywords, the method returns the
String "I don't understand your question. Ask me something else."
Interactions
class so that it
initializes all the data and plays the game.
Create an archive of your Eclipse project. Using web-based turnin, turn in a single zip file containing all code and documentation for this assignment. Follow the naming conventions when naming your file.