 |
|
 |
armyfrenchy Level: Trainee
 Registered: 13-04-2010 Posts: 2
|
please help me with my vb code???
Can someone please fill in the banks? I am totally confused!! This program needs to list in the listbox the distance a car travelled in miles based on speed and travelled time in hours...with a popup box that appears if the values entered are not numerical? Formula is distance=speed*time. When the user clicks on the button calculate, two Inputboxes need to appear asking for speed and distance. It is supposed to work with listboxes, arrays, and inputboxes, and have validations if user enters string instead of numbers...
I am not sure what to put in the listbox lblist subroutine to make what the user inputted appear? here is the assignment
Loops
 Data validation
 Decision structures
 Sub Procedures
 InputBox Function
 MessageBox Function
 Modularity concept
Program:
This program shall determine the distance that a vehicle travels (in miles) based on the vehicle’s speed (in MPH), and the traveled time (in hours).
NOTE: In most applications, the InputBox function should not be used as the primary method of input because it draws the user’s attention away from the application’s form. It also complicates data validation because if a data entry error occurs, the input box closes not allowing the user for another opportunity for coming back and fixing the error. Despite these drawbacks, it is a convenient tool for developing and testing applications.
The formula for determining the distance traveled is given by:
Distance = Speed * Time
Create an application having a graphical user interface (GUI) similar to the Main Form shown in Figure 1. When the user clicks the Calculate button, the application displays an InputBox asking the user to enter the speed of the vehicle in miles-per-hour (MPH), followed by a second InputBox asking the user to enter the traveled time (in hours) If the user enters non-numeric data or leaves empty the contents in either of the InputBoxes, then the user receives the appropriate error message in a MessageBox. To finalize the project requirements, the program uses a Loop structure to display in a ListBox the distance that the vehicle traveled during each hour, and the total distance traveled (Figure 5).
In order to meet the Modularity requirement of this project, the SpeedErrorMessage () and TimeErrorMessage () sub procedures must be called from inside the CalculateDistance() routine (sub procedure) and executed only when the user enters non-numeric data into the
corresponding Input Boxes (speed and time). The validation requirements include:
 The vehicle’s speed of travel must be numeric (in MPH) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The vehicle’s time of travel must be numeric (in hours) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The Total travel distance must be numeric.
This is what i got so far:
Public Class Form1
Private Sub lbList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOutput.SelectedIndexChanged
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim intSpeed As Integer
Dim intTime As Integer
Dim intCount As Integer
Dim intDistance As Integer
Dim strDriver As String
Dim strTapeMessage As String
strDriver = InputBox("What's yer name?")
If strDriver = "" Then
DriverErrorMessage(strDriver)
Exit Sub
End If
Try
intSpeed = CInt(InputBox("Enter the vehicle's speed, in MPH.", "Input Needed"))
Catch
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Return
End Try
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
' Validate input.
If intSpeed < 1 Then
' Display error message.
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Else
Try
' Get the number of intHours.
intTime = CInt(InputBox("How many hours have you traveled?", "Input Needed"))
Catch
If intTime < 1 Then
TimeErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
End If
End Try
End If
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
lstOutput.Items.Add(" Vehicle speed: " & intSpeed & " MPH")
lstOutput.Items.Add(" Time traveled: " & intTime & " hours")
lstOutput.Items.Add(" Hours Distance Traveled")
lstOutput.Items.Add(" ----------------------------------------")
For intCount = 1 To intTime
intDistance = intCount * intSpeed
lstOutput.Items.Add(" " & intCount & vbTab & intDistance)
Next intCount
lstOutput.Items.Add(" ----------------------------------------")
' displays the driver's name in upper case, _ the speed of travel, and a Lawful message
If intSpeed <= 60 Then
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at a Lawful speed of " & " " & intSpeed & " " & "MPH")
Else
' displays the driver's name in upper case, the speed of travel, and a Unlawful message
strTapeMessage = " and you got caught on tape!"
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at an UNLAWFUL speed of " & " " & intSpeed _
& " " & "MPH")
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub SpeedErrorMessage()
MessageBox.Show("The speed must must be an integer value", "Input Error")
Return
End Sub
Private Sub TimeErrorMessage()
MessageBox.Show("The travel time must be an integer value in hours", "Input Error")
Return
End Sub
Private Sub DriverErrorMessage(ByVal a As String)
MessageBox.Show("What, your mom didn't name you?", "Input Error")
Return
End Sub
Private Sub Lawfullspeed(ByVal a As String)
MessageBox.Show("Congratulations for being a conformist", "Mr. " & UCase(a) & " " & ".")
Return
End Sub
Private Sub UnLawfullspeed(ByVal a As String)
MessageBox.Show("What's your hurry", "Mr. Big Shot " & UCase(a) & " " & "?")
Return
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
____________________________ Attached:
time.jpg 25 KB (Downloads: 3)
|
|
13-04-2010 at 02:58 PM |
|
|
haxialelite Level: Trainee
 Registered: 09-06-2010 Posts: 3
|
Re: please help me with my vb code???
quote: armyfrenchy wrote:
Can someone please fill in the banks? I am totally confused!! This program needs to list in the listbox the distance a car travelled in miles based on speed and travelled time in hours...with a popup box that appears if the values entered are not numerical? Formula is distance=speed*time. When the user clicks on the button calculate, two Inputboxes need to appear asking for speed and distance. It is supposed to work with listboxes, arrays, and inputboxes, and have validations if user enters string instead of numbers...
I am not sure what to put in the listbox lblist subroutine to make what the user inputted appear? here is the assignment
Loops
 Data validation
 Decision structures
 Sub Procedures
 InputBox Function
 MessageBox Function
 Modularity concept
