CompSci 6, Summer 2007
Homework #5
Due: Wednesday, August 8

Important: Code that does not compile (i.e. contains syntax errors) will receive no credit.

Snarf the starter code (Homework5), and complete the following exercises:

  1. Tag Cloud: (This exercise is based on an idea presented by Owen Astrachan at the Friday Visualization Forum last spring.) A tag cloud is a visualization of the words that make up a written document. In this exercise, we will create a simple tag cloud that uses font size to indicate the relative frequencies with which various words appear in the document. For an example of a robust tag cloud implementation, see Tag Crowd.

    A tag cloud is not very effective unless we filter out words that are not indicative of the topic of the document. We will use two criteria to decide whether to ignore a word: (1) our tag cloud only includes words of a specified minimum length, and (2) it allows us to explicitly specify a list of words to ignore. A sample list of words to ignore is included in the text file ignore.txt (this list is based on the list of the 100 commonly used words in English found here, with some additions). For the purpose of this exercise, a word is a sequence of adjacent letters; all symbols and numbers should be ignored. We also do not distinguish between upper and lower case letters.

    The starter code contains three classes related to this exercise. The class TagCloud contains code for actually performing the visualization; this class has already been completed for you. The class WordCounter is responsible for actually opening a text document and keeping track of the frequencies of each of the words in the document. Finally, the TagCloudMain class sets up and runs the program.

    You will need to complete the methods of the WordCounter class according to the Javadoc comments. The countWords method is of particular importance. Here is an outline of how this method should work:

    • Open the file with the given filename.
    • Iterate through all lines in the file. For each line:
      • Convert the line to lower case.
      • Replace all characters that are not letters (i.e. numbers or symbols) with spaces. A simple way to do this is to get each character in the string and then use the expression ('a' <= c && c <= 'z') to check whether it is a letter (here, c is a char variable).
      • Split the line into individual words. You can do this with the split method of the String class, like so: String[] words = line.split("\\s+") (here, \\s+ is a regular expression which represents all white space -- we will not discuss regular expressions further in this course).
      • For each word, check that the word is not in the ignore list and is at least as long as the specified minimum word length. If both of these conditions are true, then update the frequency associated with the word.

    The TagCloudMain program shows how to use the WordCounter and TagCloud classes to visualize the complete text of in The Art of War, which is provided in the art_of_war.txt file. You should also try making tag clouds for other documents (a good source is Project Gutenberg, which is a collection of free electronic books). After trying several different documents, briefly discuss in your README file other techniques we could use to further improve the selection of words that appears in the tag cloud.

  2. Computing Wages: Complete exercise P13.6 in Horstmann. Call the test program WorkerTest.java. Your implementation should make use of polymorphism; this means that when we call the computePay(int hours) method on a Worker variable, the amount computed should be correct for the actual type of object referenced, even if we don't know its concrete class type.

    (Hint: it may be helpful to review how to call a superclass method from within an overriden method in a subclass -- see Chapter 13.3.)

  3. Bouncing Smiley Faces: (You will need your code from the Circles animation project for this exercise; if you need a copy of the code you've submitted, let me know.) Create a subclass of the Circle class called SmileyFace, which behaves exactly as a circle does except that it draws a smiley face instead of a circle. Your smiley face can look however you'd like, except that it must have at the very least a mouth and two eyes (what methods in the Graphics class would be useful for this?).

    Modify your CircleCanvas code so that it has some smiley faces in addition to circles bouncing around. You should do this by modifying as little code as possible.

  4. Making Change: The starter code contains skeleton code for a class ChangeMaker, which you must complete. The ChangeMaker class contains no instance variables, and contains a single method makeChange(int amount), which should return a String that contains the fewest number of quarters, dimes, nickels, and pennies that are needed to make the given amount of money (in cents).

    Although it is possible to solve this problem using iteration, you must use a recursive solution for this exercise. You may use a helper method, if necessary (see section 18.3). Also, notice that the class contains two final static arrays that contain the names and values of each coin that is used to make change, which you should use in your implementation. (If you are not familiar with the way these arrays are being initialized, see Advanced topic 8.1 on page 284.)

    Write a program, ChangeTester.java which prompts the user to enter a positive number of cents and then uses a ChangeMaker object to create change for the given amount.

    Enter the number of cents: 99
    99 cents is 3 quarters, 2 dimes, 0 nickels, and 4 pennies.
    
    Enter the number of cents: 100
    100 cents is 4 quarters, 0 dimes, 0 nickels, and 0 pennies.
    
  5. Recursive Art (Extra Credit): Write a graphical program that uses recursion to create an artistic design. Examples of student and faculty designs from previous iterations of the course can be found here. (Refer to previous graphical program we designed in this class for details on how to create a canvas on which you can draw and how to add it to a frame.)

    The extra credit awarded will be based on both the creativity of the work and the simplicity of the algorithm. You should include some details about your work in your README file, including a title and a description of how the algorithm works.

    Do not attempt the extra credit until you have completed the rest of the exercises and are confident that your code is correct and well-designed.

Submit your programs using Ambient by the end of the day Wednesday, August 8. The target folder is called hw5. Be sure to include a README file as specified in the submission instructions.