Question: import javax.swing. * ; import java.awt.event. * ; / / Immutable class that calculates the trip cost class TripCost { private final double distance; private

import javax.swing.*;
import java.awt.event.*;
// Immutable class that calculates the trip cost
class TripCost {
private final double distance;
private final double gasCost;
private final double gasMileage;
private final int numDays;
private final double hotelCost;
private final double foodCost;
private final double attractionsCost;
public TripCost(double distance, double gasCost, double gasMileage, int numDays, double hotelCost, double foodCost, double attractionsCost){
this.distance = distance;
this.gasCost = gasCost;
this.gasMileage = gasMileage;
this.numDays = numDays;
this.hotelCost = hotelCost;
this.foodCost = foodCost;
this.attractionsCost = attractionsCost;
}
public double calculateTotalTripCost(){
double gasolineCost =(distance / gasMileage)* gasCost;
return gasolineCost +((hotelCost + foodCost)* numDays)+ attractionsCost;
}
}
// GUI class
public class Project3 extends JFrame {
private JTextField txtDistance, txtGasolineCost, txtGasMileage, txtNumDays, txtHotelCost, txtFoodCost, txtAttractions;
private JComboBox cmbDistance, cmbGasolineCost, cmbGasMileage;
private JButton btnCalculate;
private JTextField txtTotalCost;
public Project3(){
// Initialize the components
txtDistance = new JTextField();
txtGasolineCost = new JTextField();
txtGasMileage = new JTextField();
txtNumDays = new JTextField();
txtHotelCost = new JTextField();
txtFoodCost = new JTextField();
txtAttractions = new JTextField();
txtTotalCost = new JTextField();
txtTotalCost.setEditable(false);
// Combo boxes with options
cmbDistance = new JComboBox<>(new String[]{"miles", "kilometers"});
cmbGasolineCost = new JComboBox<>(new String[]{"dollars/gal", "dollars/litre"});
cmbGasMileage = new JComboBox<>(new String[]{"miles/gallon", "kilometers/litre"});
btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
calculateCost();
}
});
// Layout for the GUI components
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(new JLabel("Distance:"));
add(txtDistance);
add(cmbDistance);
add(new JLabel("Gasoline Cost:"));
add(txtGasolineCost);
add(cmbGasolineCost);
add(new JLabel("Gas Mileage:"));
add(txtGasMileage);
add(cmbGasMileage);
add(new JLabel("Number Of Days:"));
add(txtNumDays);
add(new JLabel("Hotel Cost:"));
add(txtHotelCost);
add(new JLabel("Food Cost:"));
add(txtFoodCost);
add(new JLabel("Attractions:"));
add(txtAttractions);
add(btnCalculate);
add(new JLabel("Total Trip Cost"));
add(txtTotalCost);
// Setting up the frame
setTitle("Trip Cost Estimator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void calculateCost(){
// Parse user input and perform conversions if necessary
double distance = Double.parseDouble(txtDistance.getText());
distance = cmbDistance.getSelectedItem().equals("kilometers")? distance *0.621371 : distance; // Convert to miles if necessary
double gasCost = Double.parseDouble(txtGasolineCost.getText());
gasCost = cmbGasolineCost.getSelectedItem().equals("dollars/litre")? gasCost *3.78541 : gasCost; // Convert to dollars/gallon if necessary
double gasMileage = Double.parseDouble(txtGasMileage.getText());
gasMileage = cmbGasMileage.getSelectedItem().equals("kilometers/litre")? gasMileage *2.35215 : gasMileage; // Convert to miles/gallon if necessary
int numDays = Integer.parseInt(txtNumDays.getText());
double hotelCost = Double.parseDouble(txtHotelCost.getText());
double foodCost = Double.parseDouble(txtFoodCost.getText());
double attractionsCost = Double.parseDouble(txtAttractions.getText());
// Create a TripCost object and calculate the total cost
TripCost tripCost = new TripCost(distance, gasCost, gasMileage, numDays, hotelCost, foodCost, attractionsCost);
double totalCost = tripCost.calculateTotalTripCost();
// Display the total cost
txtTotalCost.setText(String.format("$%.2f", totalCost));
}
public static void main(String[] args){
// Run the GUI in the Event Dispatch Thread for thread safety
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Project3(); // Create and show the GUI
}
});
}
}
Need a test plan and description of above program.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!