Program:
This program shall determine the distance that a vehicle travels (in miles) based on the vehicle’s speed (in MPH), and the traveled time (in hours).
NOTE: In most applications, the InputBox function should not be used as the primary method of input because it draws the user’s attention away from the application’s form. It also complicates data validation because if a data entry error occurs, the input box closes not allowing the user for another opportunity for coming back and fixing the error. Despite these drawbacks, it is a convenient tool for developing and testing applications.
The formula for determining the distance traveled is given by:
Distance = Speed * Time
Create an application having a graphical user interface (GUI) similar to the Main Form shown in Figure 1. When the user clicks the Calculate button, the application displays an InputBox asking the user to enter the speed of the vehicle in miles-per-hour (MPH), followed by a second InputBox asking the user to enter the traveled time (in hours) If the user enters non-numeric data or leaves empty the contents in either of the InputBoxes, then the user receives the appropriate error message in a MessageBox. To finalize the project requirements, the program uses a Loop structure to display in a ListBox the distance that the vehicle traveled during each hour, and the total distance traveled (Figure 5).
In order to meet the Modularity requirement of this project, the SpeedErrorMessage () and TimeErrorMessage () sub procedures must be called from inside the CalculateDistance() routine (sub procedure) and executed only when the user enters non-numeric data into the
corresponding Input Boxes (speed and time). The validation requirements include:
 The vehicle’s speed of travel must be numeric (in MPH) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The vehicle’s time of travel must be numeric (in hours) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The Total travel distance must be numeric.
This is what i got so far:
Public Class Form1
Private Sub lbList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOutput.SelectedIndexChanged
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim intSpeed As Integer
Dim intTime As Integer
Dim intCount As Integer
Dim intDistance As Integer
Dim strDriver As String
Dim strTapeMessage As String
strDriver = InputBox("What's yer name?")
If strDriver = "" Then
DriverErrorMessage(strDriver)
Exit Sub
End If
Try
intSpeed = CInt(InputBox("Enter the vehicle's speed, in MPH.", "Input Needed"))
Catch
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Return
End Try
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
' Validate input.
If intSpeed < 1 Then
' Display error message.
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Else
Try
' Get the number of intHours.
intTime = CInt(InputBox("How many hours have you traveled?", "Input Needed"))
Catch
If intTime < 1 Then
TimeErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
End If
End Try
End If
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
--------->COMPUTE() ' call the COMPUTE FUNCTION somewhere here
lstOutput.Items.Add(" Vehicle speed: " & intSpeed & " MPH")
lstOutput.Items.Add(" Time traveled: " & intTime & " hours")
lstOutput.Items.Add(" Hours Distance Traveled")
lstOutput.Items.Add(" ----------------------------------------")
For intCount = 1 To intTime
intDistance = intCount * intSpeed
lstOutput.Items.Add(" " & intCount & vbTab & intDistance)
Next intCount
lstOutput.Items.Add(" ----------------------------------------")
' displays the driver's name in upper case, _ the speed of travel, and a Lawful message
If intSpeed <= 60 Then
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at a Lawful speed of " & " " & intSpeed & " " & "MPH")
Else
' displays the driver's name in upper case, the speed of travel, and a Unlawful message
strTapeMessage = " and you got caught on tape!"
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at an UNLAWFUL speed of " & " " & intSpeed _
& " " & "MPH")
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub SpeedErrorMessage()
MessageBox.Show("The speed must must be an integer value", "Input Error")
Return
End Sub
Private Sub TimeErrorMessage()
MessageBox.Show("The travel time must be an integer value in hours", "Input Error")
Return
End Sub
Private Sub DriverErrorMessage(ByVal a As String)
MessageBox.Show("What, your mom didn't name you?", "Input Error")
Return
End Sub
Private Sub Lawfullspeed(ByVal a As String)
MessageBox.Show("Congratulations for being a conformist", "Mr. " & UCase(a) & " " & ".")
Return
End Sub
Private Sub UnLawfullspeed(ByVal a As String)
MessageBox.Show("What's your hurry", "Mr. Big Shot " & UCase(a) & " " & "?")
Return
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
***You could call "IsNumeric" to verify the user input is a number; such as:
Try
intSpeed = CInt(InputBox("Enter the vehicle's speed, in MPH.", "Input Needed"))
If intSpeed NOT ISNUMERIC OR intSpeed < 1 then
SpeedErrorMessage()
ELSE
RETURN
Catch
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Return
End Try
As far as the computations, you could do this with the strings themselves, such as:
Private Function COMPUTE
intDistance= intSpeed * intTime
'we take intSpeed and multiply it by intTime to get intDistance
'ex: intSpeed(60) *(multiply by) intTime(2) hours = intDistance(120 miles)
'60*2=120
dim strEquation as string
msgbox("You traveled " & intDistance & " in " & intTime & " at " & intSpeed & " MPH!")
strEquation="Traveled " & intDistance & " at " & intSpeed & " MPH in " & intTime
lstOutput.items.add(strEquation)
End Function
'we have already ensured that the required parameters are integers, so we can use a string (strEquation) to hold the data, ready for the listbox. This should give you a good foothold, you can change variable names to input them into the listview also; maybe add them to strEquation in the same manner.
'this should work, off the top of my head. Hope it helps.
[Edited by haxialelite on 09-06-2010 at 11:54 PM GMT]
____________________________
"The tree of knowledge must be refreshed from time to time with perseverance"
|
|
09-06-2010 at 11:52 PM |
|
|
|
|
 |
 |