Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is in java, Write a class that implements a hash table of strings.

ID: 3858853 • Letter: T

Question

This program is in java, Write a class that implements a hash table of strings. The program should prompt the user to input any strings and should have the menu to add, remove, contains to check whether the string a is in the table, size, and hash to compute the hash code of a string.

The table should have the following classes:
- add() – adds string a to the table

- remove() – removes string a from the table

- contains() – returns Boolean value that checks whether the string a is in the table

- size() – returns number of strings in the table

- A hash function to compute the hash code of a string

Explanation / Answer

HashTable_string.java

package venky_tasks;
import java.util.Scanner;
class HashTable
{
public int currentSize, maxSize;   
public String key_array[];   
public String val_array[];
public HashTable(int size)
{
currentSize = 0;
maxSize = size;
key_array=new String[maxSize];
val_array=new String[maxSize];
}
public void add(String key, String str)
{
int temp = hash(key);
int i=temp;
do
{
if (key_array[i] == null)
{
   key_array[i] = key;
val_array[i] = str;
currentSize++;
}
if (key_array[i].equals(key))
{
   val_array[i] = str;
}
i=(i+1)%maxSize;
} while(i!=temp);   
}
public void print()
{
System.out.println("---------------------");
System.out.println("Key Value");
for(int i=0;i<maxSize;i++)
{  
if (key_array[i] != null)
{
System.out.println(key_array[i] +" "+ val_array[i]);
}
System.out.println();
}
}   
public String search(String key)
{
int i = hash(key);
while (key_array[i] != null)
{
if (key_array[i].equals(key))
{
return val_array[i];
}
i=(i+1)%maxSize;
}
return null;
}
public int remove(String key)
{
if (search(key)==null)
{
   return -1;
}
int i = hash(key);
while (!key.equals(key_array[i]))
{
i=(i+1)%maxSize;
key_array[i]=null;
val_array[i] = null;   
for(i=(i+1)%maxSize;key_array[i]!=null;i=(i+1)%maxSize)
{
   String tmp1 = key_array[i];
   String tmp2 = val_array[i];
   key_array[i] = val_array[i] = null;
   currentSize--;
   add(tmp1, tmp2);
}
currentSize--;
}
return 1;
}
public void clear()
{
currentSize = 0;
key_array = new String[maxSize];
val_array = new String[maxSize];
}
public int getSize()
{
return currentSize;
}
private int hash(String key)
{
return key.hashCode()%maxSize;
}
}
public class HashTable_string {
   public static void main(String[] args)
   {
       int size,opt,res;
       String key=null;
       String value=null;
       String result=null;
       Scanner sc = new Scanner(System.in);
       System.out.println("-------------------------");
System.out.println("Hash Table Implementation");
System.out.println("Enter The Hash Table size");
size=sc.nextInt();
HashTable obj = new HashTable(size);
while(true)
{
System.out.println("********Hash Table Operations************ ");
System.out.println("1. -Add ");
System.out.println("2. -Remove");
System.out.println("3. -Contains");
System.out.println("4. -Size");
System.out.println("5. -Clear");
System.out.println("6. -Display");
System.out.println("7. -Exit");
System.out.println("Select Any Option: ");
opt= sc.nextInt();
switch (opt)
{
case 1 :
System.out.println("Enter key to add a Value");
key=sc.next();
System.out.println("Enter The Value");
value=sc.next();
obj.add(key,value);
break;
case 2 :   
System.out.println("Enter key To Delete");
key=sc.next();
res=obj.remove(key);
if(res==-1)
System.out.println("The key is Not Found !Unable To Delete");
break;
case 3 :
System.out.println("Enter key To Search");
key=sc.next();
result=obj.search(key);
if(result!=null)
System.out.println("Value = "+result);
else
System.out.println("The Key is Not Found");
break;   
case 4 :
   System.out.println("Size = "+ obj.getSize() );
break;
case 5 :
obj.clear();
System.out.println("Hash Table Cleared ");
break;
case 6:
   System.out.println("The Data in Hash Table is:");
   obj.print();
   break;
case 7:
   System.exit(0);
default :
System.out.println("Invalid Option! Try Again");
break;   
}
}
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote