Project 7: Concurrency

This goal of this project is to practice writing code that uses threads, especially for coordinating use of shared resources and interacting with GUI components correctly.

1. Introduction

The first thing to know about this project is that quite a bit of the code is given to you, and the amount of code you'll need to write in terms of number of lines is fairly small---but you'll need to think very carefully about the things we've talked about this week to get those lines right.

(When I say "the number of lines is fairly small", here are the specifics: When I took my solution to this project and deleted lines from it to make the starter code to give you, I got rid of just 35 lines of code. The does not include comments or white space, of course, but it does include simple lines like declarations of instance variables, initializing instance variables in the constructor, and closing curly braces.)

The premise of this program is that it counts occurrences of chosen words on a web page. Here is a screen shot of the program:

At the top, the user can enter the url for a web page. In the rest of the window are places where the user can enter up to ten words that he or she wants counted. Every time the user wants to track a different word, he or she can enter a new word in one of the fields and press "Reset", which will read the word in the field and reset its count to 0. Every time the user wants to analyze a new website, he or she can enter a new url in the top field and press "Go". This will not reset the word counts but will continue adding from where the tally left off---that way someone can use this to see the total count of words over several web pages.

2. Set up

Copy the starter code from the course directory:

cp -r ~tvandrun/Public/cs245/proj7/* .

You will find files for the following classes:

The action listener classes are the only ones you need to modify (although you may decide to write more classes to support these). Do not modify or turn in the other Java files. You don't need to know much about how the other classes work---you need only know how to interact with URLWordIterator and Model.

3. A class to use: Model

Open Model.java and inspect it. It keeps track of the current state of the tally. It has parallel arrays of a given size (which in the driver program is always set to 10): words[i] contains the ith word and counts[i] contains the tally of the times we have seen the ith word. One can start over at a position with resetWord(), which will put a new word in place of the ith word and set its count at 0. markWord() increments the count for a given word (note that it takes a word and not an index; you can call that method with a word we're not tracking with no harm done).

Modifying the model will not automatically cause the changes to be displayed on the window. The method modifyDisplay() brings what appears on the screen up to date with what is in the mode.

4. A class to use: URLWordIterator

All the work of picking out the words from a web page are done for you. All you need to do with this class is pass a URL (as a string) to the constructor and it will act like an iterator returning all the alphabetic character sequences from that page. (If you want to see how it's done, you may inspect that code, but that's not necessary for doing this project.) Note that this will also return html tags (with the angle brackets and other non-letters removed).

5. A class to finish: ResetListener

The ResetListener is given a JTextField from which it can read the word indicated by the user, the index in the model that it corresponds to, and a reference to the model. When the action is performed, it must read from the JTextField modify the model so that it is tracking the new word, and update the display. See "hard parts" below. The class will get instantiated once for every reset button--that is already done for you in the WordCompWindow class; you do not need to instantiate this class ever.

6. A class to finish: GoListener

The GoListener is given a JTextField from which it can read the URL indicated by the user and a reference to the model. When the action is performed, it must read from the JTextField to get the URL, iterate through the words in that webpage using URLWordIterator, and modify the model appropriately, finally updating the display. See "hard parts" below. The class will be instantiated once for the go method; this is done for you in the WordCompWindow class; you do not need to instantiate this class ever.

7. The hard parts

At first glance everything mentioned above should seem pretty straightforward. BUT...

To do all this you'll need to write several runnables. You may chose to do this using anonymous inner classes (which is what I did), or you could write stand-alone classes.

8. Turn in

Copy the listener classes and any other classes you may have written to support these to the turn-in directory for this project:

cp (some file) /cslab.all/ubuntu/cs245/turnin/(your user id)/proj7

DUE: 5:00 pm Tuesday, Nov 25, 2014.


Thomas VanDrunen
Last modified: Wed Nov 12 16:42:26 CST 2014