Answered! Use/slightly modify the hashtable code provided below to create a program that makes a registry for student records….

Use/slightly modify the hashtable code provided below to create a program that makes a registry for student records. Create a class that keeps students info (name, student ID, and grade). If must do the following.

It must be a persistent record, so the registry should be saved on file when exiting, and after any major changes.

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Use/slightly modify the hashtable code provided below to create a program that makes a registry for student records….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

It should give an option to make an new entry with the students name, ID, and grade.

It should give an option to lookup a student from their ID (this will ke the key in the hash table).

It should give an option to remove an entry by providing the student ID.

Lastly an option to print the whole registry sorted by ID.

HashTable.java

public interface HashTable<K,V> {

public void add(K key, V value);

public V remove(K key);

public V lookup(K key);

public Object[] getValuesList();

public V[] getSortedList(V[] list);

public void printReport();

}

Hashtbl.java

import java.util.*;

public class Hashtbl<K, V> implements HashTable<K, V> {

ArrayList<HashNode<K, V>> bucket = new ArrayList<>();
int numBuckets = 10;
int size;

public Hashtbl() {
for (int i = 0; i < numBuckets; i++) {
bucket.add(null);
}
}

public int getSize() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public int additiveHashing(char[] key, int numBuckets) {
int hash = 0;
for (char c : key) {
hash += c;
}
return hash % numBuckets;
}

public int xorHashing(char[] key, int numBuckets) {
int hash = 0;
for (char c : key) {
hash ^= c;
}
return hash % numBuckets;

}
public int xorShiftHashing(char[] key, int numBuckets){
int hash = 0;

for(char c : key){
hash += (c << 3) ^ (c >> 5) ^ hash;
}
hash = Math.abs(hash);

return hash % numBuckets;
}

public void add(K key, V value) {
String a = key.toString();
char[] b = a.toCharArray();
//int index = additiveHashing(b, numBuckets);
//int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
HashNode<K, V> toAdd = new HashNode<>(key, value);

if (head == null) {
bucket.set(index, toAdd);
size++;
} else {
while (head != null) {
if (head.key.equals(key)) {
head.value = value;
// size++; No need to increase the size, as the key is already present in the table
break;
}
head = head.next;
}
if (head == null) {
head = bucket.get(index);
toAdd.next = head;
bucket.set(index, toAdd);
size++;
}
}

// Resizing logic
if ((1.0 * size) / numBuckets > 0.7) {
// do something
ArrayList<HashNode<K, V>> tmp = bucket;
bucket = new ArrayList<>();
numBuckets = 2 * numBuckets;
for (int i = 0; i < numBuckets; i++) {
bucket.add(null);
}

for (HashNode<K, V> headNode : tmp) {
while (headNode != null) {
add(headNode.key, headNode.value);
headNode = headNode.next;
}
}
}
}

public V remove(K key) {
String a = key.toString();
char[] b = a.toCharArray();
//int index = additiveHashing(b, numBuckets);
//int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
if (head == null) {
return null;
}
if (head.key.equals(key)) {
V val = head.value;
head = head.next;
bucket.set(index, head);
size–;
return val;
} else {
HashNode<K, V> prev = null;
while (head != null) {

if (head.key.equals(key)) {
prev.next = head.next;
size–;
return head.value;
}
prev = head;
head = head.next;
}
size–;
return null;
}
}

public V lookup(K key) {
String a = key.toString();
char[] b = a.toCharArray();
//int index = additiveHashing(b, numBuckets);
//int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
while (head != null) {
if (head.key.equals(key)) {
return head.value;
}
head = head.next;
}
return null;
}
public String scrambleString(String k){
char key[] = k.toCharArray();
Stack stack = new Stack();
Queue<Stack> queue = new LinkedList<Stack>();
//storing
for(int i=0;i<key.length/3;i++){
stack.push(key[i*3]);
stack.push(key[i*3+1]);
stack.push(key[i*3+2]);
queue.add(stack);
stack = new Stack();
}
if(key.length%3==2){
stack = new Stack();
int pos = key.length-2;
stack.push(key[pos]);
stack.push(key[pos+1]);
queue.add(stack);
}
System.out.println(“Queue size:”+queue.size());
//retrieving and scrambling
int i=0;
while(!queue.isEmpty()){
stack = (Stack)queue.poll();
key[i++] = (char)stack.pop();
key[i++] = (char)stack.pop();
if(!stack.empty())
key[i++] = (char)stack.pop();
}
String a = new String(key);
return a;
}

public static void main(String[] args) {
Hashtbl<String, Integer> map = new Hashtbl<String, Integer>();
System.out.println(“Scrambled String: ” +map.scrambleString(“Joshua”) );
map.add(“this”, 1);
map.add(“blah”, 2);
map.add(“please”, 3);
map.add(“array”, 3);
map.add(“Java”, 5);
map.add(“Hi”, 5);
map.add(“WiFi”, 5);

map.printReport();

System.out.println(“Trying to find value of key “this”: ” + map.lookup(“this”));

System.out.println(“Values: ” + Arrays.toString(map.getValuesList()));
}

@Override
public Object[] getValuesList() {
// we have got total size elements
Object result[] = new Object[size];
int count=0;

for (int index = 0; index < numBuckets; index++) {
HashNode<K, V> head = bucket.get(index);
while (head != null) {
result[count++] = head.value;
head = head.next;
}
}

return result;
}

@Override
public V[] getSortedList(V[] list) {
Arrays.sort(list);
return list;
}

@Override
public void printReport() {
int usedBuckets = 0;
int longestBucket = 0;
int totalBucketLen = 0;

System.out.println(“n+++++++++++++++++ Printing HashTable ++++++++++++n”);
for (int index = 0; index < numBuckets; index++) {
System.out.print(“Index ” + index + “: “);
HashNode<K, V> head = bucket.get(index);
int bucketLen = 0;

if(head != null) {
usedBuckets++;
}
while (head != null) {
System.out.print(head.key + “(” + head.value + “)” + ” => “);
head = head.next;
bucketLen++;
}
System.out.println();

if(bucketLen > longestBucket) {
longestBucket = bucketLen;
}
totalBucketLen += bucketLen;
}
System.out.println(“++++++++++++++++++++++++++++++++++++++++++++++++++++”);
System.out.println(“Load factor: ” + (1.0 * usedBuckets / numBuckets) + “n”
+ “Longest Chain: ” + longestBucket + ” collisions” + “n”
+ “Density Factor: ” + (1.0 * size/numBuckets) + “n”
+ “Chaining Factor: ” + (1.0 * totalBucketLen / numBuckets));
}
}

