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 ordered 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 orderedCollection that allow us to maintain uniquely keyed objects. I have designed a Binary Search Tree class and a Hash Table class that implement Ordered 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 ordered collections adhere to the Keyed interface enable us to ensure uniqueness.
Once, you finish building these ordered 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!
cd public-html/classes/csc241
cp -r ~mohammad/public-html/classes/csc241/Collection/ .
-- don't forget the dot or the -r.
ls -R List
. The list should look like:
Collection/: Keyed.class hashNode.class orderedCollection.class Keyed.java hashNode.java orderedCollection.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
java test1 tree
and
java test1 hash 10
java test2 tree
and
java test2 hash 10
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.
We like to compare the performance of our two implementations of orderedCollection. 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.
What are the answers to the questions above when this version of Tree is used?