Question: Download the HashingExample.zip startup code from the LMS and import into your eclipse. Study it thoroughly. It is a working example. You could run it

Download the HashingExample.zip startup code from the LMS and import into your eclipse. Study it thoroughly. It is a working example. You could run it to check how hash table concept applying and it operation.
Update the code that will perform the followings:
Ask user to insert 5 different values in a hash table.
Ask user to enter key value and search about and display the result.
Allow user to delete key send to delete method and display the result.
THE CODE :
package yuc.edu.sa;
//import java.util.Hashtable;
public class HashingExample1{
private int[] hashTable = new int[10];
public int hash(int ele){
return ele % this.hashTable.length;
}
public boolean insert(int ele){
int pos = hash(ele);
System.out.println("Hashing key "+ pos);
if(this.hashTable[pos]==0)
this.hashTable[pos]= ele;
else{
for(int i = pos +1; i < this.hashTable.length; i++){
if(this.hashTable[i]==0){
this.hashTable[i]= ele;
return true;
}
}
}
return true;
}
public int search(int ele){
int loc =-1;
int pos = hash(ele);
if(hashTable[pos]== ele)
loc = pos;
else{
for(int i = pos +1; i < this.hashTable.length; i++){
if(this.hashTable[i]== ele)
loc = i;
}
}
return loc;
}
public static void main(String[] args){
HashingExample1 he = new HashingExample1();
System.out.println(he.insert(245));
System.out.println(he.insert(342));
System.out.println(he.insert(156));
System.out.println(he.insert(345));
System.out.println(he.insert(999));
System.out.println(he.insert(109));
System.out.println("Element in hash table");
for(int i =0; i < he.hashTable.length; i++)
System.out.printf("Hashtable[%d]--->%d
", i, he.hashTable[i]);
int loc = he.search(342);
if(loc ==-1)
System.out.println("Element 342 is not found");
else
System.out.println("Element 342 is found at index position "+ loc);
loc = he.search(345);
if(loc ==-1)
System.out.println("Element 345 is not found");
else
System.out.println("Element 345 is found at index position "+ loc);
String str = "ABCDEF";
System.out.println(str.hashCode());
}
}

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!