Results 1 to 9 of 9

Thread: Visual Basic Homework Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Location
    St. Louis, MO
    Posts
    8

    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).

    Name:  Form.jpg
Views: 1640
Size:  43.8 KB

    The output should look as follows when using the variables 5 for Height and 34 for Velocity:

    Name:  image.jpg
Views: 1447
Size:  89.4 KB

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Visual Basic Homework Question

    MaxHeight = 0
    Do While MaxHeight >= 0

    BtnTime.PerformClick()
    btnMaxHeight.PerformClick()


    Looks like you've got an eternal loop there to me. MaxHeight surely only has one value so you can't use it as the conditional for the loop. You should be monitoring the actual height for each time frame.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Location
    St. Louis, MO
    Posts
    8

    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.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Visual Basic Homework Question

    Quote Originally Posted by melinda2538 View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Visual Basic Homework Question

    Quote Originally Posted by melinda2538 View Post
    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
  •  



Click Here to Expand Forum to Full Width