Question: Modify this following program so that it can display this same Output. It must be the SAME. Your posted output after modifying MUST be this

Modify this following program so that it can display this same Output. It must be the SAME.

Your posted output after modifying MUST be this one (exept for thr random #'s that can be any):

(Try size=1000, N=3, max=1000000)

Enter size of hash table: 10

Enter number of items: 4

Enter max key value: 1000000

key=889845, groups are 5+4+8+9+8+8, hashValue is 42, trimmed to 2

key=964039, groups are 9+3+0+4+6+9, hashValue is 31, trimmed to 1

key=806169, groups are 9+6+1+6+0+8, hashValue is 30, trimmed to 0

key=265774, groups are 4+7+7+5+6+2, hashValue is 31, trimmed to 1

Enter first letter of show, insert, delete, or find: s

Table: 806169 964039 889845 265774 ** ** ** ** ** **

Enter first letter of show, insert, delete, or find: i

Enter key value to insert: 65789

key=65789, groups are 9+8+7+5+6, hashValue is 35, trimmed to 5

Enter first letter of show, insert, delete, or find: s

Table: 806169 964039 889845 265774 ** 65789 ** ** ** **

Enter first letter of show, insert, delete, or find: i

Enter key value to insert: 98765

key=98765, groups are 5+6+7+8+9, hashValue is 35, trimmed to 5

Enter first letter of show, insert, delete, or find: s

Table: 806169 964039 889845 265774 ** 65789 98765 ** ** **

Enter first letter of show, insert, delete, or find: d

Enter key value to delete: 265774

key=265774, groups are 4+7+7+5+6+2, hashValue is 31, trimmed to 1

Enter first letter of show, insert, delete, or find: s

Table: 806169 964039 889845 -1 ** 65789 98765 ** ** **

Enter first letter of show, insert, delete, or find:

THIS IS THE PROGRAM THAT NEEDS TO BE MODIFIED:

import java.io.*;

////////////////////////////////////////////////////////////////

class DataItem

{ // (could have more data)

private int iData; // data item (key)

//--------------------------------------------------------------

public DataItem(int ii) // constructor

{ iData = ii; }

//--------------------------------------------------------------

public int getKey()

{ return iData; }

//--------------------------------------------------------------

} // end class DataItem

////////////////////////////////////////////////////////////////

class HashTable {

private DataItem[] hashArray; // array holds hash table

private int arraySize;

private DataItem nonItem; // for deleted items

private int divisor;

// -------------------------------------------------------------

public HashTable(int size) // constructor

{

arraySize = size;

hashArray = new DataItem[arraySize];

nonItem = new DataItem(-1); // deleted item key is -1

divisor = 1;

while (size > 0)

{

size/= 10;

divisor *= 10;

}

}

// -------------------------------------------------------------

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)

{

int hashVal = 0;

while(key > 0)

{hashVal += key % divisor;

key /= divisor;

}

return hashVal % arraySize; // hash function

}

// -------------------------------------------------------------

public void insert(DataItem item) // insert a DataItem

// (assumes table not full)

{

int key = item.getKey(); // extract key

int hashVal = hashFunc(key); // hash the key

// until empty cell or -1,

while(hashArray[hashVal] != null &&

hashArray[hashVal].getKey() != -1)

{

++hashVal; // go to next cell

hashVal %= arraySize; // wraparound if necessary

}

hashArray[hashVal] = item; // insert item

} // end insert()

// -------------------------------------------------------------

// -------------------------------------------------------------

public DataItem delete(int key) // delete a DataItem

{

int hashVal = hashFunc(key); // hash the key

while(hashArray[hashVal] != null) // until empty cell,

{ // found the key?

if(hashArray[hashVal].getKey() == key)

{

DataItem temp = hashArray[hashVal]; // save item

hashArray[hashVal] = nonItem; // delete item

return temp; // return item

}

++hashVal; // go to next cell

hashVal %= arraySize; // wraparound if necessary

}

return null; // cant find item

} // end delete()

// -------------------------------------------------------------

public DataItem find ( int key) // find item with key

{

int hashVal = hashFunc(key); // hash the key

while (hashArray[hashVal] != null) // until empty cell,

{ // found the key?

if (hashArray[hashVal].getKey() == key)

return hashArray[hashVal]; // yes, return item

++hashVal; // go to next cell

hashVal %= arraySize; // wraparound if necessary

}

return null; // cant find item

}

// -------------------------------------------------------------

} // end class HashTable

////////////////////////////////////////////////////////////////

class HashTableApp

{

public static void main(String[] args) throws IOException

{

DataItem aDataItem;

int aKey, size, n, maxKeyVal;

// get sizes

System.out.println("(Try size=1000, N=3, max=1000000)");

System.out.print("Enter size of hash table: ");

size = getInt();

System.out.print("Enter number of items: ");

n = getInt();

System.out.print("Enter max key value: ");

maxKeyVal = getInt();

// make table

HashTable theHashTable = new HashTable(size);

for(int j=0; j

{

aKey = (int)(java.lang.Math.random() * maxKeyVal);

aDataItem = new DataItem(aKey);

theHashTable.insert(aDataItem);

}

while(true) // interact with user

{

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 ");

} // end switch

} // end while

} // 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);

}

//--------------------------------------------------------------

} // end class HashTableApp

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 Databases Questions!