Hello, I have an assignment for class where I have to write an apple that uses a trip time calculator. I have the following codes but cannot get the applet to run properly. Help is appreciated. Thanks.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TripComputerApplication extends JFrame implements ActionListener {
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private TripComputer theComputer;
public JLabel totalTimeLabel;
public JTextField distanceTextField;
public JTextField speedTextField;
public JTextField timeTextField;
/**
* Creates a new instance of TripComputerApplication
*/
public TripComputerApplication() {
setTitle("Trip Time Computer");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setBackground(Color.GREEN);
contentPane.setLayout(new BorderLayout());
// Do the panel for the rest stop
timeTextField = new JTextField();
JLabel timeLabel = new JLabel("Stop Time (hours)");
JButton stopButton = new JButton("Add Stop");
stopButton.addActionListener(this);
JPanel stopPanel = new JPanel();
stopPanel.setLayout(new GridLayout(3, 1, 30, 0));
stopPanel.add(timeTextField);
stopPanel.add(timeLabel);
stopPanel.add(stopButton);
contentPane.add(stopPanel, BorderLayout.WEST);
// Do the panel for the leg
distanceTextField = new JTextField();
JLabel distanceLabel = new JLabel("Distance (miles)");
speedTextField = new JTextField();
JLabel speedLabel = new JLabel("Speed (mph)");
JButton legButton = new JButton("Add Leg");
legButton.addActionListener(this);
JPanel legPanel = new JPanel();
legPanel.setLayout(new GridLayout(5, 1, 30, 0));
legPanel.add(distanceTextField);
legPanel.add(distanceLabel);
legPanel.add(speedTextField);
legPanel.add(speedLabel);
legPanel.add(legButton);
contentPane.add(legPanel, BorderLayout.EAST);
totalTimeLabel = new JLabel("Your trip time so far (hours): ");
contentPane.add(totalTimeLabel, BorderLayout.SOUTH);
theComputer = new TripComputer();
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
Container contentPane = getContentPane();
if (actionCommand.equals("Add Leg")){
try{
double speed = Double.parseDouble(speedTextField.getText().trim());
double distance = Double.parseDouble(distanceTextField.getText().trim());
theComputer.computeLegTime(distance, speed);
totalTimeLabel.setText("Your trip time so far (hours): " + theComputer.getTripTime());
} catch(TripComputerException ex){
totalTimeLabel.setText("Error: " + ex.getMessage());
} catch(Exception ex){
totalTimeLabel.setText("Error in speed or distance: " + ex.getMessage());
}
} else if (actionCommand.equals("Add Stop")){
try{
double time = Double.parseDouble(timeTextField.getText().trim());
theComputer.takeRestStop(time);
totalTimeLabel.setText("Your trip time so far (hours): " + theComputer.getTripTime());
} catch(TripComputerException ex){
totalTimeLabel.setText("Error: " + ex.getMessage());
} catch(Exception ex){
totalTimeLabel.setText("Error in time: " + ex.getMessage());
}
}
else
System.out.println("Error in button interface.");
}
public static void main(String[] args) {
TripComputerApplication gui = new TripComputerApplication();
gui.setVisible(true);
}
}
public class TripComputer {
private double totalTime;
private boolean restStopTaken;
/**
* Creates a new instance of TripComputer
*/
public TripComputer() {
totalTime = 0.0;
restStopTaken = true;
}
public void computeLegTime(double distance, double speed)
throws TripComputerException {
// throw an exception if distance <= 0
// throw an exception if speed <= 0
// compute legTime
// update totalTime
// clear the restStopTaken flag
}
public void takeRestStop(double time)
throws TripComputerException {
// throw an exception if time <= 0
// throw an exception if restStopTaken
// update totalTime
// set the restStopTaken flag
}
public double getTripTime(){
return totalTime;
}
}
public class TripComputerException extends Exception{
/**
* Creates a new instance of TripComputerException
*/
public TripComputerException(String reason) {
super(reason);
}
}