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

Program with Java using data structures The objective is to create your own Hash

ID: 3710794 • Letter: P

Question

Program with Java using data structures

The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in.

Employees.txt: (No WhiteSpace between lines)

135,John Peterman

160,Joe Divola

101,David Putty

68,Jerry Seinfeld

225,George Costanza

100,Elaine Benes

200,Cosmo Kramer

TableEntry.java:

public class TableEntry {

private int key;

private String value;

TableEntry(int key, String value) {

this.key = key;

this.value = value;

}

public int getKey() {

return key;

}

public String getValue() {

return value;

}

}

Directions:

1) Your hash table class should have an array of TableEntry objects that store the key(id number) and value(name)

2) Use the Linear probing technique to handle collisions when you add or search the table

3) You hash function code should be in its own method, NOT in your main method.

You don't have to worry about removing data from the table or keeping track of “Empty Since Start” or “Empty After Removal” buckets. Treat all empty buckets as "Empty since start".

You must have these 3 classes: Main.java, HashTable.java, TableEntry.java

Flow of the main program:

Create an instance of your hash table class in the main class.

Read in the Employees.txt file and store the names and ID numbers in your hash table using the SIMPLE modulo operator (%) hash function: "key%N" where N is the size of your array.

After you stored all the data in the table, print out all the employee names and their corresponding id numbers.

Pick a random employee's ID number and then search for them in the hash table and retrieve their name, print it to the screen.

must read in data from text file Employees.txt (No Whitespace between entries, just next line between entries)

Explanation / Answer

package chegg2;

public class TableEntry {

private int key;

private String value;

TableEntry(int key, String value) {

this.key = key;

this.value = value;

}

public int getKey() {

return key;

}

public String getValue() {

return value;

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "TableEntry [key=" + key + ", value=" + value + "]";

}

}

*******************************************************************************

package chegg2;

public class HashTable {

public static int SizeOfTable =0;

  

TableEntry[] table;

HashTable(int sizeoftable) {

this.SizeOfTable=sizeoftable;

table = new TableEntry[SizeOfTable];

for (int i = 0; i < SizeOfTable; i++)

table[i] = null;

}

public String get(int key) {

int hashValue = (key % SizeOfTable);

while (table[hashValue] != null && table[hashValue].getKey() != key)

hashValue = (hashValue + 1) % SizeOfTable;

if (table[hashValue] == null)

return null;

else

return table[hashValue].getValue();

}

public void put(int key, String value) {

int hashValue = (key % SizeOfTable);

while (table[hashValue] != null && table[hashValue].getKey() != key)

hashValue = (hashValue + 1) % SizeOfTable;

table[hashValue] = new TableEntry(key, value);

}

}

***********************************************************************************

package chegg2;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class Main {

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

TableEntry[] tableentries=new TableEntry[7];

HashTable table=new HashTable(7);

BufferedReader reader = new BufferedReader(new FileReader(new File(

"D:\employee.txt")));

String line = null;

int count=0;

while ((line = reader.readLine()) != null) {

String[] words = line.split(",");

if(words.length==2)

{

if(line!=null)

{

String key=words[0];

String value=words[1];

TableEntry entry=new TableEntry(Integer.parseInt(key), value);

tableentries[count]=entry;

count++;

table.put(Integer.parseInt(key), value);

}

}

  

}

for(TableEntry entry:tableentries)

{

System.out.println(entry);

}

}

}

output

TableEntry [key=135, value=John Peterman]
TableEntry [key=160, value=Joe Divola]
TableEntry [key=101, value=David Putty]
TableEntry [key=68, value=Jerry Seinfeld]
TableEntry [key=225, value=George Costanza]
TableEntry [key=100, value=Elaine Benes]
TableEntry [key=200, value=Cosmo Kramer]