Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io . BufferedReader; import java.io . IOException; import java.io . InputStreamReader; import java.util.LinkedList; class Student { public String Name; public Student ( String name

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
class Student {
public String Name;
public Student(String name){
this.Name = name;}}
interface IHashTable {
public void add(Student student);
public void remove(Student student);
public boolean contains(Student student);
public int getSize();
public boolean isEmpty();
public void print();}
class HashTable implements IHashTable {
private int size;
private double occupancyThreshold;
private int loadFactor;
private LinkedList[] hashTable = null;
public HashTable(int slotCount, int loadFactor, double occupancyThreshold){
this.size =0;
this.loadFactor = loadFactor;
this.occupancyThreshold = occupancyThreshold;
this.hashTable = createHashTable(slotCount); }
private LinkedList[] createHashTable(int slotCount){
LinkedList[] hashTable = new LinkedList[slotCount];
for (int i =0; i < slotCount; i++){
hashTable[i]= new LinkedList();}
return hashTable;}
private int nextPrime(int n){
boolean isPrime;
n++;
while (true){
int sqrt =(int) Math.sqrt(n);
isPrime = true;
for (int i =2; i <= sqrt; i++){
if (n % i ==0)
isPrime = false;}
if (isPrime)
return n;
else {
n++;}}}
public void printState(){
String result = String.format(
"{"+//"LOAD_FACTOR: %d,"+//"OCCUPANCY_THRESHOLD: %.3f,"+//"OCCUPANCY_RATIO: %.3f,"+// "SIZE: %d,"+//"SLOT_COUNT: %d "+//"}"+//"
",
this.loadFactor,
this.occupancyThreshold,
this.calculateOccupancyRatio(),
this.size,
this.hashTable.length);
System.out.println(result);
this.print();}
private int getHashCode(Student student, int slotCount){// Write your code here...}
/* Return the ratio of total number of elements added to the HashTable to the*/
private double calculateOccupancyRatio(){// Write your code here...}
/*if the calculated occupancy ratio is more than [occupancyThreshold].*/
private boolean needToRehash(){// Code here}
/*Create a new HashTable with the [newSlotCount]*/
private void rehash(int newSlotCount){// Code here}
/*Use the provided function [nextPrime] to find the next new [slotCount].*/
@Override
public void add(Student student){// Code here}
@Override
public void remove(Student student){// Code here}
@Override
public boolean contains(Student student){// Code here}
@Override
public int getSize(){// Write your code here.. }
@Override
public boolean isEmpty(){// Code here}
@Override
public void print(){// Code here}
}
class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int OPERATION_COUNT = Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+$",
"").split("=")[1].trim());
int SLOT_COUNT = Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+$",
"").split("=")[1].trim());
int LOAD_FACTOR = Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+$",
"").split("=")[1].trim());
float OCCUPANCY_THRESHOLD = Float
.parseFloat(bufferedReader.readLine().replaceAll("\\s+$",
"").split("=")[1].trim());
bufferedReader.readLine();
HashTable hashTable = new HashTable(SLOT_COUNT, LOAD_FACTOR,OCCUPANCY_THRESHOLD);
IntStream.range(0, OPERATION_COUNT).forEach(opCountItr ->{
try {
List theInput = Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split("\\(")).collect(toList());
String action = theInput.get(0);
String argsString = theInput.get(1);
argsString = argsString.replaceAll("[\\);]","");
String arg0= null;
if (argsString.length()>0){
arg0= argsString.replaceAll("\"","");}
ProcessInputs(hashTable, action, arg0); }
catch (IOException exception){
throw new RuntimeException(exception);}
});
bufferedReader.close(); }
private static void ProcessInputs(HashTable hashTable, String action, String arg0){
switch (action){
case "add": hashTable.add(new Student(arg0));
break;
case "remove":hashTable.remove(new Student(arg0));
break;
case "contains": boolean contains = hashTable.contains(new Student(arg0));
System.out.println(contains);
break;
case "getSize":int size = hashTable.getSize();
System.out.println(size);
break;
case "isEmpty":boolean isEmpty = hashTable.isEmpty();
System.out.println(isEmpty);
break;
case "print":hashTable.printState();
break;
}}}
//Input(words like SLOT_COUNT,LOAD_FACTOR will be included in the code)
OPERATION_COUNT=8 SLOT_COUNT=5 LOAD_FACTOR=3 OCCUPANCY_THRESHOLD=0.5
add("Noah");
add("Oliver");
add("Samuel");
add("Henry");
add("David");
add("Victoria");
add("Audrey");
print();
Sample Output 8
{ LOAD_FACTOR: 3, OCCUPANCY_THRESHOLD: 0.500, OCCUPANCY_RATIO: 0.467, SIZE: 7, SLOT_COUNT: 5}//<=this part will need to be printed too
0: --> Noah --> Oliver --> Samuel --> null
1: --> null
2: --> null
3: --> Henry --> David --> Victoria --> Audrey --> null
4: --> null

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions