122k views
2 votes
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" thus "ball" becomes "allbay" for words that begin with vowels add "way" to the end of the word thus "all" becomes "allway" use a flow layout with a jtextarea for the source text and a seperate jtextarea for the translated text add a jbutton with an event to perform the translation.

1 Answer

3 votes

Answer:

Code given below

Step-by-step explanation:

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 );

}

}

);

}

}

User Harsh Gundecha
by
5.8k points