| 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.
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.
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));
}
Modify both PigLatinApplet and PigLatinGUIPanel so they interface with the PigLatin class and have the appropriate components and documentation.
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".
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.