If you know a vehicle\'s speed and the amount of time it has travelled, you can
ID: 3809964 • 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? This is for Visual Basics
Explanation / Answer
Code:-
Public Class DistanceCalculator
Private Sub ButtonCalculate_Click(sender As System.Object,e As System.EventArgs)Handles ButtonCalculate.Click
'Declare Variables
Dim Speed,part_Distance,totalDist,partialTime As Double
Dim Time_hours As Double
Dim wholeTime As Integer
Speed=InputBox("Enter the speed of the vehicle as MPH terms ")
If Not IsNumeric(Speed) And Len(Speed) > 0 Then
MsgBox("Invalid value for speed...please Try again ")
Exit Sub
End If
If Not Val(Speed) > 1 Then
MsgBox("Invalid value for speed...Speed value should be greater than 1..please Try again ")
Exit Sub
End If
Speed = Val(Speed)
Time_hours = InputBox("Enter the time taken to travel as hours. ")
If Not IsNumeric(Time_hours) And Len(Time_hours) > 0 Then
MsgBox("Invalid value for time...please try again")
Exit Sub
End if
If Not Val(Time_hours) > 0 Then
MsgBox("Invalid value for time...please try again")
Exit Sub
End if
Time_hours=Val(Time_hours)
totalDist=Time_hours*Speed;
wholeTime = Time_hours / 1
partialTime = Time_hours Mod 1
ListBox1.Items.Add("Speed: "& Speed)
ListBox1.Items.Add("Time Travelled In hours : " & Time_hours)
ListBox1.Items.Add("------------)
ListBox1.Items.Add(" ")
For i = 1 to wholeTime
part_Distance = Speed
ListBox.Items.Add("Distance travelled untill this hour " & i & "is " & part_Distance & " miles")
Next
ListBox1.Items.Add("The distance travelled in the last "& Time_hours - wholeTime & "hour is " & Speed * (Time_hours - wholeTime))
ListBox1.Items.Add(" ")
ListBox1.Items.Add(" ")
ListBox1.Items.Add("The total distance travelled is : "&totalDist & "miles ")
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.