Below is the code for HashMap and HashEntry public class HashMap { private final
ID: 3824118 • Letter: B
Question
Below is the code for HashMap and HashEntrypublic class HashMap { private final static int TABLE_SIZE = 100;
HashEntry[] table;
HashMap() { //Implement }
public String get(int key) { // Implement }
public void put(int key, String value) { // Implement }
public void linearProbe(int key, String value){ // Implement }
public void quadraticProbe(int key, String value){ // Implement }
}
public class HashMap { private final static int TABLE_SIZE = 100;
HashEntry[] table;
HashMap() { //Implement }
public String get(int key) { // Implement }
public void put(int key, String value) { // Implement }
public void linearProbe(int key, String value){ // Implement }
public void quadraticProbe(int key, String value){ // Implement }
}
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
public class HashEntry { private int key; private String value;
HashEntry(int key, String value) { this.key = key; this.value = value; }
public int getKey() { return key; }
public String getValue() { return value; } public void setValue(String val) { this.value = val; } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */
public class HashEntry { private int key; private String value;
HashEntry(int key, String value) { this.key = key; this.value = value; }
public int getKey() { return key; }
public String getValue() { return value; } public void setValue(String val) { this.value = val; } } 2. Create a hash table that is made of elements HashElement(int Key, String Value). The size of hash table will be 100. 3. Implement the following methods for the hash table: a. put(int Key, String Value): i. Puts the key value pair in the hash table at a certain index. ii. You need to implement a simple hash function H(key) key mod mapsize to find the index where you will put the pair iii. If collision occurs, i.e., a pair already exists in that index and the key is not the same as the current key, then you will use this function to resolve the collision, H(key) (7 H(key+1) mod mapsize, until you get an empty slot. iv. If the index is already full and the keys are the same, just replace the old value with the new one. b. get int Key): i. Gets the value associated with the Key. ii. You should not do linear search throughout the hash table for the key, rather you will calculate the index using the hash function stated above, go directly to that and retrieve the value. 4. Write a driver program to test your implementation of hash table. Allow the user to put or get data
Explanation / Answer
Please find the attached code, comment if you have any doubts.
=-=====HashMap========
import java.math.BigInteger;
public class HashMap {
private final static int TABLE_SIZE = 100;
HashEntry[] table;
HashMap() {
table = new HashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public String get(int key) {
int hash = new BigInteger(toAscii(key)).mod(new BigInteger(((Integer) TABLE_SIZE).toString())).intValue();
while (table[hash] != null && table[hash].getKey() != key)
hash = ((7 * hash) + 1) % TABLE_SIZE;
if (table[hash] == null)
return null;
else
return table[hash].getValue();
}
public void put(int key, String value) {
// creating hash code using key value given as a string
int hash = new BigInteger(toAscii(key)).mod(new BigInteger(((Integer) TABLE_SIZE).toString())).intValue();
while (table[hash] != null && table[hash].getKey() != key)
hash = ((7 * hash) + 1) % TABLE_SIZE;
table[hash] = new HashEntry(key, value);
}
public void linearProbe(int key, String value) {
// Implement
}
public void quadraticProbe(int key, String value) {
// Implement
}
public static String toAscii(int value) {
String s = String.valueOf(value);
StringBuilder sb = new StringBuilder();
long asciiInt;
// loop through all values in the string, including blanks
for (int i = 0; i < s.length(); i++) {
// getting Ascii value of character and adding it to the string.
char c = s.charAt(i);
asciiInt = (int) c;
sb.append(asciiInt);
}
return String.valueOf(sb);
}
}
====Main=====
import java.io.IOException;
public class HashTable {
public static void main(String[] args) throws IOException {
HashMap entry = new HashMap();
entry.put(36100, "Dipal");
entry.put(52120, "Hemant");
entry.put(22100, "Harleen");
System.out.println(entry.get(52120));
entry.put(12345, "John");
System.out.println(entry.get(12345));
}
}
=====O/P===
Hemant
John
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.