Write an application that encodes English language phrases into Pig Latin (Fig. 23.20). Pig Latin is a form of coded language often used for amusement. Use the following algorithm to form the Pig Latin words:

To form a Pig Latin word from an English-language phrase, the translation proceeds one word at a time. To translate an English word into a Pig Latin word, place the first letter of the English word (if it is not a vowel) at the end of the English word and add the letters “ay.” If the first letter of the English word is a vowel, place it at the end of the word and add "y." Using this method, the word “jump” becomes “umpjay,” the word “the” becomes “hetay” and the word “ace” becomes “ceay.” Blanks between words remain blanks.



Assume the following: The English phrase entered by the user consists of words sepa- rated by blanks, there are no punctuation marks and all words have two or more letters. The translateToPigLatin method should translate the sentence into Pig Latin, word by word.



a) Copying the template to your working directory. Copy the C:Examples Tutorial23ExercisesPigLatin directory to your C:SimplyJava directory.

b) Opening the template file. Open


```
1 // Exercise 23.16: PigLatin.java
2 // Converts text entered by user into Pig Latin.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class PigLatin extends JFrame
9 {
10 // JLabel and JTextField for input in English
11 private JLabel enterSentenceJLabel;
12 private JTextField enterSentenceJTextField;
13
14 // JLabel and JTextField for output in Pig Latin
15 private JLabel pigLatinJLabel;
16 private JTextField pigLatinJTextField;
17
18 // JButton to translate from English to Pig Latin
19 private JButton translateJButton;
20
21 // no-argument constructor
22 public PigLatin()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 public void createUserInterface()
29 {
30 // get content pane for attaching GUI components
31 Container contentPane = getContentPane();
32
33 // enable explicit positioning of GUI components
34 contentPane.setLayout( null );
35
36 // set up enterSentenceJLabel
37 enterSentenceJLabel = new JLabel();
38 enterSentenceJLabel.setBounds( 8, 16, 106, 23 );
39 enterSentenceJLabel.setText( "Enter a sentence:" );
40 contentPane.add( enterSentenceJLabel );
41
42 // set up enterSentenceJTextField
43 enterSentenceJTextField = new JTextField();
44 enterSentenceJTextField.setBounds( 114, 16, 306, 20 );
45 contentPane.add( enterSentenceJTextField );
46
47 // set up pigLatinJLabel
48 pigLatinJLabel = new JLabel();
49 pigLatinJLabel.setBounds( 8, 56, 88, 23 );
1 // Exercise 23.16: PigLatin.java
2 // Converts text entered by user into Pig Latin.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class PigLatin extends JFrame
9 {
10 // JLabel and JTextField for input in English
11 private JLabel enterSentenceJLabel;
12 private JTextField enterSentenceJTextField;
13
14 // JLabel and JTextField for output in Pig Latin
15 private JLabel pigLatinJLabel;
16 private JTextField pigLatinJTextField;
17
18 // JButton to translate from English to Pig Latin
19 private JButton translateJButton;
20
21 // no-argument constructor
22 public PigLatin()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 public void createUserInterface()
29 {
30 // get content pane for attaching GUI components
31 Container contentPane = getContentPane();
32
33 // enable explicit positioning of GUI components
34 contentPane.setLayout( null );
35
36 // set up enterSentenceJLabel
37 enterSentenceJLabel = new JLabel();
38 enterSentenceJLabel.setBounds( 8, 16, 106, 23 );
39 enterSentenceJLabel.setText( "Enter a sentence:" );
40 contentPane.add( enterSentenceJLabel );
41
42 // set up enterSentenceJTextField
43 enterSentenceJTextField = new JTextField();
44 enterSentenceJTextField.setBounds( 114, 16, 306, 20 );
45 contentPane.add( enterSentenceJTextField );
46
47 // set up pigLatinJLabel
48 pigLatinJLabel = new JLabel();
49 pigLatinJLabel.setBounds( 8, 56, 88, 23 );
50 pigLatinJLabel.setText( "Pig Latin:" );
51 contentPane.add( pigLatinJLabel );
52
53 // set up pigLatinJTextField
54 pigLatinJTextField = new JTextField();
55 pigLatinJTextField.setBounds( 114, 56, 306, 20 );
56 pigLatinJTextField.setEditable( false );
57 contentPane.add( pigLatinJTextField );
58
59 // set up translateJButton
60 translateJButton = new JButton();
61 translateJButton.setBounds( 325, 96, 95, 23 );
62 translateJButton.setText( "Translate" );
63 contentPane.add( translateJButton );
64 translateJButton.addActionListener(
65
66 new ActionListener() // anonymous inner class
67 {
68 // event handler called when translateJButton is pressed
69 public void actionPerformed( ActionEvent event )
70 {
71 translateJButtonActionPerformed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end call to addActionListener
77
78 // set properties of application’s window
79 setTitle( "Pig Latin" ); // set title bar string
80 setSize( 444, 160 ); // set window size
81 setVisible( true ); // display window
82
83 } // end method createUserInterface
84
85 // receive sentence from user and send to translateToPigLatin
86 private void translateJButtonActionPerformed( ActionEvent event )
87 {
88 // retrieve English phrase from user
89 String phrase = enterSentenceJTextField.getText();
90
91 // non-empty input
92 if ( phrase.equals( "" ) )
93 {
94 return; // exit method
95 }
96
97 // display phrase converted to Pig Latin
98 pigLatinJTextField.setText( translateToPigLatin( phrase ) );
99
100 } // end method translateJButtonActionPerformed
101
102 // translate String from English to Pig Latin
103 private String translateToPigLatin( String englishPhrase )
104 {
105 String prefix; // stores first letter of each word
106 String suffix; // stores a String added to the end of each word
107
108 // split words into an array
109 String words[] = englishPhrase.split( " " );
110
111 String translatedText = ""; // stores translated sentence
112
113 // iterate through each word in array words
114 for ( int index = 0; index <= words.length - 1; index++ )
115 {
116 // get first letter of each word
117 prefix = words[ index ].substring( 0, 1 );
118 prefix = prefix.toLowerCase();
119
120 // if each word starts with a vowel
121 if ( prefix.equals( "a" ) || prefix.equals( "e" ) ||
122 prefix.equals( "i" ) || prefix.equals( "o" ) ||
123 prefix.equals( "u" ) )
124 {
125 suffix = "y";
126 }
127 else // word does not start with a vowel
128 {
129 suffix = "ay";
130 }
131
132 // swap letters to create new word
133 words[ index ] = words[ index ].substring( 1 ) +
134 prefix + suffix;
135
136 translatedText += words[ index ] + " ";
137
138 } // end for
139
140 return translatedText;
141
142 } // end method translateToPigLatin
143
144 // main method
145 public static void main( String[] args )
146 {
147 PigLatin application = new PigLatin();
148 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
149
150 } // end method main
151
152 } // end class PigLatin
```

Computer Science & Information Technology

You might also like to view...

Word provides only three different SmartArt layouts

Indicate whether the statement is true or false

Computer Science & Information Technology

The computer system unit (the box enclosing the equipment) includes the ____, which is the electronic circuit board that connects the system's main components.

A. ALU B. motherboard C. control unit D. video card

Computer Science & Information Technology

Computer manuals and packaging materials might be useful for investigators for all of the following reasons EXCEPT which?

a. They are legally required for the related hardware to be admitted as evidence. b. They can alert investigators to hidden programs. c. They are often a popular place for hiding passwords. d. They can provide clues about the relative sophistication of the user.

Computer Science & Information Technology

When you want to create a chart you first need to select a data ________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology