|
-
Apr 9th, 2013, 06:47 PM
#1
Thread Starter
New Member
Visual Basic Homework Question
Hello All,
We're using VB 2010 in class and I have listed the homework problem below
6. Write a program to provide information on the height of a ball thrown straight up into the air. The program should request the initial height, h feet, and the initial velocity,v feet per second, as input. 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 observe 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.
(d) Quit.
Here is my code:
Public Class frmProjectile
Dim MaxHeight As Double
Dim InitialHeight As Double
Dim Velocity As Double
Dim Time As Double
Private Sub btnHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaxHeight.Click
MaxHeight = Velocity / 32
End Sub
Private Sub BtnTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTime.Click
MaxHeight = InitialHeight + Velocity * Time - 16 * Time ^ 2
End Sub
Private Sub btnTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTable.Click
InitialHeight = CDbl(txtHeight.Text)
Velocity = CDbl(txtVelocity.Text)
Time = 0
MaxHeight = 0
Do While MaxHeight >= 0
BtnTime.PerformClick()
btnMaxHeight.PerformClick()
lstOutput.Items.Add(Time & " " & MaxHeight)
Time = Time + 0.25
Loop
End Sub
End Class
When I do my step into, I can see that it's working (or trying to!), but nothing shows in my list box (included a screenshot).

The output should look as follows when using the variables 5 for Height and 34 for Velocity:
-
Apr 9th, 2013, 07:02 PM
#2
-
Apr 9th, 2013, 07:16 PM
#3
Frenzied Member
Re: Visual Basic Homework Question
you have an eternal loop
maxheight >0 ?
Check what logic you are really after something tells me you have misunderstood something in the assignment
-
Apr 9th, 2013, 07:55 PM
#4
Re: Visual Basic Homework Question
Let's look closely at what you've got here:
Code:
MaxHeight = 0
Do While MaxHeight >= 0
BtnTime.PerformClick()
btnMaxHeight.PerformClick()
lstOutput.Items.Add(Time & " " & MaxHeight)
Time = Time + 0.25
Loop
Let us for simplicity reasons exchange the PerformClick calls with the calculations you have for those buttons and we get this:
Code:
MaxHeight = 0
Do While MaxHeight >= 0
MaxHeight = InitialHeight + Velocity * Time - 16 * Time ^ 2
MaxHeight = Velocity / 32
lstOutput.Items.Add(Time & " " & MaxHeight)
Time = Time + 0.25
Loop
Now look closely at the lines where you change the value of MaxHeight.
Code:
MaxHeight = InitialHeight + Velocity * Time - 16 * Time ^ 2
MaxHeight = Velocity / 32
On the first line you do some calculation and MaxHeight is changed to the result of that, but on the second line you change it again. So MaxHeight will always be Velocity / 32, which BTW is not the maximum height the ball will reach but the number of second it will take to get there. So the number you output will always be Velocity / 32 which, unless Velocity is a negative number, will always be above or equal to 0, so your loop will never end.
-
Apr 9th, 2013, 09:31 PM
#5
Thread Starter
New Member
Re: Visual Basic Homework Question
Thanks all for the feedback. This assignment has been very frustrating for me. My husband and friend both say 4 buttons are totally unnecessary as it can all be performed with just one. I'm also not the most math oriented person when it comes to creating formulas from poorly worded textbook problems! This insight was very helpful.
-
Apr 10th, 2013, 01:04 AM
#6
Re: Visual Basic Homework Question
 Originally Posted by melinda2538
My husband and friend both say 4 buttons are totally unnecessary as it can all be performed with just one.
I agree with them. In fact, I wouldn't use any buttons at all but as you implied, its school assignment so it may be necessary to show your teacher that you have an idea of what you're doing.
-
Apr 10th, 2013, 03:28 AM
#7
Re: Visual Basic Homework Question
Just a bit of math(s) help: you can work out the height reached like this. When the ball goes up, its starting velocity is v and its final velocity is 0. Since the ball undergoes constant deceleration due to gravity, the average velocity is v/2. It keeps up that average speed for v/32 seconds so the maximum height is .... 
BB
-
Apr 10th, 2013, 09:34 AM
#8
Re: Visual Basic Homework Question
A different approach
Code:
Const a As Double = -32 'gravity
Dim vo As Double = 34 'initial velocity
Dim ttop As Double = -(vo / a) 'time until max height
Dim xo As Double = 5 'initial height
Dim xmax As Double = ((vo * ttop) + (a * ttop ^ 2) / 2) + xo 'max height
Dim t As Double = 0.0 'time
Dim xf As Double = 0 'height at t
Dim tinc As Double = 0.005 '0.1 ' 0.25 ' ttop / 10
Debug.WriteLine(String.Format("Time {0}, Max Height {1}", ttop.ToString("n3"), xmax.ToString("n8")))
Do
'compute height at time t
xf = (a * t ^ 2) / 2 + vo * t + xo
If xf < 0 Then
Exit Do
End If
Debug.WriteLine(String.Format("Time {0}, Height {1}", t.ToString("n3"), xf.ToString("n8")))
t += tinc
Loop While xf >= 0
This is in feet. If meters change constant a to 9.8. Physics 
@boops^2 - The velocity decreases to 0 on the way up, and then increases on the way down. Acceleration remains the same.
@OP - I live in Jefferson City.
Last edited by dbasnett; Apr 10th, 2013 at 11:20 AM.
-
Apr 24th, 2013, 12:49 AM
#9
Re: Visual Basic Homework Question
 Originally Posted by melinda2538
This assignment has been very frustrating for me. My husband and friend both say 4 buttons are totally unnecessary as it can all be performed with just one.
In a real-world application I would intend to agree with them, however in this case even though the calculation for the 3 buttons (the forth is an Exit or Quit button) are very similar, they are supposed to produce different output. Besides it's a school assignment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|