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

In Java: Create a Drop box List for the application\'s applet to include: Beginn

ID: 3776997 • Letter: I

Question

In Java:

Create a Drop box List for the application's applet to include:

Beginning locations (at least six)

Destination locations (at least six)

Vehicle size (compact, mid, luxury, SUV)

Gas type (leaded, unleaded, super unleaded, diesel)

Also create the following:

A field for approximate miles

A field for cost of gas per gallon

A Submit button

A Clear button

Create a Drop box List for the application's applet to include:

Beginning locations (at least six)

Destination locations (at least six)

Vehicle size (compact, mid, luxury, SUV)

Gas type (leaded, unleaded, super unleaded, diesel)

Also create the following:

A field for approximate miles

A field for cost of gas per gallon

A Submit button

A Clear button

Explanation / Answer

Answer:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TripCost
{
private JFrame mainFrame;
private JPanel controlPanel;
private JLabel lb1,lb2,result;
private JButton calculateCost,exit;
private JTextField jf1,jf2;

TripCost()
{
prepareGUI();
}

public static void main(String[] args)
{
TripCost tc = new TripCost();
tc.mathCalculations();
}

public void prepareGUI()
{
mainFrame = new JFrame("Calculating Expected Cost of Trip Using Java Swings");
mainFrame.setSize(600,600);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setLayout(new BorderLayout());

controlPanel = new JPanel(new GridLayout(4,2));
mainFrame.add(controlPanel);
mainFrame.setVisible(true);

}

public void mathCalculations()
{

lb1 = new JLabel("Enter Number of Miles ",JLabel.CENTER);
jf1 = new JTextField(5);
lb2 = new JLabel("Enter cost of gas per gallon ",JLabel.CENTER);
jf2 = new JTextField(5);
result = new JLabel("",JLabel.CENTER);

calculateCost = new JButton("calculate Cost");
calculateCost.addActionListener(new MyListener());

exit = new JButton("Exit");
exit.addActionListener(new MyListener());

controlPanel.add(lb1);
controlPanel.add(jf1);
controlPanel.add(lb2);
controlPanel.add(jf2);

controlPanel.add(calculateCost);
controlPanel.add(exit);

controlPanel.add(result);

mainFrame.setVisible(true);

}


class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exit)
{
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else if(e.getSource() == calculateCost)
{
int miles = Integer.parseInt(jf1.getText());
double gallonsCost = Double.parseDouble(jf2.getText());

// Assuming an Oil Change is $30 and the car we drive averages 30 miles per gallon
double costOfTrip = (miles * gallonsCost) / 30 + 30;
result.setText("Expected Cost of Trip is : "+costOfTrip);
}
}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote