Complete exercise 14.7. // Exercise 14.7 : PigLatin.java // Application translat
ID: 3785512 • Letter: C
Question
Complete exercise 14.7.
// Exercise 14.7 : PigLatin.java
// Application translates English to Pig Latin.
import javax.swing.JFrame;
public class PigLatin{
public static void main(String[] args) {
// create PigLatinFrame object
//set default close operation
//set frame size(400, 200)
//display frame
}
} // end class PigLatin
//Exercise 14.7: PigLatinFrame.java
// Application translates English to Pig Latin.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PigLatinFrame extends JFrame{
// create JTextField for entering sentences to be converted
// create JTextArea to log converted pig Latin phrases
public PigLatinFrame() {
super("Pig Latin Conversion");
// create input text field
// add event handler
// anonymous inner class
{
public void actionPerformed(ActionEvent e) {
//get input text and convert the phrase
//clear input text
}
}
//close event handler
// create output text area
// do not allow output text area to be editable
// set layout
//add input component
//add scroll pane component to allow scrolling
}
// convert a phrase to pig Latin and display it
private void convertPhrase(String phrase) {
// translate user input
// print words converted to pig Latin one at a time
for (String word : words)
// print latin word
// add newline to output
}
// convert a single word to pig Latin and add to output text area
private void printLatinWord(String word) {
output.append(word.substring(1));
// append first character of output to lower case
output.append("ay ");
}
} // end class PigLatin
Explanation / Answer
// Exercise 14.7 : PigLatin.java
// Application translates English to Pig Latin.
import javax.swing.JFrame;
public class PigLatin{
public static void main(String[] args) {
// create PigLatinFrame object
PigLatinFrame pig=new PigLatinFrame();
pig.setSize(400, 200);
pig.setLocation(100,100);
pig.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pig.setVisible(true);
}
} // end class PigLatin
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PigLatinFrame extends JFrame implements ActionListener {
// Data members: components of the GUI.
private JLabel englishLabel;
private JTextArea englishText;
private JButton translate;
private JLabel latinLabel;
private JTextArea latinText;
/**
* Constructor: sets up the GUI.
*/
public PigLatinFrame() {
// Set some basic frame properties
this.setTitle("Pig Latin Translator");
// Get the content panel from inside the frame
Container panel = this.getContentPane();
// Not using a layout- doing absolute positioning
panel.setLayout(null);
// Construct the labels for english and latin text
englishLabel = new JLabel("English: ");
latinLabel = new JLabel("Pig Latin: ");
// Construct the area for text entry, with border and text wrap
englishText = new JTextArea();
englishText.setEditable(true);
englishText.setBorder(BorderFactory.createLoweredBevelBorder());
englishText.setLineWrap(true);
JScrollPane englishTextScrollPane = new JScrollPane (englishText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Construct the area for text display, with border and text wrap
latinText = new JTextArea();
latinText.setEditable(false);
latinText.setBorder(BorderFactory.createRaisedBevelBorder());
latinText.setLineWrap(true);
JScrollPane latinTextScrollPane = new JScrollPane (latinText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// Construct the button for translation
translate = new JButton("Translate");
translate.addActionListener(this);
// Add all the components to the panel
panel.add(englishLabel);
panel.add(englishTextScrollPane);
panel.add(translate);
panel.add(latinLabel);
panel.add(latinTextScrollPane);
// Set the locations of the components inside the panel
englishLabel.setBounds(25,100,400,25);
englishTextScrollPane.setBounds(50,130,400,100);
translate.setBounds(200,260,100,40);
latinLabel.setBounds(25,320,100,25);
latinTextScrollPane.setBounds(50,350,400,100);
}
/**
* Returns the string in Pig Latin.
* @param text The text to translate
* @return The translated version
*/
private String toPigLatin(String text) {
String translation = "";
// Go until there aren't any more words
while (!text.equals("")) {
// Get the first word
int wordEndIndex = findWordEnd(text);
String word = text.substring(0, wordEndIndex);
// Find the root and tail in pig latin
int vowelIndex = findVowel(word);
String root = word.substring(vowelIndex);
String tail = "-" + word.substring(0, vowelIndex) + "ay";
// Print the translated word
translation = translation + root + tail + " ";
// Cut first word off text (and spaces)
text = text.substring(wordEndIndex).trim();
}
return translation;
}
/**
* Finds the end of the first word in the string.
* @param text The text to look in
* @return The index of the first space
*/
private int findWordEnd(String text) {
int indexOfSpace = text.indexOf(" ");
if (indexOfSpace == -1) // Happens if there is no space
return text.length();
else
return indexOfSpace;
}
/**
* Finds the first vowel in the word.
* @param word The word to look in
* @return The index of the first vowel
*/
private int findVowel(String word) {
// Look for a vowel, or the end of the word
for (int i=0; i<word.length(); i++) {
if (word.charAt(i) == 'a' ||
word.charAt(i) == 'e' ||
word.charAt(i) == 'i' ||
word.charAt(i) == 'o' ||
word.charAt(i) == 'u' ) {
return i;
}
}
// Gets here if there aren't any vowels
return word.length();
}
/**
* Calls the translation method when there's a button click.
* @param event The ActionEvent caused by a button click
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() == translate) {
// Get the English they entered
String text = englishText.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
latinText.setText(toPigLatin(text));
}
}
}
Please Create Separate files for both PigLatin.java and PigLatinFrame.java and compile them separately.
Then run the PigLatin file.
Also you can manipulate the orientation of the components in the frame by varying the following code:
// Set the locations of the components inside the panel
englishLabel.setBounds(25,100,400,25);
englishTextScrollPane.setBounds(50,130,400,100);
translate.setBounds(200,260,100,40);
latinLabel.setBounds(25,320,100,25);
latinTextScrollPane.setBounds(50,350,400,100);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.