Sets and Maps

Due date: Mar 5, 2007

Your goal in this project is to build a Hashtable245 class that implements the Map245 and Set245 interfaces shown below. Much of the functionality between the two interfaces is the same. You will have to decide how to partition the functionality among the various functions. All of these functions are implicitly recursive because I can make a set of sets, for example. The toString method will call get and then toString and so on. You should explicitly include a set of sets in your test rig.

public interface Map245 {
    /** Map key to value.  Do not allow null keys nor null values.
     *  For null key/value, do nothing; i.e., return immediately.
     *  Do not allow duplicates. In other words, if you put to the same key
     *  twice, the new value replaces the old value.  Duplicate values
     *  may appear associated with different keys, however.
    public void put(Object key, Object value);

    /** Return the value associated with the key.  Return null
     *  if there is no value associated with that key.  For null key,
     *  do nothing.
    public Object get(Object key);

    /** The same as get(key) except that the key/value pair is deleted
     *  from the table. Return the object associated with key.
    public Object delete(Object key);

    /** How many elements are in the map?  This is not how much space is
     *  available in your implementation-- it is literally how many elements
     *  you have added.
    public int size();

    /** Return a string containing the elements.  The string should look
     *  EXACTLY like this for both implementations:
     *  [key1:value1, ..., keyN:valueN]
     *  The element separator is ", " (comma space) and there NO newline
     *  character as I will print that myself during testing.  You will
     *  call toString() on key and value in order to form the string.
    public String toString();

and here is Set245:

public interface Set245 {
    /** Added x uniquely to the set. If x is null, do nothing. */
    public void add(Object x);

    /** Return true if x is a member of the set else return false.
     *  If x is null, return false.
    public boolean member(Object x);

    /** Delete an element from the set. */
    public Object delete(Object x);

    /** Return how many elements there are in the set */
    public int size();

    /** Return a string containing the elements.  The string should look
     *  EXACTLY like this for both implementations:
     *  {v1, v2, ..., vN}
     *  The element separator is ", " (comma space) and there NO newline
     *  character as I will print that myself during testing.
     *  NOTE: this is not same function as toString()
    public String toSetString();

You must build Hashtable245 (watch the capitalization) that implements them both:

public class Hashtable245 implements Map245, Set245 {
    public static final int NUM_BUCKETS = 101;
    /** Your array of buckets made from your standard java class */
    List[] buckets = new LinkedList[NUM_BUCKETS];


Now that you know how to build a linked list, let's use the built-in Java LinkedList class to implement our buckets. Note that the array definition uses the List interface (abstract data type) for its type instead of the more specific LinkedList. This is because you always want to keep your code as flexible as possible. List is less specific and, hence, more general. When we tell it to create the array itself with new we do have to specify a specific type though.

Reminder: There are millions of implementations of hashtables out on the web. You are required to implement your own versions without cutting and pasting code from other people or any other source. You may discuss the project with your classmates, but you must not work together on a project. This includes not sitting next to each other in the lab and looking at each other's screens.

Project deliverables

You must turn in implementations of Hashtable245 and please include in your jar interfaces Map245 and Set245 from above.

Put the source and .class files into hash.jar.

Do NOT use a Java package for this project.

For the mid-release, I need a print out of just your code.

For the final release of this project, I need your final printout of your code.


You will submit a jar file called hash.jar containing source and *.class files (include Map245, Set245) into the submit directory:


(Use Java 1.5 or lower to compile your classes). For example, I would submit my project as file:



We will run your program via

java -cp ".:/home/submit/cs245/userid/hash.jar" parrtTestProgram

We will assign points as follows:

points what
10 mid-project release Wed Feb 28th
10 unit tests
8 Hashtable245.put
8 Hashtable245.get
8 Hashtable245.delete
8 Hashtable245.size
8 Hashtable245.toString
8 Hashtable245.add
8 Hashtable245.member
8 Hashtable245.delete
8 Hashtable245.size
8 Hashtable245.toString

For each 8 point method, we will assign 6 points to functionality and 2 points to style. Style includes how clean the code is, how well you comment, how good the method and variable names are, whether you have removed all your debugging print statements, removed commented-out dead code, etc...

5 points off if your jar is messed up or your classes or have wrong case etc... I.e., anything that prevents us from being able to run your library "out of the box".

Reminder: There is no such thing as a late project; late projects get a zero score. Projects are due the instant class starts at 9:40 a.m. and your jars must be in the submit directory.