JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table. Finally, it prints out how long it took to fill each symbol table.
The program should consist of five sections:
Please format the output so that one can see what the values represent, for example, by printing the type of each symbol table and the number of seconds elapsed for filling it.
Please test your program on the text file in data/tale.txt. You should see differences in the times.
package csc403; import algs31.SequentialSearchST; import algs32.BST; import stdlib.StdIn; import stdlib.Stopwatch; public class TimeSymbolTables { final static String FILE_NAME = "tale.txt"; public static void fillSequentialSearchTable(SequentialSearchST<String, Integer> symbolTable) { StdIn.fromFile("data/" + FILE_NAME); while(StdIn.hasNextLine()) { String line = StdIn.readLine(); // split the line by any number of whitespace characters String parts[] = line.split("\\s+"); // if there are 2 parts retrieved for(String p: parts) { // p is a word if(!symbolTable.contains(p)) { symbolTable.put(p, 0); } symbolTable.put(p, symbolTable.get(p) + 1); } } } public static void fillBSTTable(BST<String, Integer> symbolTable) { StdIn.fromFile("data/" + FILE_NAME); while(StdIn.hasNextLine()) { String line = StdIn.readLine(); // split the line by any number of whitespace characters String parts[] = line.split("\\s+"); // if there are 2 parts retrieved for(String p: parts) { // p is a word if(!symbolTable.contains(p)) { symbolTable.put(p, 0); } symbolTable.put(p, symbolTable.get(p) + 1); } } } public static void main(String[] args) { SequentialSearchST<String, Integer> symbolTable1 = new SequentialSearchST<String, Integer>(); BST<String, Integer> symbolTable2 = new BST<String, Integer>(); Stopwatch sw1 = new Stopwatch(); fillSequentialSearchTable(symbolTable1); double timeTook1 = sw1.elapsedTime(); Stopwatch sw2 = new Stopwatch(); fillBSTTable(symbolTable2); double timeTook2 = sw2.elapsedTime(); System.out.println("Time took to fill SequentialSearch symbol table: " + timeTook1); System.out.println("Time took to fill BinarySearch symbol table: " + timeTook2); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.