I have this problem for a final thats in my book thats i\'m stuck on . Any help
ID: 3843673 • Letter: I
Question
I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated.
Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation.
The rules for Pig Latin are as follows:
For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes “allbay”; “button” becomes “uttonbay”; and so forth.
For words that begin with vowels, add “way” to the end of the word. For example, “all” becomes “allway”; “one” becomes “oneway”; and so forth.
To parse any String of words, you can use a StringTokenizer.
You also can use the Scanner class on a String. For example, the following code:
Scanner scan = new Scanner("foo bar zot");
while (scan.hasNext()) {
System.out.println(scan.next());
}
will output
foo
bar
zot
Use a FlowLayout with a JTextArea for the source text and a separate JTextArea for the translated text. Add a JButton with an event to perform the actual translation.
Also provide a JButton which will exit the program
make screen with a 5 x 1 GridLayout:
The first panel contains the the title “Pig Latin Translator”
The second panel contains the two headings
The third panel contain two JTextArea objects. The one on the left is enabled. The one on the right does not accept input
The fourth panel holds the “Translate” and “Exit” buttons
The last panel contains an example of a translation from English to Pig Latin.
Feel free to use your own screen design, one which is more pleasing, easier to use, or more interesting. Add colors, but subtle colors are usually preferable to loud ones.
Submit listings for class or classes in your project. Test your program with at least 3 different translations and PrintScreen those results, as well.
This is some sample code he gave us for context . Can't use anything too advance.
Explanation / Answer
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class PigLatin extends JFrame
{
private JLabel prompt;
private JTextField input;
private JTextArea output;
private int count;
public PigLatin()
{
super( "Pig Latin Generator" );
prompt = new JLabel( "Enter English phrase:" );
input = new JTextField( 30 );
input.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
String s = e.getActionCommand().toString();
StringTokenizer tokens = new StringTokenizer( s );
count = tokens.countTokens();
while ( tokens.hasMoreTokens() ) {
count--;
printLatinWord( tokens.nextToken() );
}
}
}
);
output = new JTextArea( 10, 30 );
output.setEditable( false );
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.add( prompt );
c.add( input );
c.add( output );
setSize( 500, 150 );
show();
}
private void printLatinWord( String token )
{
char letters[] = token.toCharArray();
StringBuffer schweinLatein = new StringBuffer();
schweinLatein.append( letters, 1, letters.length - 1 ) ;
schweinLatein.append( Character.toLowerCase( letters[ 0 ] ) );
schweinLatein.append( "ay" );
output.append( schweinLatein.toString() + " " );
if ( count == 0 )
output.append( " " );
}
public static void main( String args[] )
{
PigLatin app = new PigLatin();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}