Showing posts with label methods. Show all posts
Showing posts with label methods. Show all posts

Tuesday, May 10, 2011

Methods 2.0

Grab the code below and try to arrange the code into logical groups of code.  Here are some pointers:
1. Create a single method that will be responsible for creating the place holder for each of the dice numbers.
 a. You will likely need to pass the label in as an attribute in the method call
2. Create a single method that will be responsible for adding the button and 3 stats text boxes
3. Add a method who's job is just to roll the dice.
4. Add a method who's job is to collect the stats of how many there are of each number
5. Add a method who's job is to update the stats textboxes
6. Add comments to the code blocks to explain what is going on.

 Public Class Form1
  
   'Declare all variables
  
   Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
  
   Dim WithEvents butRoll As New Button
  
   Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
  
   Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
  
   Dim rnd As New Random
  
   Private Sub addDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
  
     'Code labels for dice roll
  
     lbl.Text = 0
  
     lbl.Location = New Point(x, y)
  
     lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
  
     lbl.Height = 40
  
     lbl.Width = 40
  
     Me.Controls.Add(lbl)
  
   End Sub
  
   Private Sub addCounts(ByRef ctxt As TextBox, ByVal c As Integer, ByVal d As Integer, ByVal e As String)
  
     'Code for counters
  
     ctxt.Text = e
  
     ctxt.Location = New Point(c, d)
  
     ctxt.Width = 150
  
     Me.Controls.Add(ctxt)
  
   End Sub
  
   Private Sub addRoll(ByRef butr As Button, ByVal s As String, ByVal t As Integer, ByVal u As Integer)
  
     'Button coding
  
     butRoll.Text = s
  
     butRoll.Location = New Point(t, u)
  
     Me.Controls.Add(butRoll)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Dice rolls
  
     addDice(lblDice1, 10, 20)
  
     addDice(lblDice2, 70, 20)
  
     addDice(lblDice3, 130, 20)
  
     addDice(lblDice4, 190, 20)
  
     addDice(lblDice5, 250, 20)
  
     'Adding counters
  
     addCounts(lblYatzee, 20, 140, "Yatzees: 0")
  
     addCounts(lblFourOfAKind, 20, 180, "Four Of A Kind: 0")
  
     addCounts(lblThreeOfAKind, 20, 220, "Three Of A Kind: 0")
  
     'Add button
  
     addRoll(butRoll, "Roll", 100, 90)
  
   End Sub
  
   Private Sub codeDice(ByRef codelbl As Label)
  
     'Dice code
  
     codelbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub getTotal()
  
     'Declare variable for total
  
     Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
  
     For Each lbl As Label In Me.Controls.OfType(Of Label)()
  
       arrNumbers(lbl.Text - 1) += 1
  
     Next
  
     'Completes 3-of-a-kind, 4-of-a-king, and yahtzee!
  
     For Each i As Integer In arrNumbers
  
       If i = 5 Then
  
         nYatzee += 1
  
       ElseIf i = 4 Then
  
         nFourOfAKind += 1
  
       ElseIf i = 3 Then
  
         nThreeOfAKind += 1
  
       End If
  
     Next
  
   End Sub
  
   Private Sub nameLabel()
  
     'Displays total for 3-of-a-kind, 4-of-a-king, and yahtzee
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     'Call dice roll to form
  
     codeDice(lblDice1)
  
     codeDice(lblDice2)
  
     codeDice(lblDice3)
  
     codeDice(lblDice4)
  
     codeDice(lblDice5)
  
     'Call total to form
  
     getTotal()
  
     'Call display to form
  
     nameLabel()
  
   End Sub
  
 End Class  

Practice 6: Methods

Create the program
that you see on the right. The program
will allow long text to inputted into it through a textbox. When the user clicks "Analyze", it
will do 4 things:
  1. Count the total characters
  2. Count the words
  3. Count the sentences
  1. Count the spaces

I want each of these
counts to be calculated in their own functions. Name these functions according to what is happening inside of it.

You should also have
a sub who's job is to update the text boxes with the proper counts.

You will have to do
some googling to figure out how to do word and sentence counts. Spaces and total chars should be pretty
straight forward.

 Public Class Form1
  
   Public Function SentenceSetup(ByVal InputString As String) As Integer
  
     Dim period, qm, ex As Integer
  
     period = InputString.Split(".").Length
  
     qm = InputString.Split("?").Length
  
     ex = InputString.Split("!").Length
  
     Return (period + qm + ex) / 2
  
   End Function
  
   Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  
     'Count characters
  
     txtChar.Text = AnalyzeText.Text.Length.ToString
  
     'Count words
  
     Dim str As String = AnalyzeText.Text
  
     Dim strA() As String = str.Split(" ")
  
     Dim TChrs As Int32
  
     Dim i As Int32
  
     For i = strA.Length - 1 To 0 Step -1
  
       TChrs += strA(i).Length
  
     Next
  
     txtWords.Text = strA.Length
  
     'Count spaces
  
     Dim Spaces As Integer = 0
  
     For Each s As String In AnalyzeText.Text
  
       If s = " " Then
  
         Spaces += 1
  
       End If
  
     Next
  
     txtSpace.Text = Spaces
  
     'Count Sentences
  
     txtSentence.Text = SentenceSetup(AnalyzeText.Text)
  
   End Sub
  
 End Class