Question

JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...

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:

  1. Create a String array of the words in the text file using the methods StdIn.fromFile() and StdIn.readAllStrings().
  2. Create three symbol table objects, one each of the following classes:
    • SequentialSearchST
    • BinarySearchST
    • BST
  3. Write a loop for each of the symbol table objects in which the code iterates through the String array created in step 1, adding to and updating words in the symbol table. Just before the iterating loop, create a Stopwatch object. Just after the bottom of the loop, use the elapseTime() method to get and print the amount of time.

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements:...
Program Instructions Write a program called censor.py. This program will have the following functionality and requirements: Has a main function: That asks the user to input a sentence Then asks the user to enter a list of words they want to replace (separated by spaces) Calls a function called replaceMultiWords passing those two parameters, which returns a filtered string Prints out the filtered (censored) string to the console Has a replaceMultiWords function that: Takes a string "sentence" and a string...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a...
Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a frequency. You are given a file where each line contains a note name, a tab character, and a frequence, which is a floating point value. For example, here are the first few lines from the file notes_frequencies.txt: A0 27.5 A#0 29.1353 B0 30.8677 C1 32.7032 C#1 34.6479 D1 36.7081 D#1 38.8909 E1 41.2035 F1 43.6536 Playing chords Write a program named PlayChords that first...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...