2 Replies - 1894 Views - Last Post: 09 April 2011 - 05:59 PM Rate Topic: -----

#1 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

batting statistics with function - stuck on function

Posted 09 April 2011 - 11:22 AM

. Create a program to calculate batting statistics.

Hits = singles + doubles + triples + home runs


At bats = plate appearances - sacrifices - base on balls - hit by pitch

Average = hits / at bats and is displayed with three decimals

Total bases = singles + (doubles * 2) + (triples times * 3) + (home runs * 4)

On-base percentage = hits / plate appearances and is displayed with three decimals

Slugging percentage = total bases / at bats and is displayed with three decimals

Use functions and procedures when possible. Your program should have at least one function and one
procedure. Do the proper desk checking to ensure that each calculation is correct.


I've attached a screen shot of my form.



Public Class frmBattingAverage

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        ' Exit and close program
        Me.Close()

    End Sub

    Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click

        ' Declare variables
        Dim Singles As Double
        Dim Doubles As Double
        Dim Triples As Double
        Dim HomeRuns As Double
        Dim Hits As Double
        Dim PlateAppearances As Double
        Dim Sacrifices As Double
        Dim BaseOnBalls As Double
        Dim HitByPitch As Double
        Dim AtBats As Double
        Dim Average As Double
        Dim TotalBases As Double
        Dim OnBasePercentage As Double
        Dim SluggingPercentage As Double

        ' Receive input and associate with variables
        Singles = Val(txtSingles.Text)
        Doubles = Val(txtDoubles.Text)
        Triples = Val(txtTriples.Text)
        HomeRuns = Val(txtHomeRuns.Text)
        Hits = Val(lblHits.Text)
        PlateAppearances = Val(txtPlate.Text)
        Sacrifices = Val(txtSacrifices.Text)
        BaseOnBalls = Val(txtBaseBalls.Text)
        HitByPitch = Val(txtHitPitch.Text)
        AtBats = Val(lblAtBats.Text)
        Average = Val(lblAverage.Text)
        OnBasePercentage = Val(lblOnBasePercent.Text)
        SluggingPercentage = Val(lblSlugPercent.Text)

        ' Perform calculations
        AtBats = PlateAppearances - Sacrifices - BaseOnBalls - HitByPitch
        Average = Hits / AtBats ' display with three decimals
        TotalBases = Singles + (Doubles * 2) + (Triples * 3) + (HomeRuns * 4)
        OnBasePercentage = Hits / PlateAppearances  ' display with three decimals
        SluggingPercentage = TotalBases / AtBats ' display with three decimals    End Sub


    Public Function Hits(ByVal Hits As Double, ByVal Singles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Hits = Singles + Doubles + Triples + Homeruns

    End Function


End Class

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 1
  • +

Replies To: batting statistics with function - stuck on function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: batting statistics with function - stuck on function

Posted 09 April 2011 - 05:41 PM

First of all you should never name your functions the same as your variables or parameters. Your "Hits" function taking a "Hits" parameter? This can get the compiler confused. Your function is also missing the "doubles". So below I made your hits parameter the one for your doubles. Notice now we take the singles, doubles, triples and home runs. Also keep in mind that functions return values. Where VB6 used the functions name to show a return value, in VB.NET we use the return statement....

Public Function Hits(ByVal HitDoubles As Double, ByVal Singles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + HitDoubles + Triples + Homeruns
End Function



Ok, so now we have our hits function take in each of the four variables and a return statement to return the sum of all those hits. Now the next problem is when you call it. When you call this function, you have to give it the four values it needs to work with.

TotalHits = Hits(Doubles, Singles, Triples, HomeRuns)



Here this code is saying give our Hits function four values, the value in the variable Doubles, the variable Singles, the variable Triples and the variable HomeRuns. It will then add them up and "RETURN" the value to be stored into TotalHits.

After this line is called, TotalHits will now have the sum of all the singles, doubles, triples and homeruns.

I hope this makes some sense. :)
Was This Post Helpful? 1
  • +
  • -

#3 CodeWomanSRQ1234  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 34
  • Joined: 08-August 09

Re: batting statistics with function - stuck on function

Posted 09 April 2011 - 05:59 PM

How can I get it to post what its calculated in the label boxes. You can just tell me one and i can figure out the rest. Haven't done this in awhile.

     ' Declare variables
        Dim Singles As Double
        Dim Doubles As Double
        Dim Triples As Double
        Dim HomeRuns As Double
        Dim Hits As Double
        Dim PlateAppearances As Double
        Dim Sacrifices As Double
        Dim BaseOnBalls As Double
        Dim HitByPitch As Double
        Dim AtBats As Double
        Dim Average As Double
        Dim TotalBases As Double
        Dim OnBasePercentage As Double
        Dim SluggingPercentage As Double

        ' Receive input and associate with variables
        Singles = Val(txtSingles.Text)
        Doubles = Val(txtDoubles.Text)
        Triples = Val(txtTriples.Text)
        HomeRuns = Val(txtHomeRuns.Text)
        Hits = Val(lblHits.Text)
        PlateAppearances = Val(txtPlate.Text)
        Sacrifices = Val(txtSacrifices.Text)
        BaseOnBalls = Val(txtBaseBalls.Text)
        HitByPitch = Val(txtHitPitch.Text)
        AtBats = Val(lblAtBats.Text)
        Average = Val(lblAverage.Text)
        OnBasePercentage = Val(lblOnBasePercent.Text)
        SluggingPercentage = Val(lblSlugPercent.Text)

        ' Perform calculations
        AtBats = PlateAppearances - Sacrifices - BaseOnBalls - HitByPitch
        Average = Hits / AtBats ' display with three decimals
        TotalBases = Singles + (Doubles * 2) + (Triples * 3) + (HomeRuns * 4)
        OnBasePercentage = Hits / PlateAppearances  ' display with three decimals
        SluggingPercentage = TotalBases / AtBats ' display with three decimals    
    End Sub

    Public Function Hits(ByVal HitDoubles As Double, ByVal Singles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + HitDoubles + Triples + HomeRuns
    End Function
End Class





View PostMartyr2, on 09 April 2011 - 06:41 PM, said:

First of all you should never name your functions the same as your variables or parameters. Your "Hits" function taking a "Hits" parameter? This can get the compiler confused. Your function is also missing the "doubles". So below I made your hits parameter the one for your doubles. Notice now we take the singles, doubles, triples and home runs. Also keep in mind that functions return values. Where VB6 used the functions name to show a return value, in VB.NET we use the return statement....

Public Function Hits(ByVal HitDoubles As Double, ByVal Singles As Double, ByVal Triples As Double, ByVal Homeruns As Double) As Double
        ' Calculate the number of hits
        Return Singles + HitDoubles + Triples + Homeruns
End Function



Ok, so now we have our hits function take in each of the four variables and a return statement to return the sum of all those hits. Now the next problem is when you call it. When you call this function, you have to give it the four values it needs to work with.

TotalHits = Hits(Doubles, Singles, Triples, HomeRuns)



Here this code is saying give our Hits function four values, the value in the variable Doubles, the variable Singles, the variable Triples and the variable HomeRuns. It will then add them up and "RETURN" the value to be stored into TotalHits.

After this line is called, TotalHits will now have the sum of all the singles, doubles, triples and homeruns.

I hope this makes some sense. :)


Yes! I finally understand loops but functions puzzle me a little. Esp. C++ overloaded functions.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1