Using Visual Basic 2008 2. Distance Calculator If you know a vehicles speed and
ID: 3619419 • Letter: U
Question
Using Visual Basic 20082. Distance Calculator If you know a vehicles speed and the amount of time it has traveled, you can calculate the distance it has traveled as follows: Distance = Speed * Time For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Create an application.
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 trav-eled. Then it should use a loop to display in a list box the distance the vehicle has traveled for each hour of that time period.
Input validation: Do not accept a value less than 1 for the vehicles speed or the number of hours traveled.
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:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.