CS 2102 - Dterm 10

Homework 7 - Working with ArrayLists

Due: Friday, April 30 at 5pm

Acknowledgements

This assignment was designed by Prof. Viera Proulx, 2008. Modified by Glynis Hamel.

Assignment Goals

Assignment

Eliza is a historically-famous computer program where Eliza is a mock psychiatrist, carrying on a conversation with a patient. The patient (the user) asks a series of questions, and the computer-psychiatrist answers them. Questions start with one of six keywords: why, who, how, where, when, and what. Associated with each keyword is a list of possible answers to the question. When the patient types in a question, the program randomly chooses one of the possible answers associated with the question. Here is a sample "session" with the psychiatrist Eliza:

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

Problems

  1. Start with the file Interactions.java. Create an Eclipse project and run the program (notice that the main() method is in the Interactions class). This starter version of the Eliza program simply displays a prompt, waits for you to type something in, and echoes back what you typed in.

    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.

  2. Design the class 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.

  3. Design the method 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().

  4. Design the class Eliza that contains an ArrayList of Replies - one for each of the question keywords.

  5. In the class 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.

  6. In the class 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."

  7. Modify the Interactions class so that it initializes all the data and plays the game.

What to Turn In

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.