The goal of this lab is to practice writing generic classes.
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).
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 .
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.
SortList
classWrite 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
.
Integer
sWhen 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.
StringWrapper
class Now write a class StringWrapper
.
It must
Comparable
interface
compareTo()
based on String length
toString()
by returning its contained
String.
Then uncomment the parts in the driver that use
StringWrapper
and test.
In a typescript file, cat all the files you wrote. Then run the program to demonstrate that it works. Print the file.