import objectdraw.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Translate text into Pig Latin. * * The rules for translating a word into Pig Latin are: * * 1. if the word has no vowels, it stays the same. * 2. if the word starts with a vowel, add "way" to the end. * 3. if the word's first vowel is at position i, move the first * i-1 characters to the end, and append "ay". */ public class PigLatin extends FrameWindowController { private static final int WINDOW_WIDTH = 500; private static final int WINDOW_HEIGHT = 300; private static final Location INPUT_POS = new Location(50, 50); private static final Location OUTPUT_POS = new Location(50, 150); private static final int FONT_SIZE = 20; private Text inputText, outputText; public void begin() { resize(WINDOW_WIDTH, WINDOW_HEIGHT); inputText = new Text("string processing is fun", INPUT_POS, canvas); inputText.setFontSize(FONT_SIZE); outputText = new Text("Click to translate!", OUTPUT_POS, canvas); outputText.setFontSize(FONT_SIZE); } public void onMouseClick(Location point) { String input = inputText.getText(); String convertedText = translate(input); outputText.setText(convertedText); } // translate a String into Pig Latin private String translate(String line) { // the translated string (so far) String result = ""; // start position of the next word int startIndex = 0; int endIndex = line.indexOf(" "); while (endIndex != -1) { // the next word is the current index up to the next space String nextWord = translateWord(line.substring(startIndex, endIndex)); result = result + nextWord + " "; // start looking for the next word just past the current one startIndex = endIndex + 1; // get the index of the next space in the line endIndex = line.indexOf(" ", startIndex); } // endIndex is -1, meaning startIndex is the starting index of the last word String lastWord = translateWord(line.substring(startIndex)); result = result + lastWord; // return the complete translated string return result; } // return true if c is a vowel private boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } // return the index of the first vowel in a word, or -1. private int indexOfFirstVowel(String word) { // search from the beginning int index = 0; while (index < word.length()) { if (isVowel(word.charAt(index))) { // found a vowel, return its position return index; } index++; } // didn't find a vowel return -1; } /** * Convert a word into a Pig Latin word. * The rules are: * 1. if a word has no vowels, it stays the same * 2. if a word starts with a vowel, add "way" to the end * 3. if a word's first vowel is at position i, move the first * i-1 characters to the end, and append "ay". */ private String translateWord(String word) { // get the index of the first vowel in the word int firstVowelIndex = indexOfFirstVowel(word); if (firstVowelIndex == -1) { // no first vowel, just return the input string return word; } else if (firstVowelIndex == 0) { // starts with a vowel, add "way" to the input string return word + "way"; } else { // otherwise, move everything up to the vowel to end of the word and add "ay" String upToVowel = word.substring(0, firstVowelIndex); String followingVowel = word.substring(firstVowelIndex); return followingVowel + upToVowel + "ay"; } } }