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:
Optionally, your main program itself may be revised so that it contains no loops -- only calls to recursive methods.
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:
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:
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:
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.
Input can be taken from a text file, rather than from System.in, by making the following simple changes in a 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:
For this particular input file, literacy.notes1, the first few lines of output from your program should be:
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.
You may work on the programming parts of this lab in teams of two, but all answers to these questions should be developed individually.