Lab 7: Writing generic and inner classes

The goal of this lab is to practice writing generic classes.

1. Introduction

This lab will tie together several Java features that we have talked about recently, either directly or incidentally. You will write (at least) two generic classes; you will implement the Comparable interface and the Iterator interface. You will also have the opportunity to write an anonymous inner class and another nested class. The main part of the program will be something familiar--implementing yet another linked list-- this time, a generic linked list. This list will sort items as they are being added (in other words, it will maintain a sorted property for the list).

2. Setup

Make a directory for this lab. Then copy the following files from the course public directory:

cp /cslab.all/ubuntu/cs245/lab7/List.java .
cp /cslab.all/ubuntu/cs245/lab7/ListDriver.java .
cp /cslab.all/ubuntu/cs245/lab7/aeneid . 

3. Specification

The ListDriver program (once all of its parts are uncommented) reads in a file and creates three lists-- a list of all the Strings of the file, a list of Integers which contains the lengths of all the Strings in the file, and a list of a certain class StringWrapper, which you will have to write. For each String in the file, it will enter the String, its size, and a wrapper based on the String into the respective lists. Finally, it prints the contents of each list. Since the lists should sort their items as they are entered, the output should be, first, the Strings alphabetized; then the sizes, in increasing number; then the Strings from shortest to longest.

(I've included a sample text file to use for testing, aeneid, but you may use another file if you wish.)

Thus, the StringWrapper class must implement the Comparable interface, and it must compares Strings based on their length.

4. The SortList class

Write a class SortList. It needs to implement the List interface I've given you, but in addition, its type parameter must implement the Comparable type. Implement the list so that when a new item is added, it is put in the order specified by compareTo(). See the Comparable interface in the Java API for reference.

You will also have to write a Node class (also generic), which could be a nested class.

The method iterator() will require some more work. It needs to return an object which implements the Iterator interface and iterates over the contents of the list. Consult the Iterator interface in the Java API for help on this. You will need to write another class to implement Iterator, which you will return from the iterator() method. This class may be an anonymous inner class.

Then compile and test ListDriver.

5. Sorting Integers

When you are confident that what you did in the previous section works right, uncomment the part that uses the List class as an integer list. It should just work, without any modifications.

6. The StringWrapper class

Now write a class StringWrapper. It must

Then uncomment the parts in the driver that use StringWrapper and test.

7. To turn in

In a typescript file, cat all the files you wrote. Then run the program to demonstrate that it works. Print the file.


Thomas VanDrunen
Last modified: Mon Mar 2 08:43:17 CST 2009 ˆ