Can someone help me my Hashtable Frequency counter of words
Can someone help me my Hashtable Frequency counter of words my code is below please modify it for me
I believe my Quadratic probing is correct but can someone please check it for me espically my contains method and I need to use my Quadartatic probing for a word Frequency counter in the next part can someone help because it\'s not working for me all the words also must be descending order and if theres a tie pick the one that comes first alphabet wise also can someone check
Hashtable with Quadratic Probing Implement a Java class of hashtables using quadratic probing for collision resolution. The objects to be stored in the table are key-value pairs The keys are strings, and the values are integers The table sizes, m, are prime numbers. The first size should be 1031. Let n be the number of items in the table. When n m/2, you use the technique of dynamic arrays to enlarge the table You want to approximately double the table size but keep to the primes Use these primes: 2063 127, 8263, 16529, and 33071. You will not need larger primes for the purpose of this homework You may use Java String hashCode() as the hash function. The following is what you do for collision resolution. Let b be the hash value modulo m. If bucket b is occupied, you probe (b+12) m, (b-+22) m, (b- 32) m (b- 2)2) m, and stop as soon as you find an empty bucket. As long as n is kept less than m you will find an empty bucket by the end of the probing Use this page https docs oracle com avase/8/docs/api ava/util/Hashtable .html as a template for your class, but you do not need to implement all the methods. Implement as many as necessary to accomplish the following task 2 word Frequencies in a Text File Implement a Java program that counts word frequencies in a text file. Use a hashtable to store the data the words are the keys, and their frequencies are the values. The output of the program is the complete list of all the words and their frequencies in descending order of frequencies when two words have the same frequency, output them by alphabetical order. Each line of output consists of a word, a tab, and the frequency A sample input is \"The Tragedy of Hamlet, Prince of Denmark\", in the file Hamlet.txt. When processing the text, you should keep just the words, and discard all punctuation marks, digits, and so on. You also need to turn all upper case letters to lower cases.Solution
public class HashTableQ { private final int INIT_TABLE_SIZE = 1031; private HashEntry table[]; private int occupiedSize; //Number of table entries currently occupied private String[] keys; private int[] values; public HashTableQ(int initSize){ this.occupiedSize = 0; table = new HashEntry[initSize]; } public HashTableQ() { this.occupiedSize = 0; table = new HashEntry[INIT_TABLE_SIZE]; } public int size() { //Returns the size of the hash table return (occupiedSize); } public int getTotalSize(){ return table.length; } public boolean containsKey(String key) { String str = \"\"; //Dummy string for (int i = 0; i < keys.length; i++) { return (key.equals(keys[i])); //Return true if the key we are searching for is apparent when we are looping through the array } return false; } public boolean isEmpty() { //Returns true if the hash table is empty using above method to retrieve the size return (size() == 0); } private int getHashCode(String key, int tableLength) { return (Math.abs(key.hashCode() % tableLength)); } public void put(String key, int value) { int hashCode = getHashCode(key, table.length); // starting index int index = hashCode % table.length; int probeValue = 1; /* Detect collisions. It is a collision IF: First condition: Check if table position occupied (i.e. NOT empty) Second condition: Check if occupied position NOT the same key (because if SAME KEY it is not a collision since that is the key the user requested) The following loop will simply compute the correct index after accounting for collisions using quadratic probing */ while (table[index] != null && table[index].getKey() != key){ System.out.println(\"Collision detected!\"); index = (index + (probeValue^2))%table.length; probeValue++; if (probeValue >= table.length/2){ reSize(); } } HashEntry entryToWrite = new HashEntry(key, value); // Place the hash entry created to the index we calculated above table[index] = entryToWrite; occupiedSize++; if (occupiedSize >= table.length/2){ reSize(); } } public int get(String key) { int hashCode = getHashCode(key, table.length); // starting index int index = hashCode % table.length; int probeValue = 1; /* Detect collisions. It is a collision IF: First condition: Check if table position occupied (i.e. NOT empty) Second condition: Check if occupied position NOT the same key (because if SAME KEY it is not a collision since that is the key the user requested) The following loop will simply compute the correct index after accounting for collisions using quadratic probing */ while (table[index] != null && table[index].getKey() != key){ index = (index + (probeValue^2))%table.length; probeValue++; } / if (table[index].getKey() != key){ return -1; } else { return table[index].getValue(); } } public void reSize(){ //HashEntry[] newTable = Arrays.copyOf(table, table.length*2+1, table.getClass()); HashEntry[] newTable = new HashEntry[NextPrime(table.length)]; for (int i = 0; i < table.length; i++) { if (table[i] != null) { int hashCode = getHashCode(table[i].getKey(), newTable.length); int newIndex = hashCode % newTable.length; int probeValue = 1; while (newTable[newIndex] != null && newTable[newIndex].getKey() != table[i].getKey()){ newIndex = (newIndex + (probeValue^2))%newTable.length; probeValue++; } newTable[newIndex] = new HashEntry(table[i].getKey(), table[i].getValue()); } } table = newTable; } public int NextPrime(int size) { int n = size * 2; for (int x = n + 1; ; x++) { boolean isPrime = true; for (int i = 2; i < x / 2; i++) { if (x % i == 0) { isPrime = false; break; } } if (isPrime) { return x; } } } public class HashEntry { private String key; private int value; public HashEntry(String key, int value) { this.key = key; this.value = value; } public String getKey() { return this.key; } public int getValue() { return this.value; } }