Description Write a program that provides for searching students by their id’s u
ID: 3767452 • Letter: D
Question
Description
Write a program that provides for searching students by their id’s using hash table. The program will first input students one by one from a file (insert.txt) and insert them into hash table. Then it will input students one by one from a text file (search.txt) and perform searches for them in the hash table. It will keep a record of searches when required and display the summary results.
Implementation
Class StudentHT
Create a class StudentHT that provides for the following struct definitions, instance variables and methods.
struct RecType contains student id, first name, and last name.
struct NodeType contains student id, first name, last name, and a pointer to NodeType
hashPtr – an instance variable pointing to hash table array of pointers
a constructor to input hash table size
void insert (RecType item)
will insert an item in the hash table.
item search (RecType item)
Will search an item in hash table and will return it. If not found will return an item containing -1 for id.
void startStatistics ();
void endStatistics ();
void displayStatistics();
Main Method
Provide main method that will create the hash table object and call its methods.
Hashing Algorithm
Calculate the hash index from student id as below:
hash index = (student id) % (total entries in hash table)
Statistics
Provide the following statistics
Total number if id’s searched
Total number of nodes searched
Average number of nodes search per id searched
Testing
Use the data in file Add.txt for inserting in the table
Use the data in file Search.txt for doing searches in the hash table.
Explanation / Answer
import java.util.*;
import java.io.*;
public class Record
{
private String name;
private String id;
private int NAMELENGTH=16;
private int IDLENGTH=4;
public Record()
{
name = " ";
id = " ";
}
public Record(String newName, String newId)
{
name = newName;
id = newId;
}
public void read(RandomAccessFile file) throws IOException
{
name = readString(file, NAMELENGTH);
id = readString(file, IDLENGTH);
}
public void write(RandomAccessFile file) throws IOException
{
writeStr(file, name, NAMELENGTH);
writeStr(file, id, IDLENGTH);
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
private String readString (RandomAccessFile file, int strLength) throws IOException
{
char[] chs = new char[strLength];
for (int i = 0; i < chs.length; i++)
{
chs[i] = file.readChar();
}
String theStr = new String(chs);
return theStr;
}
private void writeStr( RandomAccessFile file, String str, int strLength ) throws IOException
{
StringBuffer buffer = null;
if ( str != null )
buffer = new StringBuffer( str );
else
buffer = new StringBuffer( strLength );
buffer.setLength( strLength );
file.writeChars( buffer.toString() );
}
public String toString()
{
return " Name: " + name + " ID: " + id;
}
}
import java.io.*;
import java.io.RandomAccessFile;
public class RandomFile
{
public static void main( String args[] )
{
Record rec = new Record(" ", " ");
Record rec1 = new Record();
RandomAccessFile f = null;
try
{
f = new RandomAccessFile("file.txt", "rw");
f.seek(0);
rec.write(f);
}
catch (IOException e)
{
System.out.println("Cannot Write " + e);
}
try
{
if (f != null)
{
f.seek(0);
rec1.read(f);
f.close();
}
}
catch (EOFException ee)
{
System.out.println("End of File");
}
catch (IOException e)
{
System.out.println("Cannot Read " + e);
}
System.out.println(rec1);
}
}