Question: Using Java and the standard input (stdlib library) I am supposed to create a symbol table that will contain as a key a word read
Using Java and the standard input (stdlib library) I am supposed to create a symbol table that 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. The program should fill the symbol table with these counts, keeping track of how long it takes using a Stopwatch object. Finally, it prints out the symbol table and the amount of time it took to fill it. I've included my code (which doesn't work) as well as the SequentialSearchST class. The TimeSymbolTables main method should be built using StdIn and should avoid the scanner object. I would like this method complete using StdIn.readAllStrings().
(Here is additional information on the StdIn library: https://introcs.cs.princeton.edu/java/stdlib/).

Thank you!!
package program2;
import algs31.SequentialSearchST;
import algs32.BST;
import stdlib.*;
public class TimeSymbolTables {
public static void main(String[] args) {
//StdIn.fromFile("data/tale.txt");
StdIn.fromFile("data/tinyTale.txt");
int count = 0;;
SequentialSearchST
Stopwatch swWordCountTest = new Stopwatch();
String[] words = StdIn.readAllStrings();
int value = 0;
for (int i = 0; i
if (!wordCountTest.contains(words[i])) {
wordCountTest.put(words[i], null);
} else {
}
StdOut.println("[" + words[i] + "," + wordCountTest.get(words[i]) + "]");
}
}
SEQUENTIAL SEARCH....
public class SequentialSearchST
private int N; // number of key-value pairs
private Node
// a helper linked list data type
private static class Node
public final K key;
public V val;
public Node
public Node(K key, V val, Node
this.key = key;
this.val = val;
this.next = next;
}
}
// return number of key-value pairs
public int size() { return N; }
// is the symbol table empty?
public boolean isEmpty() { return size() == 0; }
// does this symbol table contain the given key?
public boolean contains(K key) {
return get(key) != null;
}
// return the value associated with the key, or null if the key is not present
public V get(K key) {
for (Node
if (key.equals(x.key)) return x.val;
}
return null;
}
public V getR(K key) {
return getR(key, first);
}
private V getR(K key, Node
if (x == null) return null;
if (key.equals (x.key)) return x.val;
return getR(key, x.next);
}
// add a key-value pair, replacing old key-value pair if key is already present
public void put(K key, V val) {
if (val == null) { delete(key); return; }
for (Node
if (key.equals(x.key)) { x.val = val; return; }
first = new Node(key, val, first);
N++;
}
// remove key-value pair with given key (if it's in the table)
public void delete(K key) {
first = delete(first, key);
}
// delete key in linked list beginning at Node
// warning: function call stack too large if table is large
private Node
if (x == null) return null;
if (key.equals(x.key)) { N--; return x.next; }
x.next = delete(x.next, key);
return x;
}
// return all keys as an Iterable
public Iterable
Queue
for (Node
queue.enqueue(x.key);
return queue;
}
/* *********************************************************************
* Test client
**********************************************************************/
public static void main(String[] args) {
StdIn.fromString ("S E A R C H E X A M P L E");
SequentialSearchST
for (int i = 0; !StdIn.isEmpty(); i++) {
String key = StdIn.readString();
st.put(key, i);
}
for (String s : st.keys())
StdOut.println(s + " " + st.get(s));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
