This program enhances the program from Map interface to read from a URL, instead of just counting words from the command line.
If you aren't familiar with the Swing component set, don't worry. The Tester
program includes all the necessary code to create the user interface. You are only responsible for counting the words and formatting the output. Even the source code to read from the URL is provided.
Skeleton Code
Task 1:
Either start with the skeleton code or create a WordCount
class.
Help for task 1:
Shift click to save the file to your working directory.
If you don't start from the skeleton code, you'll have to read the URL yourself and parse the contents with a StringTokenizer
.
Task 2:
Create an instance variable of type Map
and assign it to a new HashMap
. In the getMap()
method return the map created. In the clear()
method, empty out the defined map.
Help for task 2:
Use the clear()
method of Map
to empty it out.
Task 3:
Complete the addWords()
method to count each word returned by the StringTokenizer
. The program already separates each line in the URL into individual words.
Help for task 3:
The value for the key (word) is the current frequency. If the word is not found, then it is not in the map yet and should be added with a value of one. Otherwise, add one to the existing frequency.
Refer back to the map usage example in Map interface.
Feel free to try a different set of delimiters with the StringTokenizer
.
Task 4:
The Tester
program has a JTextArea
to display the results of the counting. The program displays the String
returned by the private convertMap()
method in the JTextArea
. It is your job to format the output nicely, as the toString()
of AbstractMap
displays everything on one line. Start off with the skeleton code for CaseInsensitiveComparator
so you can sort the output in a case-insensitive manner. The implementation will be identical to the comparator interface described in Sorting.
Help for task 4:
Shift click to save the file to your working directory.
Either implement compare()
yourself, or copy it from the course notes.
Task 5:
Now that you have a case-insensitive Comparator
, use it to create a TreeMap
full of the original map contents. That way, the output can be displayed sorted.
Help for task 5:
Getting the original Map
sorted with the new Comparator
is a two-step process. In the TreeMap
constructor, specify the Comparator
. Then, put all the original map entries in the TreeMap
with the putAll()
method.
Task 6:
After getting an Iterator
of all the keys, display one key-value pair per line, using the predefined PrintWriter
to format the output. It is backed by a StringBuffer
and will be automatically returned.
Help for task 6:
First get the entry set with the entrySet()
method.
Then, get its Iterator
. Each element is a Map.Entry
Task 7:
Compile your program and run the Tester
program to see what happens. You specify the URL to read in the JTextField
. When you press Enter, the URL is read and the words are added to the map. Once done reading, the JTextArea
is updated. If you want to clear out the map, press the Clear button.