Question: For the question below and my code gives out the result: 1 - > 2 1 , 2 - > 2 , 4 - >

For the question below and my code gives out the result:1->21,2->2,4->4,5->5. Please read the question and modify my code to make it work, do not have much time, I need the expected output, here is the question and my current code: In this assignment, you will be implementing the insertion and search algorithms of a very simple hash table.
The elements in the table will simply be non-negative integers, so that the data to be stored is already the
key (recall from the lectures that for non-integer data types, we would have to implement that hashCode()
method). The hash table itself is an abstract data type, defined by the following interface:
public interface OpenAddressTable {
/**
* @return the load factor of the table.
*/
double loadFactor();
/**
* The dictionary insert operation.
* @param value the value to be inserted into the dictionary.
* @throws java.lang.NullPointerException if the value is null.
*/
void insert(int value);
/**
* The dictionary search operation.
* @param value the value to search.
* @return the index associated with the given value, or -1 if the value is
* not present in the dictionary.
*/
int find(int value);
/**
* The extended hash function, which takes a pair of values (the key and the
* probe number; where a probe number i indicates the ith slot
* in the probe sequence.
* @param key the key to be stored or searched in the hash table.
* @param probenumber the number that specifies which element of the probe
* sequence is to be calculated.
* @return the slot number (i.e., index or position) to be probed in the table.
*/
int hash(int key, int probenumber);
}
In our lectures, we have seen two very different ways of implementing a hash table: direct addressing, and
open addressing. Open addressing, in turn, can again be done in multiple ways. This is why the basic idea
of using open addressing is not an implemented class, but an interface. A specific way of implementing open
addressing will, however, be a fully implemented class.
In this assignment, you have to use double hashing in the following class:
public class DoubleHashTable implements OpenAddressTable
Methods in this class that override the interface definitions must follow the documentation provided in the
respective interfaces. You can have additional features, fields, and methods in your class, but you cannot
violate the conditions and rules already specified in the interface documentation. A hash table that uses
1
CSE 214(Data Structures) Homework IV
double hashing must, of course, use two hash functions. We used the names h1 and h2 for these in the
lectures, and will use the same nomenclature here. Java provides really nice Function objects for such
applications. These objects use two parameters. A function that takes an input of type A and provides an
output of type B is declared as Function. For example, if I wanted to write a function that converts
a string to its first character, I would define it as follows:
Function toFirstChar = new Function(){
public Character apply(String s){
return s == null ? null : s.charAt(0);
}
};
Clearly, the hash functions h1 and h2 both are of the type Function.
In your implementation, you must create a constructor for the hash table that takes in a fixed table size and
two such hash functions, as follows:
DoubleHashTable table = new DoubleHashTable(5, h1, h2);
You should test your code with the two following functions (and include their implementation in the driver
method given below), but keep in mind that for grading, we may call the constructor with other hash
functions as well.
The function h1 takes a key and hashes it to the index of its leftmost digit, modulo the tables size.
The function h2 takes a key and hashes it to the index of its rightmost digit, modulo the tables size.
Your DoubleHashTable.java file must include the following as part of the main driver method:
public static void main(String[] args){
// your implementation of the h1 function described above (see rubric)
// your implementation of the h2 function described above (see rubric)
DoubleHashTable t = new DoubleHashTable(10, h1, h2);
}
The above code snippet should compile if you have correctly implemented the constructor and the two
Function objects.
Finally, you must also override the toString() method in your DoubleHashTable class, which has the signature
public String toString();
The string returned by your overriding implementation must be exactly in the following format (in increasing
order of the slot number:
slot -> key, slot -> key, ...
For example, if you have a table of size 5, and you are using double hashing with the above h1 and h2 to
insert 4,5,21, and 2, the following behavior is expected:
table.insert(4);
table.insert(5);
table.insert(21);
table.insert(2);
System.out.println(t.toString());

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!