Help needed with Java Program! The two pieces of code provided must be combined
ID: 3910342 • Letter: H
Question
Help needed with Java Program! The two pieces of code provided must be combined to fit the following question.
Below is a hash function, followed by the code.
---------------------------------------------------------------------------
public static int hashFunc3(String key)
{
int hashVal = 0;
for(int=0;j for (int 0; j {
int letter = key.charAt(j)-96; // get char code
hashVal = (hashVal * 27 + letter)%arraySize; // mod
}
return hashVal;
} // end hashFunc3
---------------------------------------------------------------------------
class DataItem
{
private int iData;
public DataItem(int ii)
{ iData = ii; }
public int getKey()
{ return iData; }
}
class HashTable
{
private DataItem[] hashArray;
private int arraySize;
private DataItem nonItem;
public HashTable(int size)
{
arraySize = size;
hashArray = new DataItem[arraySize];
nonItem = new DataItem(-1);
}
public void displayTable()
{
System.out.print("Table: ");
for(int j=0; j {
if(hashArray[j] != null)
System.out.print(hashArray[j].getKey() + " ");
else
System.out.print("** ");
}
System.out.println("");
}
public int hashFunc(int key)
{
return key % arraySize;
}
public void insert(DataItem item)
{
int key = item.getKey();
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null &&
hashArray[hashVal].getKey() != -1)
{
++hashVal;
hashVal %= arraySize;
}
hashArray[hashVal] = item;
}
public DataItem delete(int key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
{
DataItem temp = hashArray[hashVal];
hashArray[hashVal] = nonItem;
return temp;
}
++hashVal;
hashVal %= arraySize;
}
return null;
}
public DataItem find(int key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
return hashArray[hashVal];
++hashVal;
hashVal %= arraySize;
}
return null;
}
}
class HashTableApp
{
public static void main(String[] args) throws IOException
{
DataItem aDataItem;
int aKey, size, n, keysPerCell;
System.out.print("Enter size of hash table: ");
size = getInt();
System.out.print("Enter initial number of items: ");
n = getInt();
keysPerCell = 10;
HashTable theHashTable = new HashTable(size);
for(int j=0; j {
aKey = (int)(java.lang.Math.random() *
keysPerCell * size);
aDataItem = new DataItem(aKey);
theHashTable.insert(aDataItem);
}
while(true)
{
System.out.print("Enter first letter of ");
System.out.print("show, insert, delete, or find: ");
char choice = getChar();
switch(choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Enter key value to insert: ");
aKey = getInt();
aDataItem = new DataItem(aKey);
theHashTable.insert(aDataItem);
break;
case 'd':
System.out.print("Enter key value to delete: ");
aKey = getInt();
theHashTable.delete(aKey);
break;
case 'f':
System.out.print("Enter key value to find: ");
aKey = getInt();
aDataItem = theHashTable.find(aKey);
if(aDataItem != null)
{
System.out.println("Found " + aKey);
}
else
System.out.println("Could not find " + aKey);
break;
default:
System.out.print("Invalid entry ");
}
}
}
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);
}
}
Implement a linear probe hash table that stores strings. You'll need a hash function that converts a string to an index number; see the section "Hashing Strings" in this chapter. Assume the strings will be lowercase words, so 26 characters will suffice.Explanation / Answer
import java.io.*;
import java.util.*;
import java.lang.*;
class DataItem
{
private String iData;
public DataItem(String ii)
{ iData = ii; }
public String getKey()
{ return iData; }
}
class HashTable
{
private DataItem[] hashArray;
private int arraySize;
private DataItem nonItem;
public HashTable(int size)
{
arraySize = size;
hashArray = new DataItem[arraySize];
for (int i = 0; i<arraySize; i++){
hashArray[i] = null;
}
//nonItem = new DataItem(-1);
}
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("");
}
public void insert(DataItem item)
{
String key = item.getKey();
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
++hashVal;
hashVal %= arraySize;
}
hashArray[hashVal] = item;
}
public DataItem delete(String key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
{
DataItem temp = hashArray[hashVal];
hashArray[hashVal] = null;
return temp;
}
++hashVal;
hashVal %= arraySize;
}
return null;
}
public DataItem find(String key)
{
int hashVal = hashFunc(key);
while(hashArray[hashVal] != null)
{
if(hashArray[hashVal].getKey() == key)
return hashArray[hashVal];
++hashVal;
hashVal %= arraySize;
}
return null;
}
public int hashFunc(String key)
{
int hashVal = 0;
for(int j=0; j < key.length(); j++){
int letter = key.charAt(j)-96; // get char code
hashVal = (hashVal * 27 + letter) % arraySize; // mod
}
return hashVal;
}
}
public class HashTableApp
{
public static void main(String[] args) throws IOException
{
DataItem aDataItem;
int size, n;
String aKey;
String str = "abcdefghijklmnopqrstuvwxyz";
System.out.print("Enter size of hash table: ");
size = getInt();
System.out.print("Enter initial number of items: ");
n = getInt();
HashTable theHashTable = new HashTable(size);
Random rand = new Random();
for(int j=0; j < n; j++){
aKey = str.substring(0,rand.nextInt(26));
System.out.println(aKey);
aDataItem = new DataItem(aKey);
theHashTable.insert(aDataItem);
}
while(true)
{
System.out.print("Enter first letter of ");
System.out.print("(s)how, (i)nsert, (d)elete, or (f)ind: ");
char choice = getChar();
switch(choice)
{
case 's':
theHashTable.displayTable();
break;
case 'i':
System.out.print("Enter key value to insert: ");
aKey = getString();
aDataItem = new DataItem(aKey);
theHashTable.insert(aDataItem);
break;
case 'd':
System.out.print("Enter key value to delete: ");
aKey = getString();
theHashTable.delete(aKey);
break;
case 'f':
System.out.print("Enter key value to find: ");
aKey = getString();
aDataItem = theHashTable.find(aKey);
if(aDataItem != null)
{
System.out.println("Found " + aKey);
}
else
System.out.println("Could not find " + aKey);
break;
default:
System.out.print("Invalid entry ");
}
}
}
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.