Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If you know a vehicle\'s speed and the amount of time it has travelled, you can

ID: 3624873 • Letter: I

Question

If you know a vehicle's speed and the amount of time it has travelled, you can calculate the distance it has travelled as follows:

* Distance = Speed * Time

For example, if a train travels 40 miles per hour for 3 hours, the distance travelled is 120 miles. Create an application with a form of the Calculator.

When the user clicks the calculate button, the application should display an input box asking the user for the speed of the vehicle in miles-per-hour, followed by another input box asking for the amount of time, in hours, that the vehicle has travelled. Then it should use a loop to display in a list box the distance the vehicle has travelled for each hour of that time period.

*Input validation: Do not accept a value less than 1 for the vehicle's speed or the number of hours travelled.

Can you provide all the code and picture?
Thanks

Explanation / Answer

Please rate...

Program DistanceCalculator.java

=========================================================

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class DistanceCalculator extends JFrame implements ActionListener
{
    private JLabel speed,time;
    private JTextField speedTF,timeTF;
    private JTextArea distance;
    private JButton calculate;

    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;

    DistanceCalculator()
    {
        setTitle("Distance Claculator");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        speed=new JLabel("Speed:               ");
        time=new JLabel("Time(in hours): ");

        speedTF=new JTextField("",30);
        timeTF=new JTextField("",30);

        calculate=new JButton("Calculate");

        distance=new JTextArea("",10,30);
        JScrollPane sp=new JScrollPane(distance);

        panel1=new JPanel();
        panel1.add(speed);
        panel1.add(speedTF);

        panel2=new JPanel();
        panel2.add(time);
        panel2.add(timeTF);

        panel3=new JPanel();
        panel3.add(calculate);

        panel4=new JPanel();
        panel4.add(sp);

        setLayout(new GridLayout(4,1));
        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);

        pack();
        addListeners();
        setVisible(true);
    }
    public void addListeners()
    {
        calculate.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==calculate)
        {
            double s=Double.parseDouble(speedTF.getText());
            double t=Double.parseDouble(timeTF.getText());
            if(s<1)
            {
                JOptionPane.showMessageDialog(this,"The value of speed is lees than 1");
                setVisible(false);
                new DistanceCalculator();
            }
            if(t<1)
            {
                JOptionPane.showMessageDialog(this,"The value of time is lees than 1");
                setVisible(false);
                new DistanceCalculator();
            }
            int i;
            String d1="";
            double d2=0;
            for(i=1;i<=t;i++)
            {
                d2=d2+(s*i);
                String d=d2+"";
                d1="For "+i+" hour: "+d+". ";
                distance.append(d1);
            }
        }
    }
    public static void main(String args[])
    {
        new DistanceCalculator();
    }
}

========================================================

Sample output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote