YCP Logo Lab 16: Word Count

Due: Friday, Nov 7th by 11:59 PM

Getting started

Import CS201_Lab16.zip (File->Import...->General->Existing Projects into Workspace->Archive File). You should see a project called CS201_Lab16 in the Package Explorer.

Your Task

Complete the program so that it counts the number of occurrences of words in the text file whose name is entered by the user, and then prints out a histogram showing the number of occurrences of each word (in lower case), listing the words alphabetically.

Code is provided which

  • opens the file
  • reads each line of the file
  • extracts each word (sequence of letters) from the lines of the file, converting each word to lower case

An example text file, gettysburg.txt, is provided for you to use when testing your program.

Here is partial output from running the program on this file:

Read which file? gettysburg.txt
Word counts:
a          : =======
above      : =
add        : =
advanced   : =
ago        : =
all        : =
altogether : =
and        : ======
any        : =
are        : ===
as         : =
battle     : =

The length of the "bar" following each word indicates the number of occurrences of that word. For example, the word "and" occurred 6 times, so there are six "=" characters making up the bar for that word.

There are 126 more lines of output.

Hints

Use a map with key type String and value type Integer to count the number of occurrences of each word.

You are required to list the words alphabetically when displaying the histogram. Can you choose a built-in implementation of the Map interface that will make this job easier?

You should determine the length of the longest word so that the "bars" of the histogram will all start in the same column when printed.

Submitting

When you are done, save the project (CS201_Lab16) to a zip file by right-clicking it and choosing

Export...->Archive File

Upload the saved zip file to the Marmoset server as lab16. The server URL is

https://camel.ycp.edu:8443/