Question

Design and code a SWING GUI to translate text entered in English into Pig Latin. You...

Design and code a SWING GUI to translate text entered 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”; “button” becomes “uttonbay”; and so forth. For words that begin with vowels, add “way’ to the end of the word. Thus, “all” becomes “allway”; “one” becomes “oneway”; and so forth. Use a FLOWLAYOUT with a JTEXTAREA for the source text and separate JTEXTAREA for the translated text. Add a JBUTTON with an event to perform the translation. To parse the source text, note that you 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// PigLatinGUI.java

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Scanner;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class PigLatinGUI extends JFrame implements ActionListener {

      // declaring important UI components

      private JTextArea input;

      private JTextArea output;

      private JButton convert;

      // constructor

      public PigLatinGUI() {

            // will exit on close

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            // using flow layout

            setLayout(new FlowLayout());

            // initializing text areas and button

            input = new JTextArea(10, 30);

            output = new JTextArea(10, 30);

            convert = new JButton("Convert");

            // adding to frame

            add(input);

            add(output);

            add(convert);

            // adding action listener for the button

            convert.addActionListener(this);

            // using compact size and making window visible

            pack();

            setVisible(true);

      }

      @Override

      public void actionPerformed(ActionEvent e) {

            // creating a Scanner from input text

            Scanner scanner = new Scanner(input.getText());

            // clearing text of output text area

            output.setText("");

            // looping through each word

            while (scanner.hasNext()) {

                  String word = scanner.next();

                  // checking if first character of word is a vowel

                  if ("aeiou".contains("" + word.toLowerCase().charAt(0))) {

                        // simply appending 'way'

                        word = word + "way";

                  } else {

                        // moving first letter to the last and appending 'ay'

                        word = word.substring(1) + word.charAt(0) + "ay";

                  }

                  //writing to output text area

                  output.append(word + " ");

            }

      }

      public static void main(String[] args) {

            //initializing GUI

            new PigLatinGUI();

      }

}

/*OUTPUT (text looks smaller due to my system’s resolution, never mind that)*/


Add a comment
Know the answer?
Add Answer to:
Design and code a SWING GUI to translate text entered in English into Pig Latin. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this problem for a final thats in my book thats i'm stuck on ....

    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...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT