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 TripCostdouble 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 Project extends JFrame
private JTextField txtDistance, txtGasolineCost, txtGasMileage, txtNumDays, txtHotelCost, txtFoodCost, txtAttractions;
private JComboBox cmbDistance, cmbGasolineCost, cmbGasMileage;
private JButton btnCalculate;
private JTextField txtTotalCost;
public Project
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.setEditablefalse;
Combo boxes with options
cmbDistance new JComboBoxnew Stringmiles "kilometers";
cmbGasolineCost new JComboBoxnew Stringdollarsgal "dollarslitre;
cmbGasMileage new JComboBoxnew Stringmilesgallon "kilometerslitre;
btnCalculate new JButtonCalculate;
btnCalculate.addActionListenernew ActionListener
public void actionPerformedActionEvent e
calculateCost;
;
Layout for the GUI components
setLayoutnew BoxLayoutgetContentPane BoxLayout.YAXIS;
addnew JLabelDistance:;
addtxtDistance;
addcmbDistance;
addnew JLabelGasoline Cost:";
addtxtGasolineCost;
addcmbGasolineCost;
addnew JLabelGas Mileage:";
addtxtGasMileage;
addcmbGasMileage;
addnew JLabelNumber Of Days:";
addtxtNumDays;
addnew JLabelHotel Cost:";
addtxtHotelCost;
addnew JLabelFood Cost:";
addtxtFoodCost;
addnew JLabelAttractions:;
addtxtAttractions;
addbtnCalculate;
addnew JLabelTotal Trip Cost";
addtxtTotalCost;
Setting up the frame
setTitleTrip Cost Estimator";
setDefaultCloseOperationJFrameEXITONCLOSE;
pack;
setVisibletrue;
private void calculateCost
Parse user input and perform conversions if necessary
double distance Double.parseDoubletxtDistancegetText;
distance cmbDistance.getSelectedItemequalskilometers distance : distance; Convert to miles if necessary
double gasCost Double.parseDoubletxtGasolineCostgetText;
gasCost cmbGasolineCost.getSelectedItemequalsdollarslitre gasCost : gasCost; Convert to dollarsgallon if necessary
double gasMileage Double.parseDoubletxtGasMileagegetText;
gasMileage cmbGasMileage.getSelectedItemequalskilometerslitre gasMileage : gasMileage; Convert to milesgallon if necessary
int numDays Integer.parseInttxtNumDaysgetText;
double hotelCost Double.parseDoubletxtHotelCostgetText;
double foodCost Double.parseDoubletxtFoodCostgetText;
double attractionsCost Double.parseDoubletxtAttractionsgetText;
Create a TripCost object and calculate the total cost
TripCost tripCost new TripCostdistance gasCost, gasMileage, numDays, hotelCost, foodCost, attractionsCost;
double totalCost tripCost.calculateTotalTripCost;
Display the total cost
txtTotalCost.setTextStringformat$f totalCost;
public static void mainString args
Run the GUI in the Event Dispatch Thread for thread safety
SwingUtilities.invokeLaternew Runnable
public void run
new Project; 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
