Computer Science 2311
Fall 2009
Lab Assignment 6

Date assigned: 10/06/09


Successive refinement

This program is partly an exercise in the use of strings and 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. You 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.


Opening the file for reading

Write a main method that opens a file for reading, and then closes it. It will not do anything with the file yet. You will need the heading of main to be

  public static void main(String[] args) throws FileNotFoundException, IOException

Start by opening the file. Read the input from a file called "document.txt". 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");
to create a new object called reader whose type is FileReader.

A class, Scanner.java, is provided for you that has some tools for reading from a file. Save it into your directory and add it to your project.

After opening the file, create a Scanner object from it. (That will make reading things easier.) Create the new Scanner object as follows.

  Scanner scan = new Scanner(reader);
The scanner (scan) that is created offers the following capabilities.
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. Do not try to convert -1 to type char.

scan.backup( )

This puts the most recently read character back into the input, so that 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.

Finish this part by closing the scanner.


readWord

For this assignment, read strings. Start by writing a static method called readWord.

readWord(scan)

This should read a word, consisting of letters, from Scanner object scan. It should return the word 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. So the array is created by

  char[] word = new char[100];

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. If your counter is called k, and c is the character that you just read, then

  word[k] = c;
stores c at index k in the array. Arrays start at index 0, so make k = 0 initially.

Stop as soon as you see a character that is not a letter, or you reach the end of the file. If you did not reach the end of the file, then put that character back (using scan.backup()). Never try to do scan.backup() at the end of the file.

You can test whether a character c is a letter using Scanner.isAlpha(c). (That expression is true if c is a letter and false if not.)

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

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

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

Now test your readWord function. Your main method already creates the scanner. Put a few words into file "document.txt". Try to read a word in main using readWord. Print the resulting word. Did it work? Now try to read a second word, and print that. Did it read both words correctly?


readNonword

Now write a function called readNonword that works similarly to readWord, but instead of getting consecutive letters, it gets consecutive nonletters. Test it.


pirate

Now write a function called pirate that takes a scanner object as a parameter. It should repeatedly read 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?

Write the words and nonwords to the standard output. That is, use System.out.print(x) to print string x.

Right now, it just shows the exact words. It does not change any of them.


pirateWord

Write a function called pirateWord that takes a string as a parameter and returns a string. If its parameter string is "is" or "are", pirateWord should return "be". If its parameter string is "Is" or "Are", pirateWord should return "Be". If the parameter string is anything else, pirateWord should return the parameter. For example, pirateWord("apple") = "apple".

If x and y are two strings, use x.equals(y) to ask if x and y are equal.

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


Modify pirate

Now modify your pirate function so that, after it reads a word w, it prints pirateWord(w) instead of just printing w. It should still show the nonwords exactly the way they were.

Test your program. It should be making documents more pirate-like.


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.