Hi, can someone help with a java HashTable assignment? Thank you! For this progr
ID: 3804100 • Letter: H
Question
Hi, can someone help with a java HashTable assignment? Thank you!
For this programming project, you will be implementing a spelling checker. The spelling checker will accept words from a dictionary and a document and then output the words in the document that are not in the dictionary. You should store the words in the dictionary in a hash table and your program should output statistics about the hash table’s performance. The instructions are as follows:
1. Implement the separate chaining HashTable template class. You should use your HashTable class to store the words (i.e. strings) in the dictionary. Use one the hash function explained in your textbook.
2. Add a new method to the HashTable class called printStats() that prints the minimum, maximum, and mean chain length for all the chains in the hash table.
3. Use included dictionary and document files.
4. You may assume that the dictionary file always have the filename dictionary.txt and the document file always have the filename document.txt.
5. Save the dictionary and document files in the same directory as your executable code.
6. The dictionary file contains one word per line, and the first line will be the number of words in the file. The words are all in lowercase.
7. The document file contains lowercase words separated by spaces or new lines.
8. Your program should work with any dictionary and document files that comply with the above constraints.
9. Provide a main program. Your main program should do the following:
(a) Create a hash table whose size is equal to the number of words in the dictionary.
(b) Read the words from the dictionary and store each one in the hashtable.
(c) Output the statistics (i.e. minimum, maximum, and mean chain length) of the hash table after all words in the dictionary has been stored in the hash table.
(d) Read the words from the document and output each word that is NOT in the dictionary.
Explanation / Answer
ANSWER:
FILE: dictionary.txt
20
ape
apple
ali
bowl
bad
cotton
door
from
floor
from
good
hand
sonu
vivo
yellow
yard
zoo
ali
door
EOF
FILE: document.txt
door
hello
ape
god
hen
JAVA CODE:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;
public class HashTableMain {
public static void printStats(Hashtable<String, String> dictTable,
int numberOfWords) {
int min = dictTable.size();
int max = numberOfWords;
double mean = (min + max) / 2.f;
System.out.println("minimum maximum mean");
System.out.println(" "+min+" "+max+" "+mean);
}
@SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
int numberOfWords = 0;
int count = 1;
Hashtable<String, String> dictionaryTable = null;
Hashtable<String, String> wordsNotFound = new Hashtable<String, String>();
Scanner scan = new Scanner(new File("files/dictionary.txt"));
if (scan.hasNextLine()) {
numberOfWords = Integer.parseInt(scan.nextLine());
dictionaryTable = new Hashtable<String, String>(numberOfWords);
}
while (scan.hasNextLine()) {
String word = scan.nextLine();
dictionaryTable.put(word, word);
}
//System.out.println(dictionaryTable);
scan = new Scanner(new File("files/document.txt"));
while (scan.hasNextLine()) {
String docWord = scan.nextLine();
String fromDic = dictionaryTable.get(docWord);
if (fromDic == null) {
wordsNotFound.put(docWord, docWord);
}
}
System.out.println("Words not found in dictionary: ");
Set<String> keys = wordsNotFound.keySet();
for(String key: keys){
System.out.println(count+++": "+wordsNotFound.get(key));
}
printStats(dictionaryTable, numberOfWords);
}
}
Output for above program:
Words not found in dictionary:
1: hello
2: hen
3: god
minimum maximum mean
17 20 18.5
______________________________________________________
Working fine. Let me know any concerns. Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.