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);
    }

}

Recommended Answers

All 3 Replies

Could you tell us the problems, any error messages or runtime problem details

plus thats not an Applet thats a Swing

Well, I'm supposed to be running an applet but it crashes when it attempts to run. These are the errors I'm getting :

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
  required: tripcomputerapplication.TripComputerApplication
  found:    tripcomputerapplication.TripComputer
    at tripcomputerapplication.TripComputerApplication.<init>(TripComputerApplication.java:81)
    at tripcomputerapplication.TripComputerApplication.main(TripComputerApplication.java:115)

you can only have one public class present on a Java file where the public class must have the same name as the source file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.