Using Visual Basic, write a program to provide information on the height of a ball thrown straight up into the air. the program should request as input the initial height, h feet, and the initial velocity, v feet per second. The height of the ball (in feet) after t seconds is given by the formula h + vt – 16t^2 feet. The four options to be provided by buttons are as follows:
(a) Determine the maximum height of the ball. Note: The ball will reach its maximum height after v/32 seconds.
(b) Determine approximately when the ball will hit the ground. Hint: Calculate the height after every .1 second and determine when the height is no longer a positive number.
(c) Display a table showing the height of the ball every quarter second for five seconds or until it hits the ground. See the figure below.
(d) Quit
Design the Interface:
First we need to design the interface. The following image shows an example of what the form might look like when the program runs.

Hints:
Use a sub-procedure to input the initial height and initial velocity. Be sure to validate your input and make sure that negative values produce an error message.
The formula in Visual Basic code can be written as follows: height = h0 + v0 * t – 16 * t * t
The ball will reach its maximum height after v0 / 32 seconds. Use a function to compute the maximum height of the ball.
Use another function to determine when the ball will hit the ground.
Use a sub-procedure to determine the height of the ball at every quarter second and display the results in the list box.
Use the Courier New font to make sure that all of your results line up in the list box.
Suggested Control Names and Attributes:
| Name Property | Text Property | Control Type | Notes |
| frmBall | Projectile Motion | Form | Holds Controls |
| txtInitialHeight | Text Box | Captures initial height of ball | |
| txtInitialVelocity | Text Box | Captures initial velocity of ball | |
| btnMaxHeight | Determine Maximum Height | Button | Triggers event to display maximum height |
| btnTable | Display Table | Button | Triggers event to display table of heights |
| btnApproxTime | Determine Approximate Time until Ball Hits the Ground | Button | Triggers event to display time before ball hits ground. |
| btnQuit | Quit | Button | Triggers event to end program |
| lstOutput | ListBox | Displays data requested by user |
Write the Code:
' Project: Projectile Motion
' Description: Receives user input for initial height and velocity of a ball.
' Performs calculations and displays them based on which button is clicked.
' First button calculates the maximum height of the ball.
' Second button calculates the time until the ball hits the ground.
' Third button displays a table with the height every .25 seconds.
' Fourth button allows the user to quit the program.
Public Class frmBall
' Declare global variables
Dim ballHeight As Double = 0
Dim h0 As Double = 0 ' Initial height
Dim v0 As Double = 0 ' Initial velocity
Dim t As Double = 0 ' Time
Dim valid As Boolean = True ' variable to make sure input is valid before performing calculations
Private Sub btnMaxHeight_Click(sender As Object, e As EventArgs) Handles btnMaxHeight.Click
' Calls function to compute the maximum height if input is valid.
' Otherwise waits for valid input
lstOutput.Items.Clear()
InputData()
If valid = True Then
lstOutput.Items.Add("The maximum height is ")
lstOutput.Items.Add(maxHeight() & " feet.")
End If
End Sub
Private Sub btnApproxTime_Click(sender As Object, e As EventArgs) Handles btnApproxTime.Click
' Calls function to compute the time before the ball hits the ground if input is valid.
' Otherwise waits for valid input
lstOutput.Items.Clear()
InputData()
If valid = True Then
lstOutput.Items.Add("The ball will hit the ground after")
lstOutput.Items.Add("approximately " & timeInAir() & " seconds.")
End If
End Sub
Private Sub btnTable_Click(sender As Object, e As EventArgs) Handles btnTable.Click
lstOutput.Items.Clear()
InputData()
If valid = True Then
DisplayTable()
End If
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
' Ends the program when user clicks on Quit button
End
End Sub
Sub InputData()
' Validates input from the user and assigns values to appropriate variables
valid = True ' Initialize to true in case of prior invalid input
If IsNumeric(txtInitialHeight.Text) Then
If CInt(txtInitialHeight.Text) >= 0 Then
h0 = CInt(txtInitialHeight.Text)
Else
MessageBox.Show("You entered a negative number. Please try again.")
valid = False
End If
Else
MessageBox.Show("You did not enter a numeric value. Please try again.")
valid = False
End If
If IsNumeric(txtInitialVelocity.Text) Then
If CInt(txtInitialVelocity.Text) >= 0 Then
v0 = CInt(txtInitialVelocity.Text)
Else
MessageBox.Show("You entered a negative number. Please try again.")
valid = False
End If
Else
MessageBox.Show("You did not enter a numeric value. Please try again.")
valid = False
End If
End Sub
Function maxHeight() As Double
' Calculates the maximum height of the ball
t = v0 / 32
Return CDbl(h0 + (v0 * t) - (16 * t * t))
End Function
Function timeInAir() As Double
' Calulates the time before the ball hits the ground.
t = 0 ' Initialize time varible to 0 in case of previous calculations
ballHeight = CDbl(h0 + (v0 * t) - (16 * t * t))
Do
t += 0.1
ballHeight = CDbl(h0 + (v0 * t) - (16 * t * t))
Loop Until (ballHeight <= 0)
Return t
End Function
Sub DisplayTable()
t = 0 ' initialize the time variable in case of prior input
ballHeight = CDbl(h0 + (v0 * t) - (16 * t * t))
lstOutput.Items.Add("Time" & vbTab & "Height")
' display the table at time and height equal zero before entering the loop
' to prevent premature end of loop
lstOutput.Items.Add(t.ToString("N2") & vbTab & ballHeight)
Do
t += 0.25
ballHeight = CDbl(h0 + (v0 * t) - (16 * t * t))
lstOutput.Items.Add(t.ToString("N2") & vbTab & ballHeight.ToString("N1"))
Loop Until ((ballHeight <= 0) Or (t = 5))
End Sub
End Class