Write a GUI application that calculates a car\'s gas mileage. The application sh
ID: 3641298 • Letter: W
Question
Write a GUI application that calculates a car's gas mileage. The application should let the user enter the number of gallons of gas the car holds, and number of miles it can be driven on a full tank. When a Calculation MPG button is clicked, the application should display the number of miles that the car may be driven per gallon of gas. Use the following formula to calculate the MPG:MPG= Miles / gallons ( Miles divided by gallons)
This is what I have...what am I doing wrong? Please help me fix it or help me rewrite it...I am am getting a ton of errors when I try and complile.
import javax.swing.*;
import java.awt.event.*;
class MPGCalculatorDemo extends JFrame
{
private JPanel panel;
private JLabel messageLabel_gallons;
private JLabel messageLabel_miles;
private JTextfield gallonsTextField;
private JTextfield milesTextField;
private JButton mpgButton;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 400;
// constructor
public MPGCalculatorDemo ()
{
setTitle ("MPG Calculator");
setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
//specify what happens when the close button is clicked
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
buildPanel ();
//add the panel to the frame's content pane.
add (panel);
//display the window
setVisible(true);
}
//the buildpanel method adds a lable
//text field, and a button to a panel
private void buildPanel ()
{
//create a lable to display instructions
messageLabel_gallons =
new JLabel ("Enter number of gallons");
gallonsTextField = new JTextField (10);
//create label to display instructinos
messageLabel_miles =
new JLabel ("Enter number of miles");
milesTextField = new JTextField (10);
//create a button with the captions "Calculate".
mpgButton = new JButton ("Calculate MPG");
mpgButton.addActionListener (new CalculateMPG());
//adding items to panel
panel = new JPanel ();
panel.add (messageLabel_gallons);
panel.add (gallonsTextField);
panel.add (messageLabel_miles);
panel.add (milesTextField);
panel.add (mpgButton);
}
public class CalculateMPG implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String inputone;
String inputtwo;
double mpg;
//inpttting data
inputone = gallonsTextField.getText ();
inputtwo = milesTextField.getText ();
mpg = (Double.parseDouble (inputtwo)
/Double.parseDouble (inputone));
//outputting using Messagebox
JOptionPane.showMessageDialog (null, "Mpg of Car is:" + mpg);
}
}
}
public class MPGCalculator
{
public static void main (String[] args)
{
MPGCalculatorDemo kc = new MPGCalulatorDemo();
}
}
Explanation / Answer
You need the file to have the same name as the main class, which is MPGCalculator, so name the file MPGCalculator.java. I found other typos, the first two were in the JTextField identifier, which needs to have a capital F in "Field". There was also a typo in the main method where you misspelled calculator. I included the corrected code below, it compiled fine for me. Hope this is what you needed!
----------------------------------------------------------------
import javax.swing.*;
import java.awt.event.*;
public class MPGCalculator
{
public static void main (String[] args)
{
MPGCalculatorDemo kc = new MPGCalculatorDemo();
}
}
class MPGCalculatorDemo extends JFrame
{
private JPanel panel;
private JLabel messageLabel_gallons;
private JLabel messageLabel_miles;
private JTextField gallonsTextField;
private JTextField milesTextField;
private JButton mpgButton;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 400;
// constructor
public MPGCalculatorDemo ()
{
setTitle ("MPG Calculator");
setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
//specify what happens when the close button is clicked
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
buildPanel ();
//add the panel to the frame's content pane.
add (panel);
//display the window
setVisible(true);
}
//the buildpanel method adds a lable
//text field, and a button to a panel
private void buildPanel ()
{
//create a lable to display instructions
messageLabel_gallons =
new JLabel ("Enter number of gallons");
gallonsTextField = new JTextField (10);
//create label to display instructinos
messageLabel_miles =
new JLabel ("Enter number of miles");
milesTextField = new JTextField (10);
//create a button with the captions "Calculate".
mpgButton = new JButton ("Calculate MPG");
mpgButton.addActionListener (new CalculateMPG());
//adding items to panel
panel = new JPanel ();
panel.add (messageLabel_gallons);
panel.add (gallonsTextField);
panel.add (messageLabel_miles);
panel.add (milesTextField);
panel.add (mpgButton);
}
public class CalculateMPG implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String inputone;
String inputtwo;
double mpg;
//inpttting data
inputone = gallonsTextField.getText ();
inputtwo = milesTextField.getText ();
mpg = (Double.parseDouble (inputtwo)
/Double.parseDouble (inputone));
//outputting using Messagebox
JOptionPane.showMessageDialog (null, "Mpg of Car is:" + mpg);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.