![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
This program enhances the MapExample program from the course notes 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
Tasks
Either start with the skeleton code or create a WordCount class.
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.
Complete the addWords() method to count each word returned by the StringTokenizer. The program already separates each line in the URL into individual words.
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 course notes.
Now that you have a case-insentive Comparator, use it to create a TreeMap full of the original map contents. That way, the output can be displayed sorted.
After getting an Iterator of all the keys, display out 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.
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 ever wish to clear out the map, press the Clear button. Where help exists, the task numbers above are linked to the step-by-step help page. Solution SourceDemonstration
When you run the Tester program from the command line and have the program read from the default URL (http://www.magelang.com), you'll see the following screen: ![]() Your output may differ if the page has changed, you formatted the output differently, or you retrieved the contents of a different URL. Magercises Short CourseCopyright © 1999 MageLang Institute. All Rights Reserved. |