//**************************************************** // Kelli Wiseth // kelli_wiseth@yahoo.com // CIS 255WX // Program name: PigLatin // Description: Class PigLatin accepts user input (a single word, a phrase, or // a complete sentence) and converts to 'pig latin,' according to these simplified // rules: 1) words starting with vowels have 'way' added to them; and // 2) all other words have their first letter moved to the end, with 'ay' tacked on // after that. // // The program works for single-letter-words, such as the letter 'a' (which then // becomes "away," according to our rules). // // Each line of text input is first tokenized (the StringTokenizer is passed a set // of delimiters that includes punctuation) and then the string that comprises // each token is passed to the printLatinWord() method, which contains the // algorithm. // // The complete translated sentence is displayed in a single line in the output area; each // sentence remains in the display. // // Assignment 2 // 24 September 2004 // //**************************************************** import java.awt.*; // Container, FlowLayout import java.awt.event.*; // ActionEvent, ActionListener import javax.swing.*; // JApplet, JButton, JLabel, JTextField import java.util.*; // StringTokenizer, Pattern public class PigLatin extends JFrame { JLabel promptLabel; JTextField inputField; JTextArea outputArea; String latinWord=""; String translatedSentence=""; // calling the superclass' constructor to setup and instantiate GUI public PigLatin() { // call JFrame constructor to set title bar string super( "Kelli Wiseth Pig Latin Generator" ); // obtain content pane and set layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout(FlowLayout.LEFT, 10, 12) ); promptLabel = new JLabel( "Enter the word or phrase you want translated to PigLatin:"); container.add( promptLabel ); inputField = new JTextField( 33 ); inputField.addActionListener( new ActionListener() { // anonymous inner class for event handling // event handler for text field public void actionPerformed( ActionEvent event ) { StringTokenizer pre_latin_tokens = new StringTokenizer( event.getActionCommand(), " \t\n\r\f!?.," ); outputArea.setText(""); while (pre_latin_tokens.hasMoreTokens()) { latinWord = printLatinWord(pre_latin_tokens.nextToken()); translatedSentence += latinWord; }// closing brace for while // display the complete, translated phrase outputArea.setText(translatedSentence = translatedSentence + "\n"); } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener container.add(inputField); outputArea = new JTextArea( 15, 33 ); outputArea.setEditable( false ); container.add(new JScrollPane(outputArea)); setSize( 400, 370 ); // set the window size setVisible( true ); // show the window }// end constructor PigLatin public String printLatinWord(String word_input) { String latinWord = ""; String first_letter = ""; String pre_pig_latin_phrase = word_input; first_letter = word_input.substring(0,1); if ((first_letter.matches("[aeiou]")) || (first_letter.matches("[AEIOU]"))) { latinWord = word_input + "way "; } else { latinWord = word_input.substring(1) + word_input.substring(0,1) + "ay "; } return(latinWord); } // closing brace printLatinWord method public static void main(String args[]) { PigLatin application = new PigLatin(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // closing brace main method } // closing brace class PigLatin