logo

CMSC 132

Sections 030X/040X/050X
Assignment:     Project #6
Due Date:     Monday 04/13, 11:00PM
Open/Closed policy:     CLOSED

Hash Table


Introduction

For this project you will create a class called MyHashSet, which is a slightly simplified version of Java's HashSet. As you might guess, you will use a hash table to store the elements of the set. The "buckets" in the table will be implemented as linked lists.

The picture above depicts a set of 7 cats, stored in the kind of hash table that we will be creating. Note that although the picture contains what looks like an array, we're actually going to use an ArrayList, because generics with arrays are a serious pain. Notice that the elements of the ArrayList are references to nodes, and are sometimes null. (An empty table will be an ArrayList of a certain size, containing all null entries.)

Before you read any further, look over the starter code that has been provided for the MyHashSet class. Notice that we are using the type variable E throughout. (E for "element"). You'll find a Node class nested inside the MyHashSet class. As you would expect, each Node has a data element (the cats in the picture, above) and a "next" reference, which will either refer to another Node in this same bucket, or will be null if this Node is the last one in the bucket.

The MyHashSet class will implement the Iterable interface, which of course means that you must implement the "iterator" method. Your Iterator will allow the user to iterate over all of the elements stored in the table. Note that you do not have to implement the remove method for the iterator.



Implementation Details

Most of the specifications (including hints) are contained in comments embeded within the source code distribution. Below are some additional remarks that are important for you to understand before you implement this project.

Size vs. Capacity

In this specification, we will use the term "size" to indicate the number of data elements that are stored in the table. The term "capacity" will be used to describe the length of the table (the number of buckets). For example, the hash table pictured above is size 7, capacity 10. Make sure you understand this before reading further.

Hashing

You may assume that objects of type E to be inserted into the MyHashSet will satisfy Java's "HashCode Contract". You should use the following simple formula to determine which bucket a particular object belongs in, based on that object's hash code. (The object's hash code is obtained by simply calling the hashCode() method on the object. Writing a hashing function is not your job; that was handled by whomever implemented the objects of type E, whatever they are.)

bucket number = | hashCode % n |  
Iterator

Your Iterator should allow the user to iterate through all of the elements in the MyHashSet. The Iterator class should be implemented as an inner class of the MyHashSet class. You do not have to implement the remove method for the iterator.

The order in which the iterator traverses the data is irrelevant, and doesn't need to be the same each time.



Additional Requirements


Grading


Submission

Submit your project using the "Submit Project" option (available by right clicking on the project folder).



Academic Integrity

Please make sure you read the academic integrity section of the syllabus so you understand what is permissible in our programming projects. We want to remind you that we check your project against other students' projects and any case of academic dishonesty will be referred to the Office of Student Conduct


Web Accessibility