Question
Modify the program at the bottom so that it can display the EXACT SAME output posted bellow. Pay attention to fullfil these points: // 1.
Modify the program at the bottom so that it can display the EXACT SAME output posted bellow.
Pay attention to fullfil these points:
// 1. In time of insertion every no low case characters should be substituted by z when inserted two or more strings.
// 2. Not existing null is displayed as #Null#.
// 3. Deleted item is represented as qzqzq and displayed as Del#
OUTPUT that needs to be modified for. Please post the wole program and the output:
Enter size of hash table: 11
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: one
Enter first letter of show, insert, delete, or find: d
Enter string to delete: one
Deleted one
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Del# #Null# #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: one
Enter first letter of show, insert, delete, or find: s
Table: #Null# #Null# #Null# #Null# #Del# one #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find: i
Enter word to insert: two three
Enter first letter of show, insert, delete, or find: s
Table: #Null# twozthree #Null# #Null# #Del# one #Null# #Null# #Null# #Null# #Null#
Enter first letter of show, insert, delete, or find:
Program To be MODIFIED:
package stringhashtableapp;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner;
class StringDataItem {
private String sData;
public StringDataItem(String ss)
{ sData = ss; }
public String getKey()
{ return sData; }
}
class StringHashTable {
private StringDataItem[] hashArray;
private int arraySize;
private StringDataItem nonItem;
private StringDataItem DelItem;
public StringHashTable(int size)
{
arraySize = size;
hashArray = new StringDataItem[arraySize];
nonItem = new StringDataItem("#NULL#"); //deleted item key is a dash DelItem = new StringDataItem("#Del#"); for (int i = 0; i < size; i++)
hashArray[i] = new StringDataItem(nonItem.getKey());
}
public void displayTable()
{
System.out.print("Table: ");
for (int j = 0; j < arraySize; j++)
{
if (hashArray[j] != null)
System.out.print(hashArray[j].getKey() + " ");
else
System.out.print(" ");
}
System.out.println("");
}
//need to modify to parse string
public int hashFunc(String key) {
int hashVal = 0;
for (int j = 0; j < key.length(); j++)
{
int letter = key.charAt(j)-96;
hashVal = (hashVal * 27 + letter) % arraySize;
}
return hashVal;
}
public void insert(StringDataItem item) {
//assumes table not full
String key = item.getKey();
int hashVal = hashFunc(key);
while (hashArray[hashVal].getKey() != "#NULL#")
{
++hashVal;
hashVal %= arraySize;
}
hashArray[hashVal] = item; }
public StringDataItem delete(String key) {
int hashVal = hashFunc(key);
while (hashArray[hashVal].getKey() != "qzqzq" && hashArray[hashVal].getKey() != null)
{
if (hashArray[hashVal].getKey().equals(key))
{
StringDataItem temp = hashArray[hashVal];
hashArray[hashVal] = DelItem;
return temp; }
++hashVal;
hashVal %= arraySize;
}
return null;
}
public StringDataItem find(String key) {
int hashVal = hashFunc(key);
while (hashArray[hashVal].getKey() != "#NULL#" && hashArray[hashVal].getKey() != null)
{
if (hashArray[hashVal].getKey().equals(key))
return hashArray[hashVal];
++hashVal;
hashVal %= arraySize;
}
return null;
}
} //end class HashTable
class StringHashTableApp {
public static void main(String[] args) { Scanner sc=new Scanner(System.in); StringDataItem aDataItem1,aDataItem2;
int size, n=0, keysPerCell;
String aKey;
System.out.print("Enter size of hash table: ");
size = sc.nextInt();
keysPerCell = 10;
StringHashTable theHashTable = new StringHashTable(size);
for (int j = 0; j < n; j++) { aKey = Double.toString((java.lang.Math.random() * keysPerCell * size)); aDataItem1 = new StringDataItem(aKey); theHashTable.insert(aDataItem1); }
while (true) { System.out.print("Enter first letter of show, insert, delete, or find: ");
char choice = sc.next().charAt(0);
switch (choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Enter word to insert: ");
aKey = sc.next();
aDataItem1 = new StringDataItem(aKey);
theHashTable.insert(aDataItem1);
break;
case 'd':
System.out.print("Enter string to delete: ");
aKey = sc.next();
theHashTable.delete(aKey); System.out.print("Deleted "+aKey+" "); break;
case 'f':
System.out.print("Enter string to find: ");
aKey = sc.next();
aDataItem1 = theHashTable.find(aKey);
if (aDataItem1 != null)
System.out.println("Found " + aKey);
else
System.out.println("Could not find " + aKey);
break;
default:
System.out.println("Invalid entry!");
}
}
} //end main
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started