Computer Science 2311
Spring 2005
Programming Assignment 5

Date assigned: 2/9/05


Successive refinement

This program is partly an exercise in the use of arrays, but is also an exercise in program development.

Successive refinement is a development method where you write a little bit of a program and then test it before moving on. Test your methods. You do not need to make everything work before testing. See if what you have written so far is working. Do not write the whole program before trying it..


Talking like a pirate

This assignment has you write a program to read a text document and to make some changes to it so that it sounds more like "pirate" talk. For this assignment, the changes will be small.

For example, if the input is

  There is a star on the horizon.  It is
  bright.
then the output will be
  There be a star on the horizon.  It be
  bright.


Input

Read the input from a file called "document.txt". Up until now, you have been reading from the keyboard. To read from a file, you will want to create an object that is tied to the file. Use

  FileReader reader = new FileReader("document.txt");
This throws an exception called FileNotFoundException, so you will need a throws phrase in the heading of any method that creates the new file reader. Do not create a new file reader every time you want to read a character. Create the FileReader once.

A class is provided that is similar to the Scanner class described in the book. Get it and add it to your project. To create a new scanner, use

  Scanner scan = new Scanner(reader);
to convert the reader constructed above into a scanner called scan. This Scanner class does not provide everything that the Scanner class described in the text does, but we only need a few methods.
scan.read( )

This is an expression that returns an integer. If there is a character to be read, then scan.read( ) returns the integer code of that character. To convert the integer code to type char, use (char)(scan.read( )).

If there are no characters left, then scan.read( ) returns -1. Use that to detect the end of the file.

scan.backup( )

This puts the most recently read character back into the input, so tht it can be read again. You can only do this once before you read another character.

scan.eof( )

This is an expression of type boolean that is true if there are no more characters in this file, and returns false if there is at least one more character.

scan.close( )

This closes the file. Use it when you are done reading.


Output

Write the output to the console.


Part 1

For this assignment, you are to use arrays of characters to read strings. Start by writing a static method called readWord.

readWord(scan)

This should read a word consisting of letters, and should return it as a String. Do this by using the scan object to read one character at a time. Create an array of characters. You can assume that a word has no more than 100 characters.

Each time you read a letter, put it into an array. Keep track of the number of characters that you have put into the array. Stop as soon as you see a character that is not a letter, and put that character back. But be careful that you never put -1 back.

You can test whether a character is a letter by checking if its code is between the codes for 'a' and 'z', or between the codes for 'A' and 'Z'.

Once you have an array of characters, convert it to a string using a constructor new String(a,start,len) that produces a string consisting of len characters from array a, starting at index start. So

    String s = new String(a, 0, n);
  
makes s be the first n characters in array a.

Important note. Your readWord method is responsible for reading, not for writing. It must not print anything.

Now test your function. Create a main program that creates a scanner and reads a word from "document.txt". Put a few words into file "document.txt". Now try to read a word. Print the string that you get. Did it work?


Part 2

Now write a function called readNonword that works similarly to readWord, but reads characters that are not letters. Test it.


Part 3

Now write a main program that repeatedly reads a word, then a nonword, then a word, then a nonword, etc., until it hits the end of the file. It should print each string that it reads, including both words and nonwords. Test this. Does it work?


Part 4

Now write a method called pirateWord that takes a string and returns the pirateWord of that string. This method should just check whether its parameter is "is", "are", "Is" or "Are". If it is one of those, it should return "be" or "Be", as appropriate. It the parameter is anything else, then pirateWord should just return its parameter.

Note. Your pirateWord method must not read anything from the scanner, and should not print anything. That is not its job.

Modify your main program so that it gets the pirate-word of each word, and prints that instead of the original word. Test it. Does it work?

When you test a program, put some stress on it. You need to change your focus from wanting it to work to wanting to find any errors, if there are any. Put something in document.txt that will test every line that you have written. If your test does not run one of the lines, then it cannot possibly detect an error in that line.


What to turn in

Turn in your completed program and a printout of the document.txt file that you used to test it. You will be graded down for a poor test.