Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import javax.swing.*; import java.awt.event.*; public class MPGCalculator { publ

ID: 3657441 • Letter: I

Question

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; //inputting 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); } } }

Explanation / Answer

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class mpgGUIV3 extends JFrame implements ActionListener{ //properties private Container frameContent; private GridLayout panelLayout; private JLabel milesLabel, gallonsLabel, mpgLabel; private JTextField milesField, gallonsField; private JButton calcButton; private double miles, gallons, mpg; //for calculating results /* Construtor (default) * Purpose: create and place components, create event handler * Modifies: all properties */ public mpgGUIV3() { this.setTitle("MPG Calculator"); //set properties for this frame: ALSO DO THE PACK this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(200,200); //create the components frameContent=this.getContentPane(); panelLayout = new GridLayout(3,2); milesLabel = new JLabel("Enter miles:"); gallonsLabel = new JLabel("Enter gallons"); mpgLabel = new JLabel(); milesField = new JTextField(); gallonsField = new JTextField(); calcButton = new JButton("Calculate MPG"); //add the layout and components to the content pane //NOTE: order components added matters frameContent.setLayout(panelLayout); frameContent.add(milesLabel); frameContent.add(milesField); frameContent.add(gallonsLabel); frameContent.add(gallonsField); frameContent.add(calcButton); frameContent.add(mpgLabel); //create an event handler and add it to the button calcButton.addActionListener(this);//addActionListener //display the frame this.setVisible(true); }//constructor public void actionPerformed (ActionEvent event){//handler miles = Double.parseDouble(milesField.getText()); gallons = Double.parseDouble(gallonsField.getText()); if(miles < 0.0 || gallons