CPSC 212-301 MyHashTable Test 2A, Exams of Algorithms and Programming

Information about a programming assignment for a computer science course, specifically for creating a myhashtable class with insert and tostring methods, and an optional delete method. Students are required to use a primary hash function and double hashing to handle collisions. An example test driver and submission instructions.

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-v7n
koofers-user-v7n 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf2

Partial preview of the text

Download CPSC 212-301 MyHashTable Test 2A and more Exams Algorithms and Programming in PDF only on Docsity!

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: h 2 (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.

Submission:

Submit your work using: handin.212.301 2 MyHashTable.java

Also submit any helper methods and/or classes you developed during this test and required by your class.

Important:

If you cannot complete one or more of the methods (for example the delete() method), be sure to include

at least a skeleton of the method which prints the String:

“Method ___________ not completed”

so that my test driver will be able to compile your program.

Evaluation:

Your program will be evaluated on: (a) correctness of algorithm, and (b) efficiency of algorithm.