CSC 241- Lab 5 (Due May 7 1998)


Theme ...

There are three main objectives that this lab helps us meet:

In lab3, we learned about creating abstract classes and extending them in different ways and providing distinct implementations. We also learned about building generic stacks and queues that held any subclass of Object. In this lab, you will be introduced to the concept of interfaces that are pure design tools in Java. Interfaces only allow for identifying methods and constants, no implementation, thus, they are very similar to abstract classes. They do, however, provide further flexibility in design than a simple class hierarchy which I hope to show here.

I have developed an interface named Keyed that you will use. This interface can be implemented by all classes that we wish for their objects to be identified uniquely through a key value. This characteristic is important when dealing with keyed collections, we need to be able to distinguish and compare our objects that we keep in such a collection. Also, for us, it means that we won't have to just build a class for a binary search tree that holds integers, we will build one that holds any keyed object.

We will also work with an interface named keyedCollection that allow us to maintain uniquely keyed objects. I have designed a Binary Search Tree class and a Hash Table class that implement Keyed Collection. They both need their update methods implemented which you will do in this lab. I hope that you will understand why the concept of keyed is important in these Collections. Binary Search Trees and Hash Tables differ from stacks and queues in that they care about uniqueness of elements that they hold, in neither case, can we allow duplicates. Making sure that all objects held in keyed collections adhere to the Keyed interface enable us to ensure uniqueness.

Once, you finish building these keyed collections, we can compare their behavior for adding, removing, and finding values. You will also learn to do some performance tunning. There are many variations to hash tables, so, our results won't be complete, but, I think, you will learn something!


Create directories and copy some files

  1. cd public-html/classes/csc241
  2. cp -r ~mohammad/public-html/classes/csc241/Collection/ . -- don't forget the dot or the -r.
  3. From public-html/classes/csc241 directory, type ls -R List. The list should look like:
        Collection/:
        Keyed.class               hashNode.class            keyedCollection.class
        Keyed.java                hashNode.java             keyedCollection.java
        Tree.class                hashTable.class           testCollection
        Tree.java                 hashTable.java            treeNode.class
        duplicateException.class  notFoundException.class   treeNode.java
        duplicateException.java   notFoundException.java
    
        Collection/testCollection:
        test1.class      test2.class      testKeyed.class
        test1.java       test2.java       testKeyed.java
              

Compile all Java files and run the test program

  1. Use javac *.java in each directory to compile all of the above .java files.
  2. Run the program that tests the two implementation of keyedCollection.

Implement the update method

I have left the implementation of the update method in Tree and hashTable for you. In each case, you must use the key from the parameter x to locate the object in the collection and replace your finding with the new version. In the tree version, write update in a similar way as I have done remove and add by using find to look for the element being updated. If element is found in collection, using an internal method written similarly to insert and del locate and replace the element in the Tree.

Modify test2.java to test your update methods.


Experiment and Tune

We like to compare the performance of our two implementations of keyedCollection. To effectively compare performances for hash tables and binary search trees you need a bigger pool of data. Just inserting 100 or so elements and performing 100 or so operations on the collections will not give you a definitive conclusion on their performance. We also know that the tree version performs a find every time add, remove or update are called, this doubles the traversal time for any successful tree operations. Here are the things that you need to do and the questions that you must answer.