Computer Science 101A Lab 5: Text Files and Arrays - Computing a Readability Index
Due: October 1`2, 2000

Objectives and Overview: The purpose of this lab is to gain experience with text files, methods, simple arrays, and computing a readability index for a free-running text.  Before starting this project, the following files should be copied from the CS101a (Tucker) or CS101b (Majercik) folder and dragged inside a fresh copy of the MyProject folder on the desktop: poem, tiny, literacy, and readability.java.

Part 1.  Exercise the Readability Program

The program readability.java, which appears on page 109 of your textbook, is designed to take input data from a text file, compute the number of sentences and the number of words, and then finally compute and display the grade level for which the text is written.  It appears below:

class readability extends basic
{ public static void main (String param[]) throws Exception
  {
  // establish keyboard and screen input and output
    input keyboard = new input();
    output screen = new output();
    screen.writeln("Type file name");
  // establish input and output text files
    input in = new input(keyboard.readword());
    output out = new output("results");
    in.readblanks();
    assume(in.more(), "empty text"); // "assume" checks that the file was
                                     // successfully opened and is nonempty
    int sentences = 0, words = 0;
  // Text: read a series of sentences until the file is empty
    while(in.more())
      { String word = in.readname();
        in.readblanks();
        words = words + 1;
    // Sentence: read a sentence
        while ((in.next()!=':') && (in.next()!='?') && (in.next()!='.'))
      // AnotherWord: read the next word
          { if (in.next()==',')
              { in.readnext(); in.readblanks(); }
            word = in.readname();
            in.readblanks();
            words = words + 1;
          }
        in.readnext(); in.readblanks();
        sentences = sentences + 1;
      }
  // Compute average sentence length and save results
    int length = words / sentences;
    if (length <= 8) out.write ("4th grade");
    else if (length <= 11) out.write ("5th grade");
    else if (length <= 14) out.write ("6th grade");
    else if (length <= 17) out.write ("7th grade");
    else if (length <= 24) out.write ("High school");
    else out.write ("College");
    out.writeln (" level: " + length + " words per sentence");
    in.close(); out.close();
    keyboard.close(); screen.close();
  }
}
 

Taking input from a text file is similar to taking input from the keyboard, except that the user doesn't need to type it every time the program is run.  This program uses the same methods as the familiar ones that you have used for keyboard input (see summary on page 227).  To take input from a text file, the program declares a new file variable as follows:

input in = new input("<filename>");

Here, <filename> denotes the name of the file which will supply the text input.  Then whenever it needs to read, say, the next word in the text file and save it in the String variable word, it says:

String word = in.readname();

Now add this program to your project in place of the test.java file, and run it once for each of the input text files poem, tiny, and literacy.

  1. What is the output for each of these runs?  Where is the output for each of these runs?
  2. Change the program so that it outputs the number of words and sentences, in addition to the current output.
  3. What is the number of words and number of sentences in each of these text files?
  4. What is the role of each of the readname, readnext, readblanks, more, and next methods in this program?

Part 2.  Revise this Program to Use Methods

This program's design is not very modular - it's just one main program with no methods.  Reorganize the program with the methods suggested by the following skeleton:

class readability extends basic
{
public static void main (String param[]) throws Exception
  {
    input keyboard = new input();
    output screen = new output();
    int sentences = 0, words = 0;
// Establish input text file
    screen.writeln("Type file name");
    input in = new input(keyboard.readword());
    in.readblanks();
// Text: read a series of sentences until the file is empty
    while(in.more())
      { String word = in.readname();  // first word of a sentence
        in.readblanks();
        words = words + Sentence(in, screen);   // number of words in a sentence
        sentences = sentences + 1;      // and one more sentence
        in.readnext(); in.readblanks();
      }
    Results(words, sentences);        // save results in a file
  }

// Sentence: read a sentence and return the number of words in it;
//           call AnotherWord for each word in the sentence
static int Sentence(input in, output screen) throws Exception
  {
    int count = 1;

    return count;
  }

// AnotherWord: read the next word and return it
static String AnotherWord(input in) throws Exception
  { String word = "";

    return word;
  }

// Results: compute average sentence length and save results
static void Results (int words, int sentences) throws Exception
  {
    output out = new output("results");

    out.close();
  }
}
 

Part 3.  Design a Word-Length Counting Program

Now adapt this program so that it will count the number of 1-letter words, 2-letter words, 3-letter words, 4-letter words, 5-letter words, and (6+)-letter words (this last one is the number of words with 6 or more letters). Recall that the String class has a method "length" that returns the length of a string. You do not need an array to do this, although you are welcome to use an array. You can, instead, use six different variables to keep track of the counts.   The Results method that you designed in Part 2 of this lab should be adapted so that it displays all this new information-- the output should contain not only the average sentence length but also a list of word lengths (1 through 5, and 6+) and their frequencies. Your output should explain what is being reported; that is, a list of numbers without explanation is not sufficient. Anyone looking at your output should be able to tell exactly what information you are reporting.

Lab 5 Deliverables

By 4pm on the due date, turn in a printed listing of your completed program from Parts 2 and 3, along with your answers to the questions in Part 1.  Also, submit an electronic copy of your completed program from Part 3 as lab5yourname.java to the Drop Box in the CS101a (Tucker) or CS101b (Majercik) folder.

You may work in teams of two on this project.  If so, both team members' names should appear on all work completed jointly.