import java.io.IOException; /** * An executable that counts the words in a files
ID: 3908083 • Letter: I
Question
import java.io.IOException;
/**
* An executable that counts the words in a files and prints out the counts in
* descending order. You will need to modify this file.
*/
public class WordCount {
private static void countWords(String file) {
DataCounter<String> counter = new BinarySearchTree<String>();
try {
FileWordReader reader = new FileWordReader(file);
String word = reader.nextWord();
while (word != null) {
counter.incCount(word);
word = reader.nextWord();
}
} catch (IOException e) {
System.err.println("Error processing " + file + e);
System.exit(1);
}
DataCount<String>[] counts = counter.getCounts();
sortByDescendingCount(counts);
for (DataCount<String> c : counts)
System.out.println(c.count + " " + c.data);
}
/**
* TODO Replace this comment with your own.
*
* Sort the count array in descending order of count. If two elements have
* the same count, they should be in alphabetical order (for Strings, that
* is. In general, use the compareTo method for the DataCount.data field).
*
* This code uses insertion sort. You should modify it to use a different
* sorting algorithm. NOTE: the current code assumes the array starts in
* alphabetical order! You'll need to make your code deal with unsorted
* arrays.
*
* The generic parameter syntax here is new, but it just defines E as a
* generic parameter for this method, and constrains E to be Comparable. You
* shouldn't have to change it.
*
* @param counts array to be sorted.
*/
private static <E extends Comparable<? super E>> void sortByDescendingCount(
DataCount<E>[] counts) {
for (int i = 1; i < counts.length; i++) {
DataCount<E> x = counts[i];
int j;
for (j = i - 1; j >= 0; j--) {
if (counts[j].count >= x.count) {
break;
}
counts[j + 1] = counts[j];
}
counts[j + 1] = x;
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: filename of document to analyze");
System.exit(1);
}
countWords(args[0]);
}
}
------------------------------------------------------------------
/**
* An example of how to test your binary search tree. You should use this as
* inspiration for your unit tests.
*/
public class TestBinarySearchTree {
public static void main(String[] args) {
boolean dumpall = false, notest = false;
if (args.length > 1
|| (args.length == 1 && args[0].compareTo("dump") != 0 && args[0]
.compareTo("notest") != 0)) {
System.err.println("Arguments: [dump] to dump all output");
System.err.println(" [notest] to skip tests");
System.err.println("No arguments justs tests output");
return;
}
if (args.length == 1) {
dumpall = true;
if (args[0].compareTo("notest") == 0)
notest = true;
}
String[][] tests = {
{ "g", "h", "a", "b", "a", "h", "j", "e", "e", "f" },
{ "5", "3", "1", "2", "7", "6", "0", "8", "9", "4", "3", "5",
"0", "9" }, {} };
String[][] expected = {
{
"([g,1] . .)",
"([g,1] . ([h,1] . .))",
"([g,1] ([a,1] . .) ([h,1] . .))",
"([g,1] ([a,1] . ([b,1] . .)) ([h,1] . .))",
"([g,1] ([a,2] . ([b,1] . .)) ([h,1] . .))",
"([g,1] ([a,2] . ([b,1] . .)) ([h,2] . .))",
"([g,1] ([a,2] . ([b,1] . .)) ([h,2] . ([j,1] . .)))",
"([g,1] ([a,2] . ([b,1] . ([e,1] . .))) ([h,2] . ([j,1] . .)))",
"([g,1] ([a,2] . ([b,1] . ([e,2] . .))) ([h,2] . ([j,1] . .)))",
"([g,1] ([a,2] . ([b,1] . ([e,2] . ([f,1] . .)))) ([h,2] . ([j,1] . .)))",
"a,2 b,1 e,2 f,1 g,1 h,2 j,1 " },
{
"([5,1] . .)",
"([5,1] ([3,1] . .) .)",
"([5,1] ([3,1] ([1,1] . .) .) .)",
"([5,1] ([3,1] ([1,1] . ([2,1] . .)) .) .)",
"([5,1] ([3,1] ([1,1] . ([2,1] . .)) .) ([7,1] . .))",
"([5,1] ([3,1] ([1,1] . ([2,1] . .)) .) ([7,1] ([6,1] . .) .))",
"([5,1] ([3,1] ([1,1] ([0,1] . .) ([2,1] . .)) .) ([7,1] ([6,1] . .) .))",
"([5,1] ([3,1] ([1,1] ([0,1] . .) ([2,1] . .)) .) ([7,1] ([6,1] . .) ([8,1] . .)))",
"([5,1] ([3,1] ([1,1] ([0,1] . .) ([2,1] . .)) .) ([7,1] ([6,1] . .) ([8,1] . ([9,1] . .))))",
"([5,1] ([3,1] ([1,1] ([0,1] . .) ([2,1] . .)) ([4,1] . .)) ([7,1] ([6,1] . .) ([8,1] . ([9,1] . .))))",
"([5,1] ([3,2] ([1,1] ([0,1] . .) ([2,1] . .)) ([4,1] . .)) ([7,1] ([6,1] . .) ([8,1] . ([9,1] . .))))",
"([5,2] ([3,2] ([1,1] ([0,1] . .) ([2,1] . .)) ([4,1] . .)) ([7,1] ([6,1] . .) ([8,1] . ([9,1] . .))))",
"([5,2] ([3,2] ([1,1] ([0,2] . .) ([2,1] . .)) ([4,1] . .)) ([7,1] ([6,1] . .) ([8,1] . ([9,1] . .))))",
"([5,2] ([3,2] ([1,1] ([0,2] . .) ([2,1] . .)) ([4,1] . .)) ([7,1] ([6,1] . .) ([8,1] . ([9,2] . .))))",
"0,2 1,1 2,1 3,2 4,1 5,2 6,1 7,1 8,1 9,2 " },
{ "<empty tree>", "No Data" } };
boolean error = false;
for (int i = 0; i < tests.length; i++) {
BinarySearchTree<String> tree = new BinarySearchTree<String>();
for (int j = 0; j < tests[i].length; j++) {
tree.incCount(tests[i][j]);
String out = tree.dump();
if (notest || out.compareTo(expected[i][j]) != 0)
error = true;
if (dumpall)
System.out.println(out);
}
if (tests[i].length < 1) {
String out = tree.dump();
if (notest || out.compareTo(expected[i][0]) != 0)
error = true;
if (dumpall)
System.out.println(out);
}
DataCount<String>[] cnt = tree.getCounts();
String out = "";
if (cnt != null && cnt.length > 0)
for (int j = 0; j < cnt.length; j++)
out += cnt[j].data + "," + cnt[j].count + " ";
else
out = "No Data";
if (notest
|| out.compareTo(expected[i][expected[i].length - 1]) != 0)
error = true;
if (dumpall)
System.out.println(out);
}
if (!notest) {
if (error)
System.out.println("Test failed!");
else
System.out.println("Test passed.");
}
}
}
------------------------------------------------------------
/**
* TODO Replace this comment with your own.
*
* Stub code for an implementation of a DataCounter that uses a hash table as
* its backing data structure. We included this stub so that it's very clear
* that HashTable works only with Strings, whereas the DataCounter interface is
* generic. You need the String contents to write your hashcode code.
*/
public class HashTable implements DataCounter<String> {
/** {@inheritDoc} */
public DataCount<String>[] getCounts() {
// TODO Auto-generated method stub
return null;
}
/** {@inheritDoc} */
public int getSize() {
// TODO Auto-generated method stub
return 0;
}
/** {@inheritDoc} */
public void incCount(String data) {
// TODO Auto-generated method stub
}
}
------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
/**
* FileWordReader reads words from a file one-by-one, converting to lowercase
* and eliminating punctuation. You can read a file in using:
*
*/
class FileWordReader {
StreamTokenizer tok;
public FileWordReader(String file) throws FileNotFoundException,
IOException {
tok = new StreamTokenizer(new BufferedReader(new InputStreamReader(
new FileInputStream(file))));
tok.eolIsSignificant(false);
tok.lowerCaseMode(true);
tok.wordChars('a', 'z');
tok.wordChars('A', 'Z');
String ws = " .,!;:-[].,;!?*+-=\|/(){}"[]><'";
for (int i = 0; i < ws.length(); i++) {
tok.whitespaceChars(ws.charAt(i), ws.charAt(i));
}
}
public String nextWord() throws IOException {
int ttype = tok.nextToken();
while (ttype != StreamTokenizer.TT_WORD
&& ttype != StreamTokenizer.TT_EOF)
ttype = tok.nextToken();
if (ttype == StreamTokenizer.TT_EOF)
return null;
return tok.sval;
}
};
Explanation / Answer
Below will be the code for hashtabe class, i assume, this is what you require since all other code seems to be complete:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.