Laboratory 10: The Pig Latin Translator

April 11, 12, 2006

Bring your Java, Java, Java text to the laboratory.

Objectives

The objectives of this laboratory are
  1. To give practice with String processing classes and methods
  2. To give practice writing string processing algorithms
In preparation for this lab you should review Chapter 7 of Java, Java, Java.

Problem statement (obplempray atementstay)

Ancay uoyay eakspay igpay atinlay? Do you remember how to speak Pig Latin? Here are the rules: Write a Java program that translates an English sentence, input by the user, into Pig Latin.

Sample Solution

Pig Latin Applet

Problem Decomposition

The computational object for this week's problem can be defined in the PigApplet class, which has the design shown in the following UML diagram.
PigLatin


- PigLatin()
+ static translate(s: String): String
- static translateWord(s: String): String
- static findFirstVowel(s: String): int
+ static main(args: String[])

Note that like the Math class and LeapYear class, the PigLatin class contains no instance variables. The reason is that there is no need to store any data for this problem. Similarly, there are no instance methods, only static methods. The reason for this is that there is no need to create instances in order to solve this problem.

Also, note that there is only one public method, translate(). This method takes a string, representing an English sentence containing zero or more words, and returns its Pig Latin translation. This method should use a StringTokenizer object to break up the sentence into words (tokens) and translate each word (token).

The translateWord() method takes a String, representing a single word and applies the rules to translate the word into Pig Latin. For example, translateWord("pig") would return "igpay".

The findFirstVowel() method takes a String and finds the location of its first vowel. For example, findFirstVowel("pig") would return 1, the index of 'i'. Question: Is this the kind of loop problem that can be solved by iterating through every character of the word?

The GUI for this project should resemble the GUI you developed in previous labs. As the demo shows, the GUI really only needs an input text field and a text area to display the results (two GUI components). If you assign an ActionListener to the text field, you don't even need a button to control the action of the GUI.

Lab 7 provides a good GUI model for this problem. Copy LeapYearApplet.java and LeapYearGUIPanel.java to this week's folder and modify these files (including comments, variable names, prompts, etc.) so that they are appropriate for this week's problem.

Using a StringTokenizer

The translate() method can use a StringTokenizer method to break an English sentence into its invdividual words and translate each word. Here's some sample code that shows how to use a StringTokenizer to do a similar problem. In this code segment, a StringTokenizer is used to reverse the letters in each word of a sentence and print out the resulting sentence. Note that the import statement goes at the beginning of your Java file, before the start of the class definition.
import java.util.StringTokenizer;  // Import the tokenizer class 

...
String sentence = "this is a sample sentence";           // A sample sentence
...
StringBuffer result = new StringBuffer();                // Will store revised sentence
StringTokenizer words = new StringTokenizer(sentence);   // Create the tokenizer
while (words.hasMoreTokens()) {                          // Reverse all its words
    result.append(reverse(word.nextToken()));
}
System.out.println(result.toString());   // Print the sentence with its words reversed
...

In this example, the while may look like it is lacking the initializer and updater steps. But these tasks have been incorporated into the StringTokenizer object, which maintains an internal list of tokens. Its hasMoreTokens() method returns false when the list is empty and its nextToken() method removes the next token from the list and returns it. Thus by repeatedly calling nextToken(), the list of tokens is eventually consumed.

Implementation

Step 1. Set Up.

Copy and rename the following files from your lab7 folder into this week's lab10 folder:

Step 2. Designing and Implementing the PigLatin Class.

Use stepwise refinement to develop and test the PigLatin class. As we did in the past couple of labs, define a main() method and use it to test each method as you are developing it. Start with the findFirstVowel() method, because it is used by the translateWord() method. When that method is correctly implemented, develop and test the translateWord() method. When that method is done, work on the translate() method.

Here's some sample code to get started with your main()method:

public static void main(String args[]) {
    String word = "latin";
    System.out.println("First vowel of " + word + " at " + PigLatin.findFirstVowel(word));
}

Step 3. Adapting the Applet and Panel Classes.

Modify both PigLatinApplet and PigLatinGUIPanel so they interface with the PigLatin class and have the appropriate components and documentation.

Step 4. Optional Refinement.

Modify the translateWord() method so that it adds only "ay" to words that end in 'y'. So "my" would be translated as "myay" instead of "myyay".

Hand In.

Before handing in your programs, be sure to document your source code. Every method must have a comment block that describes the method, its parameters, and its return value.

Have your work checked by the laboratory instructor or TA. Print out and hand in a copy of your PigLatinGUIPanel.java and PigLatin.java source code.

You're done. Great work!