Using Visual Basic 2008 2. Distance Calculator If you know a vehicles speed and
ID: 3619412 • Letter: U
Question
Using Visual Basic 2008 2. 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. Using Visual Basic 2008 2. 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.
Explanation / Answer
//Define class DistanceTraveled
public class DistanceCalculator
{
//Declared main function
public static void main(String[] args)
{
//Variable declartion
double Speed,Hours, Distance;
String input;
//inputting speed
input=JOptionPane.showInputDialog("Enter Speed:");
Speed=Double.parseDouble(input);
input=JOptionPane.showInputDialog("Enter time:");
Hours=Double.parseDouble(input);
if( Speed >= 1)
{
//Check condition for negativeinput nubers
if( Hours >=1)
{
System.out.println(" Hour Distance Traveled " );
System.out.print("------------------------------------------------ ");
for ( int i = 1; i <= Hours; i++)
{
//Calculatting Distance value
Distance = ( Speed * i);
//Display Distance value
System.out.println(i + " " + Distance );
}
}
else System.out.println(" Invalid Hours " );
}
else System.out.println(" Invalid Speed " );
System.exit(0);
}//End main function
}//End class DistanceTraveled
Hope this will help you..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.