Computer Science 2311
Spring 2005
Programming Assignment 6

Date assigned: 2/23/05


For this assignment, modify your solution to assignment 5 as follows.


First modification

Your current program has the translations built into it. For example, it says to translate "is" to "be"

One general principal that software designers use is that programs should have as little information coded in them as possible. Instead, they should be flexible enough that they can read information from files. To modify the behavior of the software, you only need to modify the files, not the software.

This is how computer game manufacturers are able to create new games quickly. They write general software that reads information about characters, such as what the characters look like and how they move, from files. They read about backgrounds, a how different rooms or scenes are connected together. To make a new game, you just create files that describe new characters and backgrounds.

What to do

Modify your program that so that the translations are written in a file called pirate.txt. The file should consist of some number of lines, where each line has a regular word followed by a corresponding pirate word. For example, the pirate.txt file might look as follows, to request the same translations as before.

    is  be
    Is  Be
    are be
    Are be
  

Your program cannot assume that it knows how many lines are in the pirate.txt file. You must be able to add new lines without modifying your program at all.

Hints

  1. You know how to open a file for reading. You have already done it for document.txt. So do the same thing to read pirate.txt.

  2. You know how to read a word and how to read a nonword. Use that to read the two words per line from pirate.txt. Remember that there is is nonword (a newline character) between the second word on one line and the first word on the next line.

  3. You know how to test whether you are at the end of the file. Be careful, though. There can be a newline character after the last word. So, just after you have read the last word, you will not be at the end of the file.

  4. You will want a function to read the pirate.txt file. But it needs to put the information that it reads somewhere where other parts of the program can see it. You can create two arrays, such as regularWord and pirateWord, in main, and pass them to the function that reads pirate.txt. Then main can also pass those arrays to your translate function, so that it knows the translations to use.

    Arrange for regularWord[k] to be the k-th regular word, and pirateWord[k] to be the corresponding pirate word, where the words are numbered from 0. For example, when you read the first line of pirate.txt, put the first word into regularWord[0], and the second word into pirateWord[0]. When you read the next line, put the first word into regularWord[1], and the second word into pirateWord[1], etc.


Second modification

Now modify your program so that it prints "Argh," at the beginning of each sentence. For example, if the document.txt file contains

  The wind is strong tonight.  The sails are near 
  breaking.
then the program will print
  Argh, The wind be strong tonight.  Argh, The sails be near 
  breaking.

You can check whether a nonword string contains a period using the indexOf method in the String class. If s is a string, then expression s.indexOf(".", 0) returns a nonnegative number if s contains a period, and returns -1 if s does not contain a period. In general, s.indexOf(t,k) returns the starting index of the first occurrence of string t inside string s that starts at or beyond index k. If there is no such occurrence of t inside s, then it returns -1.

Be sure not to print Argh after the last sentence. Test your program to make sure it is working. Give it a difficult test, not an easy one. Try to expose any mistakes so that you can fix them rather than doing tests that are designed to hide mistakes.

Change your pirate.txt file by adding another translation. For example, you might translate "am" to "be". Think of some other changes you might make. You can get ideas from talklikeapirate.com. For example, look at the basics on talking like a pirate. Does the program work for the new pirate.txt file, without modifying the program?


What to turn in

Turn in your completed program and a printout of the pirate.txt and document.txt files that you used to test it.