
Akisha L.
asked 01/31/21Programming exercise
P R O G R A M M I N G
Latin Translator
Look at the following list of Latin words and their meanings:
Latin English
sinister left
dexter right
medium center
1. Design a GUI program that translates the Latin words to English. The window should have three buttons, one for each Latin word. When the user clicks a button, the program displays the English translation in a label component.
A. Name and Address
Design a GUI program that displays your name and address when a button is clicked. The program’s
window should appear as the sketch on the left side of Figure 17-9 when it runs. When the user clicks the
Show Info button, the program should display your name and address, as shown in the sketch on the right
side of the figure.
Figure 17-9 Name and Address program
B. Miles-per-Gallon Calculator
Design a GUI program that calculates a car’s gas mileage. The program’s window should have text boxes
that let the user enter the number of gallons of gas the car holds, and the number of miles it can be driven
on a full tank. When a Calculate MPG button is clicked, the program should display the number of miles
that the car may be driven per gallon of gas. Use the following formula to calculate milesper-gallon:
MPG = Miles/Gallons
C. Design an event handler that will execute when the showNameButton component is clicked. The event
handler should perform the following:
Store your first name in a label component named firstNameLabel.
Store your middle name in a label component named middleNameLabel.
Store your last name in a label component named lastNameLabel.
(Remember, to store a value in a label component, you must store the value in the component’s Text property.)
1 Expert Answer

Patrick B. answered 01/31/21
Math and computer tutor/teacher
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Akisha extends JPanel {
private JButton cmdButTranslate1;
private JButton cmdButTranslate2;
private JButton cmdButTranslate3;
private JLabel labelLatin1;
private JLabel labelLatin2;
private JLabel labelLatin3;
private JLabel labelEnglish1;
private JLabel labelEnglish2;
private JLabel labelEnglish3;
private JLabel labelGallons;
private JLabel labelMiles;
private JTextArea txtNumGallons;
private JTextArea txtNumMiles;
private JButton cmdButCalculateMPG;
private JLabel labelMPG;
private JButton cmdButShowName;
private JLabel labelFirstName;
private JLabel labelMiddleName;
private JLabel labelLastName;
private JLabel labelAddress;
private JLabel labelLatin2English;
public Akisha() {
//construct components
cmdButTranslate1 = new JButton ("Translate");
cmdButTranslate2 = new JButton ("Translate");
cmdButTranslate3 = new JButton ("Translate");
labelLatin1 = new JLabel ("sinister");
labelLatin2 = new JLabel ("dexter");
labelLatin3 = new JLabel ("medium");
labelEnglish1 = new JLabel ("");
labelEnglish2 = new JLabel ("");
labelEnglish3 = new JLabel ("");
labelGallons = new JLabel ("# of gallons");
labelMiles = new JLabel ("# of miles");
txtNumGallons = new JTextArea (5, 5);
txtNumMiles= new JTextArea (5, 5);
cmdButCalculateMPG = new JButton ("Calculate MPG");
labelMPG = new JLabel ("");
cmdButShowName = new JButton ("SHOW NAME");
labelFirstName = new JLabel ("");
labelMiddleName = new JLabel ("");
labelLastName = new JLabel ("");
labelAddress = new JLabel ("");
labelLatin2English = new JLabel (" LATIN ENGLISH");
//adjust size and set layout
setPreferredSize (new Dimension (624, 335));
setLayout (null);
//add components
add (cmdButTranslate1);
add (cmdButTranslate2);
add (cmdButTranslate3);
add (labelLatin1);
add (labelLatin2);
add (labelLatin3);
add (labelEnglish1);
add (labelEnglish2);
add (labelEnglish3);
add (labelGallons);
add (labelMiles);
add (txtNumGallons);
add (txtNumMiles);
add (cmdButCalculateMPG);
add (labelMPG);
add (cmdButShowName);
add (labelFirstName);
add (labelMiddleName);
add (labelLastName);
add (labelAddress);
add (labelLatin2English);
cmdButTranslate1.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelEnglish1.setText("left");
}
}
);
cmdButTranslate2.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelEnglish2.setText("right");
}
}
);
cmdButTranslate3.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelEnglish3.setText("center");
}
}
);
cmdButCalculateMPG.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
String inbuff = txtNumGallons.getText();
double gallons = Double.parseDouble(inbuff);
inbuff = txtNumMiles.getText();
double miles = Double.parseDouble(inbuff);
if (miles > 0)
{
double mpg = miles/gallons;
Double dblMPG = new Double(mpg);
labelMPG.setText(dblMPG.toString());
}
}
}
);
cmdButShowName.addActionListener(
new ActionListener()
{
@Override public void actionPerformed(ActionEvent e)
{
labelFirstName.setText("first_name");
labelMiddleName.setText("middle_name");
labelLastName.setText("last_name");
labelAddress.setText("123 yourStreet yourCity,ST 12345-6789");
}
}
);
//set component bounds (only needed by Absolute Positioning)
cmdButTranslate1.setBounds (5, 50, 90, 15);
cmdButTranslate2.setBounds (5, 70, 90, 20);
cmdButTranslate3.setBounds (5, 95, 90, 20);
labelLatin1.setBounds (100, 50, 65, 15);
labelLatin2.setBounds (100, 75, 55, 15);
labelLatin3.setBounds (100, 100, 65, 15);
labelEnglish1.setBounds (170, 50, 75, 15);
labelEnglish2.setBounds (170, 70, 70, 20);
labelEnglish3.setBounds (170, 95, 80, 20);
labelGallons.setBounds (335, 45, 70, 25);
labelMiles.setBounds (335, 70, 70, 25);
txtNumGallons.setBounds (405, 45, 95, 25);
txtNumMiles.setBounds (405, 75, 95, 25);
cmdButCalculateMPG.setBounds (345, 110, 100, 25);
labelMPG.setBounds (460, 115, 100, 25);
cmdButShowName.setBounds (10, 245, 110, 25);
labelFirstName.setBounds (125, 245, 100, 25);
labelMiddleName.setBounds (240, 245, 100, 25);
labelLastName.setBounds (365, 245, 100, 25);
labelAddress.setBounds (125, 280, 350, 25);
labelLatin2English.setBounds (85, 15, 145, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Akisha");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Akisha());
frame.pack();
frame.setVisible (true);
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section... Programs 2 and 4 are identically except for the address; no image was posted I'm sorry but do to time constraints I put all three apps on the same form.01/31/21