Question
PLEASE HELP WITH PART II: Part I: Miles-per-Gallon Calculator Write a GUI application that calculates a cars gas mileage. The application should let the user
PLEASE HELP WITH PART II:
Part I: Miles-per-Gallon Calculator Write a GUI application that calculates a cars gas mileage. The application should let the user enter the number of gallons of gas the car holds, and the number of miles it can bedriven on a full tank. When a Calculate 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 miles per gallon: MPG = miles/gallon
REQUIREMENTS:
-Must use meaningful names for fields, variables, methods and classes.
-Must verify all invalid data entries (letters and negative numbers).
-Must separate operation class MPGCalculator from the driver class called MPGCalculatorApp. (VERY IMPORTANT)
PART II: Assume that you have completed Part I. Convert it as an applet.
-Must use meaningful names for fields, variables, methods and classes.
-Name your applet as MPGCalculatorApplet (Note applet itself is a driver class).
-It is OK to run your applet in Eclipse without coding HTML file by yourself.
PART I RESULTS:
MPGCalculator.java(This is the driver class after saving both the .java files in the same package/folder you will be executing this one):
package chegg;
public class MPGCalculator {
public static void main (String[] args) { TMPG kc = new TMPG();
}
}
TMPG.java:
package chegg;
import javax.swing.*; import java.awt.event.*;
class TMPG extends JFrame
{ private JPanel panel; private JLabel gallonLabel; private JLabel milesLable; private JTextField gallonField; private JTextField milesFiled; private JButton clickButton; private final int WINDOW_WIDTH = 400; private final int WINDOW_HEIGHT = 400;
// constructor
public TMPG ()
{ setTitle ("MPG Calculator");
setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
buildPanel ();
add (panel);
setVisible(true);
}
private void buildPanel ()
{ //create a lable to display instructions gallonLabel =new JLabel ("Enter number of gallons"); gallonField = new JTextField (10);
//create label to display instructinos milesLable =new JLabel ("Enter number of miles"); milesFiled = new JTextField (10);
//create a button with the captions "Calculate".
clickButton = new JButton ("Calculate MPG"); clickButton.addActionListener (new CalculateMPG()); //adding items to panel
panel = new JPanel (); panel.add (gallonLabel); panel.add (gallonField); panel.add (milesLable); panel.add (milesFiled); panel.add (clickButton);
}
public class CalculateMPG implements ActionListener { public void actionPerformed (ActionEvent e)
{ String inputone; String inputtwo; double mpg;
//inpttting data
inputone = gallonField.getText (); inputtwo = milesFiled.getText (); mpg = (Double.parseDouble (inputtwo) /Double.parseDouble (inputone));
//outputting using Messagebox
JOptionPane.showMessageDialog (null, "Mpg of Car is:" + mpg);
}
}
}
NEED HELP WITH PART II. ANYTHING IS APPRECIATED GREATLY
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started