Question: I need a javadoc comment for my following code. This is what I need for the javadoc comments Author - Nas. The Date - 2

I need a javadoc comment for my following code. This is what I need for the javadoc comments Author-Nas. The Date-22 April 2024, and the purpose of the program which is to provide a family friendly interface for inputtig trip details and calculatees the toal trip cost based on the provided information. import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Project3 extends JFrame {
private JLabel distanceLabel, gasCostLabel, gasMileageLabel, resultLabel;
private JTextField distanceField, gasCostField;
private JComboBox distanceUnitComboBox, gasCostUnitComboBox, gasMileageUnitComboBox;
private JButton calculateButton;
public Project3(){
setTitle("Trip Cost Estimator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,200);
setLocationRelativeTo(null); // Center the window
distanceLabel = new JLabel("Distance:");
gasCostLabel = new JLabel("Gasoline Cost:");
gasMileageLabel = new JLabel("Gas Mileage:");
resultLabel = new JLabel("Total Trip Cost:");
distanceField = new JTextField(10);
gasCostField = new JTextField(10);
String[] distanceUnits ={"Miles", "Kilometers"};
distanceUnitComboBox = new JComboBox<>(distanceUnits);
String[] gasCostUnits ={"Dollars per Gallon", "Dollars per Liter"};
gasCostUnitComboBox = new JComboBox<>(gasCostUnits);
String[] gasMileageUnits ={"Miles per Gallon", "Kilometers per Liter"};
gasMileageUnitComboBox = new JComboBox<>(gasMileageUnits);
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new CalculateButtonListener());
JPanel panel = new JPanel(new GridLayout(4,2));
panel.add(distanceLabel);
panel.add(distanceField);
panel.add(distanceUnitComboBox);
panel.add(new JLabel()); // Empty space
panel.add(gasCostLabel);
panel.add(gasCostField);
panel.add(gasCostUnitComboBox);
panel.add(new JLabel()); // Empty space
panel.add(gasMileageLabel);
panel.add(new JLabel()); // Empty space
panel.add(gasMileageUnitComboBox);
panel.add(new JLabel()); // Empty space
panel.add(calculateButton);
panel.add(resultLabel);
panel.add(new JLabel()); // Empty space
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
}
private class CalculateButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
// Get user input
double distance = Double.parseDouble(distanceField.getText());
double gasCost = Double.parseDouble(gasCostField.getText());
// Convert units
if (distanceUnitComboBox.getSelectedIndex()==1){// Kilometers
distance *=0.621371; // Convert kilometers to miles
}
if (gasCostUnitComboBox.getSelectedIndex()==1){// Dollars per liter
gasCost *=3.78541; // Convert dollars per liter to dollars per gallon
}
// Calculate total trip cost
double totalCost = TripCost.calculateTripCost(distance, gasCost, gasMileageUnitComboBox.getSelectedIndex());
// Display result
resultLabel.setText("Total Trip Cost: $"+ String.format("%.2f", totalCost));
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Project3().setVisible(true);
}
});
}
}
class TripCost {
public static double calculateTripCost(double distance, double gasCost, int gasMileageUnitIndex){
double gasMileage;
if (gasMileageUnitIndex ==0){// Miles per gallon
gasMileage =25; // Example value, you can change this according to your needs
} else {// Kilometers per liter
gasMileage =10; // Example value, you can change this according to your needs
}
double totalCost =(distance / gasMileage)* gasCost;
return totalCost;
}
}

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 Programming Questions!