HashNode.java

class HashNode<K, V> {
K key;
V value;
HashNode<K, V> next = null;

public HashNode(K key, V value) {
this.key = key;
this.value = value;
}
}

Expert Answer

 Here is your code: –

HashNode.java

class HashNode<K, V> {
K key;
V value;
HashNode<K, V> next = null;

public HashNode(K key, V value) {
this.key = key;
this.value = value;
}
}

Hashtbl.java

import java.util.*;

public class Hashtbl<K, V> implements HashTable<K, V> {
ArrayList<HashNode<K, V>> bucket = new ArrayList<>();
int numBuckets = 10;
int size;

public Hashtbl() {
for (int i = 0; i < numBuckets; i++) {
bucket.add(null);
}
}

public int getSize() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public int additiveHashing(char[] key, int numBuckets) {
int hash = 0;
for (char c : key) {
hash += c;
}
return hash % numBuckets;
}

public int xorHashing(char[] key, int numBuckets) {
int hash = 0;
for (char c : key) {
hash ^= c;
}
return hash % numBuckets;
}

public int xorShiftHashing(char[] key, int numBuckets) {
int hash = 0;

for (char c : key) {
hash += (c << 3) ^ (c >> 5) ^ hash;
}
hash = Math.abs(hash);

return hash % numBuckets;
}

public void add(K key, V value) {
String a = key.toString();
char[] b = a.toCharArray();
// int index = additiveHashing(b, numBuckets);
// int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
HashNode<K, V> toAdd = new HashNode<>(key, value);
if (head == null) {
bucket.set(index, toAdd);
size++;
} else {
while (head != null) {
if (head.key.equals(key)) {
head.value = value;
// size++; No need to increase the size, as the key is
// already present in the table
break;
}
head = head.next;
}
if (head == null) {
head = bucket.get(index);
toAdd.next = head;
bucket.set(index, toAdd);
size++;
}
}

// Resizing logic
if ((1.0 * size) / numBuckets > 0.7) {
// do something
ArrayList<HashNode<K, V>> tmp = bucket;
bucket = new ArrayList<>();
numBuckets = 2 * numBuckets;
for (int i = 0; i < numBuckets; i++) {
bucket.add(null);
}

for (HashNode<K, V> headNode : tmp) {
while (headNode != null) {
add(headNode.key, headNode.value);
headNode = headNode.next;
}
}
}
}

public V remove(K key) {
String a = key.toString();
char[] b = a.toCharArray();
// int index = additiveHashing(b, numBuckets);
// int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
if (head == null) {
return null;
}
if (head.key.equals(key)) {
V val = head.value;
head = head.next;
bucket.set(index, head);
size–;
return val;
} else {
HashNode<K, V> prev = null;
while (head != null) {
if (head.key.equals(key)) {
prev.next = head.next;
size–;
return head.value;
}
prev = head;
head = head.next;
}
size–;
return null;
}
}

public V lookup(K key) {
String a = key.toString();
char[] b = a.toCharArray();
// int index = additiveHashing(b, numBuckets);
// int index = xorHashing(b, numBuckets);
int index = xorShiftHashing(b, numBuckets);
HashNode<K, V> head = bucket.get(index);
while (head != null) {
if (head.key.equals(key)) {
return head.value;
}
head = head.next;
}
return null;
}

public String scrambleString(String k) {
char key[] = k.toCharArray();
Stack stack = new Stack();
Queue<Stack> queue = new LinkedList<Stack>();
// storing
for (int i = 0; i < key.length / 3; i++) {
stack.push(key[i * 3]);
stack.push(key[i * 3 + 1]);
stack.push(key[i * 3 + 2]);
queue.add(stack);
stack = new Stack();
}
if (key.length % 3 == 2) {
stack = new Stack();
int pos = key.length – 2;
stack.push(key[pos]);
stack.push(key[pos + 1]);
queue.add(stack);
}
System.out.println(“Queue size:” + queue.size());
// retrieving and scrambling
int i = 0;
while (!queue.isEmpty()) {
stack = (Stack) queue.poll();
key[i++] = (char) stack.pop();
key[i++] = (char) stack.pop();
if (!stack.empty())
key[i++] = (char) stack.pop();
}
String a = new String(key);
return a;
}

 

@Override
public Object[] getValuesList() {
// we have got total size elements
Object result[] = new Object[size];
int count = 0;

for (int index = 0; index < numBuckets; index++) {
HashNode<K, V> head = bucket.get(index);
while (head != null) {
result[count++] = head.value;
head = head.next;
}
}
return result;
}

@Override
public V[] getSortedList(V[] list) {
Arrays.sort(list);
return list;
}

@Override
public void printReport() {
int usedBuckets = 0;
int longestBucket = 0;
int totalBucketLen = 0;
System.out.println(“n+++++++++++++++++ Printing HashTable ++++++++++++n”);
for (int index = 0; index < numBuckets; index++) {
System.out.print(“Index ” + index + “: “);
HashNode<K, V> head = bucket.get(index);
int bucketLen = 0;

if (head != null) {
usedBuckets++;
}
while (head != null) {
System.out.print(head.key + “(” + head.value + “)” + ” => “);
head = head.next;
bucketLen++;
}
System.out.println();

if (bucketLen > longestBucket) {
longestBucket = bucketLen;
}
totalBucketLen += bucketLen;
}
System.out.println(“++++++++++++++++++++++++++++++++++++++++++++++++++++”);
System.out.println(“Load factor: ” + (1.0 * usedBuckets / numBuckets) + “n” + “Longest Chain: ” + longestBucket
+ ” collisions” + “n” + “Density Factor: ” + (1.0 * size / numBuckets) + “n” + “Chaining Factor: ”
+ (1.0 * totalBucketLen / numBuckets));
}
}

