QuestionQuestion

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 the number of miles the car can be driven 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 MPG:

MPG = Miles / Gallons

Use the GridLayout Manager to line the text, textbox and button into 1 vertical column.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class CindyMPGCalculatorProject12 extends JFrame {

    private JPanel panel; //To reference a panel
    private JLabel messageLabelGallons; //To reference a gallons label
    private JLabel messageLabelMiles; //To reference a miles label
    private JTextField gallonsTextField;//To reference a gallons Text Field
    private JTextField milesTextField;//To reference a miles Text Field
    private JButton mpgButton; //To reference a button
    private final int WINDOW_WIDTH = 450; //Window width
    private final int WINDOW_HEIGHT = 150; //window height

    public CindyMPGCalculatorProject12() {
       //set the window title.
       setTitle("MPG Calculator");

       //set the size of the window.
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

       // Specify what happens when the close button is clicked.
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Build the panel and add it to the frame.
       buildPanel();

       //Add the panel to the frame's content pane.
       add(panel);

       // Display the window.
       setVisible(true);
       pack();
    }

    private void buildPanel() {
       // Create a label to display instructions.
       messageLabelGallons = new JLabel("Enter the number of gallons the car holds");

       //Create a text field 10 characters wide.
       gallonsTextField = new JTextField(10);...

By purchasing this solution you'll be able to access the following files:
Solution.java.

50% discount

Hours
Minutes
Seconds
$2.75 $1.38
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats