Computer Science 210 Lab 2: Making Arrays Flexible: the Vector Class
Due: September 16, 1997

Objectives and Overview: In this lab we will study the properties of "Vectors," which have all the properties of arrays and whose sizes are adjustable. The implementation details of the Vector class will also be examined, where we will gain insight into dynamic storage allocation and some of the related class concepts of Java.

Part 1 - Build and Run the WordFreq Program

The program file WordFreq.java appears on page 33 of your text. An electronic copy of that program is also in the Courses -> CS210(Tucker) -> examples folder, for your use. Make a copy of that program on the desktop for yourself and add a new line to it, as shown in the partial listing below:

Now start CodeWarrior and create a new project called Lab2.µ on the desktop. Add the WordFreq.java program file to your project, along with the four additional files shown below. These four files can be found in the Courses -> CS210 (Tucker) -> source folder; copies of these files should be dragged to the desktop before adding them to your project.

The program WordFreq.java counts the number of occurrences (called the "frequency") of each individual word in an input text, as discussed in your text and in class, and displays these counts. It uses the Vector named vocab to store each word and its frequency count. It uses the Association named wordInfo to record each single word-frequency count pair within that Vector.

To run this program, recall that the following actions need to be taken with your project Lab2.µ. Select TrivialApplication Settings under the Edit menu, and a settings window will appear. Now make three changes in this window:

  • Select Java Target and type "WordFreq" as the Main Class. This is the class that will execute when your program is run.
  • Change Target Settings -> Target Name to "WordFreq".
  • Change Linker Output -> Type to "Droplet" from its current setting.
  • Now close this window and Make and Run your project. The output should appear in the Java Output window after you type a text into the System.in window. For example, if the input text is

    the flea in the fly in the flu <command-D>

    the program will give the following output:

    Part 2 - Examine this Program and the Assert and Vector Classes

    To become familiar with this program and the key classes that it uses, answer the following questions.

    1. Run this program for each of the following inputs and record the values of the variables i, wordInfo, and f just before the final for loop in this program begins to execute. An easy way to do this is to turn on the Debugger and set a "stop sign" on the left of this for statement in the Debugger window. All the variables used in this program will appear in the upper righthand corner of this window, and their values will change as each step of the program is executed.
    2. Answer question 2.2 on page 27 of your text.
    3. Answer question 2.6.
    4. Answer question 3.1 on page 44.
    5. Answer question 3.3 on page 45. Look at the possibility of using other Vector methods to achieve the same effect as the trimToSize method (see p 546 in Java in a Nutshell for a full description of the Vector class).

    Part 3 - Develop a Program Using the Vector Class

    The problem is to write a program that finds, in any input text, the most frequently-occurring word or words and the number of times that word (or those words) occur. For instance, if the input were as shown in the above example, the output would be:

    The most frequently occurring words are
    the
    And their frequency is 3

    However, in the case of ties for the top spot, all words that appear most frequently should be displayed. For example, if the input were

    flea in the fly in the flu

    then the output would be:

    The most frequently occurring words are
    in
    the
    And their frequency is 2

    You may reuse any parts of programs you exercised above, or you may choose to develop this program from scratch. In either case, the Vector and Association classes should play a central role in your solution.

    Lab 2 Deliverables:

    Submit your Java program from Part 3 of this lab by dragging it to the CS210/Tucker -> Drop Box folder. Don't forget to rename your program file by affixing your name to it. For example, if I were submitting it, the program file would be called lab2.atucker.java, not just lab2.java.

    Also, hand in a hard copy listing of your program with your name on it, along with the answers to the questions in Part 2.