©2008 Felleisen, Proulx, et. al.

10  Javadocs, Testing Exception Use Mutating ArrayList, Loops

Goals

The first part of the lab you will learn how to generate Javadoc documentation, how to test whether the program throws a correct exception with a correct message, and practice reading Javadoc style documentation for programs.

The second part introduces ArrayList class from the Java Collections Framework library, lets you practice designing methods that mutate ArrayList objects. We will continue to use the generics (type parameters), but will do so by example, rather than through explanation of the specific details.

In the third part of the lab you will learn how to how to convert recursive loops to imperative (mutating) loops using either the Java while statement of the Java for statements to implement the imperative loops.

10.1  Documentation, Testing exceptions, Java Libraries

For this lab download the files in Lab10-Sp2008.zip. The folder contains the following files:

Create a new Project Lab10 and import into it all files from the zip file. Import the tester.jar and colors.jar.

Generating Documentation

Defining and Handling Exceptions

ArrayList and Java Libraries

10.2  Using ArrayLists and Traversals

Using ArrayList with Mutation

In this part of the lab we will work on lists of balloons.

Open the web site that shows the documentation for Java libraries

http://java.sun.com/j2se/1.5.0/docs/api/.

Find the documentation for ArrayList.

Here are some of the methods defined in the class ArrayList:

// how many items are in the collection
int size();

// add the given object of the type E at the end of this collection
// false if no space is available
boolean add(E obj);

// return the object of the type E at the given index
E get(int index);

// replace the object of the type E at the given index 
// with the given element
// produce the element that was at the given index before this change
E set(int index, E obj);

Other methods of this class are isEmpty (checks whether we have added any elements to the ArrayList), contains (checks if a given element exists in the ArrayList — using the equals method), set (mutate the element of the list at a specific position). Notice that, in order to use an ArrayList, we have to add

import java.util.ArrayList;

at the beginning of our class file.

The methods you design here should be added to the Examples class, together with all the necessary tests.

10.3  Converting Recursive Loops into Imperative while Loops

Last modified: Tuesday, March 18th, 2008 7:07:13am