©2006 Felleisen, Proulx, et. al.
Familiarize yourself with the Java Application Programmers Interface (API)
Learn the basics of working with the Java Collections Framework
Use exceptions to detect and handle errors
Download the provided zip file and unzip it. Create a new Eclipse project named Lab11. Add the given code to the project and link the external JAR jpt.jar to the project. Also add the SimpleTestHarness.jar. You should have the following Java files:
Examples that is to be used for tests that are
not a part of the program that interacts with the user
Interactions that controls our user interactions
- you will add a couple of methods here
Reply - a skeleton, you have to add the functionality
SampleEliza the database of answers and some of
the methods dealing with the answers to the patient.
Go to the Java API at html:<a href = "http://java.sun.com/j2se/1.5.0/docs/api/"> http://java.sun.com/j2se/1.5.0/docs/api/ html:</a>. Bookmark this page! When coding you will often use classes that are provided for you by Java. The Java API describes these classes and lists all of the fields and methods of these classes that are available to you.
The front page of the Java API lists all of the packages provided by Java. A package is a collection of related interfaces and classes.
The left frame of the API page lists all classes alphabetically. If
you want the specifications for a specific class you can click in this
frame and use your web browsers search function to find that
class. For example, find the
ArrayList class. Another way to quickly
find Java API specifications is to search Google for "java api class
x", where x is the name of the class you're searching for. For
example, the search "java api class arraylist" returns the
specifications for class
ArrayList as the first result.
All of the specifications are in a JavaDoc format. JavaDocs are automatically generated from source code based on specifically formated comments that the programmer adds for each class and each method. We won't cover the format of such comments here, but it's pretty easy to do yourself. For more details, see html:<a href = "http://java.sun.com/j2se/javadoc/writingdoccomments/"> How to Write Doc Comments for the Javadochtml:</a>.
Lets use the
ArrayList JavaDoc as an example.
The top of the JavaDoc lists the other classes that
and implements. In this case,
ArrayList extends from the classes
and implements the
Next is a general description of the class. In this case, the JavaDoc
ArrayList is a "Resizable-array implementation of
the List interface."
Following this is a summary of fields, constructors, and methods
ArrayList. In general, classes will provide very
few public fields and the JavaDoc will contain mostly specifications
of methods. Look over some of the methods provided by
ArrayList. We will be using similar classes when cover the
Java Collections Framework in activity 3.
The method summaries provide headers (return type, name, and arguments) and a short description of the method's functionality. More detailed descriptions are linked from these summaries and appear farther down on the same page.
Words contains some
Strings that we will use. Our first
task is to reverse the order of the words in the
reversed. Design a method in the
that reverses the order of the words in the
Do the following three tasks - modifying the previous solution as you go on (or keeping the previous one and adding a new variant:
First just produce another
with the words reversed.
Next think of how you would reverse the elements in an
ArrayList without using another
ArrayList at all.
Finally, think of how you can modify the actual values of the
reversed in the class
Finally, print all words, one to a line, traversing the
ArrayList using the for-each loop that uses the Java
The text in the
words in the class
Words is encoded. It represents verses from a poem - if you
read only the first words.
Look up the
StringTokenizer class in JavaDocs. The methods
there allow you to traverse over a
String and produce one
word at a time delimited by the selected characters. Read the
examples. Then write the method
makeWords that consumes one
String and produces an
ArrayList of words.
Design the method
firstWord that produces the first
word from a given String.
If you have not done so already, modify it, so it would recognize additional delimeters besides the default ones. Specifically, you want to recognize comma, semicolon, and the question mark.
Our goal now is to train our computer to be a mock psychiatrist, carrying on a conversation with a patient. The patient (the user) asks a series of questions. The computer-psychiatrist replies to each question as follows. If the question starts with one of the following (key)words: Why, Who, How, Where, When, and What, the computer selects one of the three (or more) possible answers appropriate for that question. If the first word is none of these words the computer replies 'I do not know' or something like that.
Start by designing the class
Reply that holds an
ArrayList of answers to a particular question, and contains
randomAnswer that produces one of the possible answers
each time it is invoked. Make sure it works fine even if you add new
answers to your database later.
Next look at the class
SampleEliza. This is our
collection of answers. We are giving you just a few - you can add
more as you wish. The field
replies is defined as
HashMap. Look up the JavaDocs for
questions, if you do not understand.
HashMap contains pairs of data
usually called the Key-Value pair. It uses the Key
to save and look up the Values. Each Key may be in
HashMap only once. To find a Value in the
HashMap one must use the Key.
Our goal is to select the right instance of the
class each time the patient asks a question. The method
get(K key) produces the correct
Reply, if it
consumes one of the keywords. However, it produces
there is no value corresponding to the given key.
findReply that is provided consumes a
String and produces a reply. Here the reply just parrots
the question: You asked .... Modify the method so that it
first extracts the first word of the question, then uses it as a key
to identify the possible set of replies from the
replies and finally produces the answer. Of course, we already have
helper method for each of these steps.
We now have to make sure the program does not crash when the
user asks a wrong question. Add try-catch clause to the method to
NullPointerException and reply 'I do not know' to
an answer it cannot identify. An example of how to catch an
exception is given in the method
readAndWrite in the
Interactions class. Use also as the model for your final game
For practice, design a new class
that extends the
Exception class. Use the instructions for
Lab 9 Part 3 as the guide.
You can now play the game, using the playEliza method in the