(Solved Homework): Using the hashtbale code provided below create a program that makes a registry for student records. Create a class

Using the hashtbale code provided below 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.

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

 ==============================================

Updated Code
==============================================

Changes:

1. Chaned the casting logic from Char to Character for below code
Before:
while(!queue.isEmpty()){
stack = (Stack)queue.poll();
key[i++] = (Character)stack.pop();
key[i++] = (Character)stack.pop();
if(!stack.empty())
key[i++] = (Character)stack.pop();
}
After:
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();
}

2. Update the toString logic from Arrays.toString to normal toString method.

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

3. Updated the Arrays sort logc to Collections sorting logic.
Before:

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

After:

@Override
public V[] getSortedList(V[] list) {
Collections.singleton(list);
return list;
}

4. Remove the unused import for Arrays and use Collections Class import.

Before:
import java.util.Arrays;

After:
import java.util.Collections;

5. Please use the JAVA8 latest version.

Hashtbl.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

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<K, V>(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<HashNode<K, V>>();
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++] = (Character)stack.pop();
key[i++] = (Character)stack.pop();
if(!stack.empty())
key[i++] = (Character)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: ” + map.getValuesList().toString());
}
@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) {
Collections.singleton(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));
}
}

Output :

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents