
CPSC 212-301 Name: _____________________________
Test #2A October 23, 2006
Honor Pledge:
I pledge that I have neither given nor received unauthorized assistance on this test.
Signed: _______________________ Date: _____________
20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web page. You are to
develop a class called MyHashTable with one constructor:
public MyHashTable (int n) {
// ... your code ...
} // MyHashTable
which receives an int variable x which specifies the length (number of elements) of the hash table, and
three public methods:
public void insert (int x) {
// … inserts x into hash table
} // insert
public String toString () {
// ... your code ...
} // toString
The method insert(int x) inserts the value x into the hash table. Use f(x)=x%n where n is the
length of the hash table, as the primary hash function. Use double hashing to handle collisions; use the
double hashing function: h2(x)=11–(x%11). If you cannot insert the value in the hash table, print an error
message. Note that a value of -1 indicates that an array element is empty.
For 5-point bonus credit, implement the method delete(int x) which deletes the value x from the hash
table. Recall the need for a bit array to remember locations of values that have been deleted. Do not
attempt to write delete() until you have completely and correctly written insert().
The following test driver:
public static void main(String args[]) {
MyHashTable h = new MyHashTable(19);
System.out.println("Initial hash table: " + h);
int v = 15; h.insert(v); System.out.print("Inserted " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
v += 19; h.insert(v); System.out.print(", " + v);
System.out.println("\nAfter inserts : " + h);
} // end main
produces the following output:
Initial hash table: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
Inserted 15, 34, 53, 72, 91
After inserts : [-1, 72, -1, -1, 91, -1, 34, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, 53, -1]
This test driver is provided only as an example. You should test your code with a more rigorous test driver.