Computer Science 210 Lab 4: Away with Loops!!!
Due: October 1, 1997

Objectives and Overview: Recursion is a very valuable tool in computing. In this lab, you will develop experience writing recursive methods, as well as implementing recursive problem solutions. This lab also provides experience with Vector sorting method and reading input from text files instead of the keyboard. Here's a summary of the problem to be solved:

First, you will reimplement the Vector methods by replacing all loops by recursive calls. Then you will design a program called WordFreqSort that takes its words from the unknown-sized text file named literacy.notes1 and displays each of the words that appears in that file and its frequency, but in decreasing order of frequency. For example, if the text file happened to contain the following words,

the flea in the fly in the flu

then your program should display the following output, with the most frequently occurring words appearing first:

the occurs 3 times.
in occurs 2 times.
flea occurs 1 times.
fly occurs 1 times.
flu occurs 1 times.

Optionally, your main program itself may be revised so that it contains no loops -- only calls to recursive methods.

Part 1 - Redesign the Vector Methods Using Recursion

Start CodeWarrior and create a new project called Lab4.µ on the desktop. Add the WordFreq.java program file to your project from the examples folder, along with the four additional files shown below (which can be found in the source folder); copies of these files should be dragged to the desktop before adding them to your project.

Now redesign your copy of the Vector.java file by replacing the following methods with equivalent methods that are defined recursively:

insertElementAt (int index)
removeElement (Object element)
removeElementAt (int where)

Now run this program to be sure that the Vector class still works correctly. Recall that the following actions need to be taken with your project Lab4.µ before running the program. 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 - Expand your WordFreq Program

    The insertionSort method inside the VectorInsertionSort class has been revised so that it sorts Vectors of Associations rather than arrays of integers. The following call will sort the Vector v os size n into descending order of the integer values of all the Associations in Vector v.

    VectorInsertionSort.insertionSort(Vector v, int n); 

    Now add a copy of that class (it's called VectorInsertionSort.java in the examples folder), and add to your WordFreq program an appropriate call that will sort the Vector vocab into descending order of frequency, so that the most frequently occurring words appear at the beginning.

    Part 3 - Redirect the Input Stream

    Input can be taken from a text file, rather than from System.in, by making the following simple changes in a program:

  • Add the following line at the beginning of the program:
  • import java.io.*;
  • Enter the name of the text file (e.g., literacy.notes1) to be read by the program in the text area
  • Edit -> WordFreq Settings -> Java Target -> Parameters
  • Drag the text file (e.g., literacy.notes1) to be read by the program into the Lab4 folder.
  • Redirect the ReadStream variable (i.e., r) so that it points to this input text file, rather than System.in, in the following way:
  • try {
    InputStream istream = new FileInputStream(args[0]);
    ReadStream r = new ReadStream(istream);
    ...
    <the same old input reading loop goes here>
    ...
    }
    catch (IOException e) {
    System.err.println(e);
    return;
    }
    Note that the parameter args[0] is used at run time to connect the program with the file literacy.notes1 when the input stream istream is declared and initialized.

    Part 3 - Run Your Revised WordFreqSort Program

    The final stage of this problem is to complete the revision of your WordFreq program so that, in addition to performing the functions of the original program, it does the following:

  • Uses your redesigned Vector class, with recursive methods.
  • Sorts, using the redesigned InsertionSort class, the list of words and frequencies before displaying it.
  • Obtains its input text from the file named literacy.notes1, which contains an unknown number of words.
  • (Optionally) contains no loops itself -- where there were loops in the original program, calls to recursive methods should appear instead.
  • For this particular input file, literacy.notes1, the first few lines of output from your program should be:

    Lab 4 Deliverables:

    Submit your Java programs from Parts 1 and 3 of this lab by dragging them to the CS210/Tucker -> Drop Box folder. Don't forget to rename these filesby affixing your name to them. For example, if I were submitting them, the files would be called WordFreqSort.atucker.java and Vector.atucker.java.

    Also, hand in hard copy listings of your program and your redesigned Vector class with your name on them, along with your answers to the following questions.

    1. What is the complexity of your recursive insertElementAt method for the Vector class? Explain.
    2. What is the complexity of your recursive insertElement method? Explain.
    3. What is the complexity of your recursive removeElementAt method? Explain.
    4. What is the complexity of each of the remaining Vector methods? Explain.
    5. What is the overall complexity of your revised WordFreq program? Explain.

    You may work on the programming parts of this lab in teams of two, but all answers to these questions should be developed individually.