HashTable.java

public interface HashTable<K, V> {
public void add(K key, V value);

public V remove(K key);

public V lookup(K key);

public Object[] getValuesList();

public V[] getSortedList(V[] list);

public void printReport();
}

StudentInfo.java

public class StudentInfo implements Comparable<StudentInfo>{
private String name;
private int ID;
private String grade;

public StudentInfo() {

}

public StudentInfo(String name, int iD, String grade) {
this.name = name;
ID = iD;
this.grade = grade;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getID() {
return ID;
}

public void setID(int iD) {
ID = iD;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}

@Override
public int compareTo(StudentInfo arg0) {
if(this.ID < arg0.getID()) {
return -1;
} else if(this.ID > arg0.getID()) {
return 1;
} else {
return 0;
}
}

}

HashTableDriver.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class HashTableDriver {
public static void main(String[] args) throws IOException {
Hashtbl<Integer, StudentInfo> map = new Hashtbl<Integer, StudentInfo>();
map = readFile(map, “students.txt”);
int option = -1;
Scanner sc = new Scanner(System.in);
while (option != 5) {
System.out.println(“Select one option: “);
System.out.println(“1. Register Student.”);
System.out.println(“2. Look for a student. “);
System.out.println(“3. Remove Student.”);
System.out.println(“4. print all students.”);
option = Integer.parseInt(sc.next());

switch (option) {
case 1:
register(map, “students.txt”);
break;
case 2:
System.out.println(“Enter student ID to search for: “);
int ID = Integer.parseInt(sc.next());
lookUp(map, ID);
break;
case 3:
System.out.println(“Enter student ID to delete for: “);
int ID1 = Integer.parseInt(sc.next());
remove(map, ID1, “students.txt”);
break;
case 4:
printResults(“students.txt”);
break;
case 5:
break;
default:
System.out.println(“Enter Correct option.”);
break;
}
}

}

public static void register(Hashtbl<Integer, StudentInfo> map, String fileName) throws IOException {
String name, grade;
int ID;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter Student ID: “);
ID = Integer.parseInt(sc.next());
System.out.println(“Enter Name: “);
name = sc.next();
System.out.println(“Enter Grade: “);
grade = sc.next();
StudentInfo s = new StudentInfo(name, ID, grade);
map.add(ID, s);
writeToFile(s, fileName);
}

public static void lookUp(Hashtbl<Integer, StudentInfo> map, Integer ID) {
StudentInfo s = map.lookup(ID);
if (s != null) {
System.out.println(“ID: ” + s.getID());
System.out.println(“Name: ” + s.getName());
System.out.println(“Grade: ” + s.getGrade());
} else {
System.out.println(“record not found.”);
}
}

public static void remove(Hashtbl<Integer, StudentInfo> map, Integer ID, String fileName) throws IOException {
int size = map.getSize();
map = new Hashtbl<>();
Scanner sc = new Scanner(new File(fileName));
String line;
String[] word;

int count = 0;
while (sc.hasNextLine()) {
line = sc.nextLine();
word = line.split(” “);
StudentInfo s = new StudentInfo(word[1], Integer.parseInt(word[0]), word[2]);
if (s.getID() != ID && count == 0) {
FileWriter fw1 = new FileWriter(fileName, false);
map.add(Integer.parseInt(word[0]), s);
fw1.write(s.getID() + ” ” + s.getName() + ” ” + s.getGrade()+”n”);
count++;
fw1.close();
} else if (s.getID() != ID && count != 0) {
map.add(Integer.parseInt(word[0]), s);
writeToFile(s, fileName);
}
}
int size2 = map.getSize();

if(size > size2) {
System.out.println(“record deleted successfully.”);
} else {
System.out.println(“REcord not found.”);
}

}

public static void writeToFile(StudentInfo s, String fileName) throws IOException {
FileWriter fw = new FileWriter(fileName, true);

fw.write(s.getID() + ” ” + s.getName() + ” ” + s.getGrade() + “n”);
fw.close();
}

public static Hashtbl<Integer, StudentInfo> readFile(Hashtbl<Integer, StudentInfo> map, String fileName)
throws FileNotFoundException {
map = new Hashtbl<>();
Scanner sc = new Scanner(new File(fileName));
String line;
String[] word;
while (sc.hasNextLine()) {
line = sc.nextLine();
word = line.split(” “);
map.add(Integer.parseInt(word[0]), new StudentInfo(word[1], Integer.parseInt(word[0]), word[2]));
}

return map;
}

public static void printResults(String fileName) throws FileNotFoundException {
ArrayList<StudentInfo> students = new ArrayList<>();
Scanner sc = new Scanner(new File(fileName));
String line;
String[] word;
while (sc.hasNextLine()) {
line = sc.nextLine();
word = line.split(” “);
students.add(new StudentInfo(word[1], Integer.parseInt(word[0]), word[2]));
}
Collections.sort(students);
for (int i = 0; i < students.size(); i++) {
StudentInfo s = students.get(i);
System.out.println((i + 1) + “.”);
System.out.println(“ID: ” + s.getID());
System.out.println(“Name: ” + s.getName());
System.out.println(“Grade: ” + s.getGrade());
System.out.println();
}
if(students.size() < 1) {
System.out.println(“No results to display.”);
}
}
}

