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 it 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. //Here are the given class fields and layout to help you with the coding. public class MPGCalculator extends JFrame //Components private JPanel panel; private JTextField gallonsInput; private JTextField milesInput; private JButton calcButton; //Constants for the window size private final int WINDOW_WIDTH = 360; private final int WINDOW_HEIGHT = 130; //Constructor // Set the title // Specify what happens when the close button is clicked. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the panel that contains the other components. // Add the panel to the content pane. // Size and display the window. //build panel method
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
- 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 it 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.
//Here are the given class fields and layout to help you with the coding.
public class MPGCalculator extends JFrame
//Components
private JPanel panel;
private JTextField gallonsInput;
private JTextField milesInput;
private JButton calcButton;
//Constants for the window size
private final int WINDOW_WIDTH = 360;
private final int WINDOW_HEIGHT = 130;
//Constructor
// Set the title
// Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the panel that contains the other components.
// Add the panel to the content pane.
// Size and display the window.
//build panel method
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images