JAVA Hash function question. I need help with this problem. Use the following ha
ID: 663454 • Letter: J
Question
JAVA Hash function question.
I need help with this problem.
Use the following hash function to build a hash table of array size (10).
hashFunction(s)
hash = ( s.charAt(0) A ) MOD 10
return hash
Use the following string input to fill in the hash table:
Apple, Banana, Kite, Motorcycle, Book, Computer, Data, Information, Dog, Teacher, White, Black, Blue, Green, Red, Yellow, Orange, Car.
To handle collisions in hash tables, we can use separate chaining or linear probing. Draw two hash tables for each one.
Explanation / Answer
import java.util.Hashtable;
public class Hash {
public int hashFunction(String s){
int hash;
hash=(s.charAt(0)-'A')%10;
return hash;
}
public void generateHashTable(){
Hashtable h=new Hashtable(10);
h.put("Apple", hashFunction("Apple"));
h.put("Banana", hashFunction("Banana"));
h.put("Kite", hashFunction("Kite"));
h.put("Motorcycle", hashFunction("Motorcycle"));
h.put("Book", hashFunction("Book"));
h.put("Computer", hashFunction("Computer"));
h.put("Data", hashFunction("Data"));
h.put("Information", hashFunction("Information"));
h.put("Dog", hashFunction("Dog"));
h.put("Teacher", hashFunction("Teacher"));
System.out.println(h);
}
public static void main(String[] args){
Hash h=new Hash();
h.generateHashTable();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.