/*****************************************************************************
* Name: *
* NetID: *
* Precept: *
* *
* Description: Counts the repeated occurrences in a list of Strings. *
* *
* Example: *
* % java-introcs FrequencyTable *
* duck duck goose[Ctrl+D] *
* 2 duck *
* 1 goose *
* *
* Remarks: *
* - This is book exercise 4.4 #36 (Booksite Creative Ex 4.4 #5) *
* - Use with mobydick.txt, available on the precepts page. *
*****************************************************************************/
public class FrequencyTable {
// maintain counts of all words seen so far
// the key is the word and the value is the count
private ST<_______________> st = _________________________;
// remark: we have not declared a constuctor! but Java lets every class
// have a no-argument constructor by default. It only runs the line of
// code above (instance variable initialization).
// increment the frequency of this word
public void click(String word) {
int count = count(word);
st.put(word, count + 1);
}
// what is the frequency of this word?
public int count(String word) {
if (!st._____(___________)) return 0; // if word is not in ST
else return _____.______(__________); // get word's count
}
// print words and their frequencies
public void show() {
// remark: this is called a "foreach loop" or an "enhanced for loop."
// it goes through all keys in alphabetical order. this is the only
// way to traverse a symbol table!
for (String word : st.keys()) {
// print frequency and word, separated by a space
StdOut.println(_______________(______) + " " + ___________);
}
}
// count all words in StdIn
public static void main(String[] args) {
FrequencyTable freq = new FrequencyTable();
// build frequency table from words on standard input
while (!StdIn.isEmpty()) {
String word = StdIn.readString();
freq.________(word);
}
// print frequency table to standard output
freq.show();
}
}