students.txt

11 SSS A+
26 SA A+
12121 Ssaaa A

Sample Run: –

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 1232
Name: Salil
Grade: A_

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
1
Enter Student ID:
12121
Enter Name:
Ssaaa
Enter Grade:
A
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 1232
Name: Salil
Grade: A_

4.
ID: 12121
Name: Ssaaa
Grade: A

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
2
Enter student ID to search for:
2
record not found.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
2
Enter student ID to search for:
12121
ID: 12121
Name: Ssaaa
Grade: A
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 1232
Name: Salil
Grade: A_

4.
ID: 12121
Name: Ssaaa
Grade: A

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
3
Enter student ID to delete for:
1111111
REcord not found.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
12121
Enter Correct option.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 1232
Name: Salil
Grade: A_

4.
ID: 12121
Name: Ssaaa
Grade: A

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
3
Enter student ID to delete for:
1212
REcord not found.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
3
Enter student ID to delete for:
2
REcord not found.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 1232
Name: Salil
Grade: A_

4.
ID: 12121
Name: Ssaaa
Grade: A

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
3
Enter student ID to delete for:
1232
record deleted successfully.
Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.
4
1.
ID: 11
Name: SSS
Grade: A+

2.
ID: 26
Name: SA
Grade: A+

3.
ID: 12121
Name: Ssaaa
Grade: A

Select one option:
1. Register Student.
2. Look for a student.
3. Remove Student.
4. print all students.

Still stressed from student homework?
Get quality assistance from academic writers!