©2006 Felleisen, Proulx, et. al.

11  Java API, Exceptions, and Collections


  1. Familiarize yourself with the Java Application Programmers Interface (API)

  2. Learn the basics of working with the Java Collections Framework

  3. 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:

11.1  Activity: Reading JavaDocs

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.

Tips For Quickly Finding Class Specifications

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.

The Anatomy of a JavaDoc

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 ArrayList extends and implements. In this case, ArrayList extends from the classes Object, AbstractCollection, AbstractList, and implements the interfaces Cloneable, Collection, List, RandomAccess, Serializable.

Next is a general description of the class. In this case, the JavaDoc says that ArrayList is a "Resizable-array implementation of the List interface."

Following this is a summary of fields, constructors, and methods provided by 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.

11.2  Activity: Working with the ArrayList

The class Words contains some Strings and ArrayLists of Strings that we will use. Our first task is to reverse the order of the words in the ArrayList reversed. Design a method in the Interactions class that reverses the order of the words in the ArrayList.

Do the following three tasks - modifying the previous solution as you go on (or keeping the previous one and adding a new variant:

Finally, print all words, one to a line, traversing the ArrayList using the for-each loop that uses the Java Iterator.

11.3  Activity: Working with the StringTokenizer

The text in the ArrayList words in the class Words is encoded. It represents verses from a poem - if you read only the first words.

11.4  Activity: Working with HashMap; Catching Exceptions, Throwing Exceptions

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.

Last modified: Tuesday, November 14th, 2006 10:08:34am