Friday, May 6, 2011

Methods 1 - Text Analyzer

 Public Class Form1
  
   Dim nwordcount As Integer
  
   Dim ncharcount As Integer
  
   Dim nspacecount As Integer
  
   Dim nsentencecount As Integer
  
   Public Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  
     Dim strMaterial As String = txtMaterial.Text
  
     ncharcount = GetCharacterCount(strMaterial)
  
     nsentencecount = GetSentenceCount(strMaterial)
  
     nspacecount = GetSpaceCount(strMaterial)
  
     nwordcount = GetWordCount(strMaterial)
  
     RefreshTextboxes()
  
   End Sub
  
   Private Function GetCharacterCount(ByVal str As String) As Integer
  
     Return str.Length
  
   End Function
  
   Private Function GetSentenceCount(ByVal str As String) As Integer
  
     Dim counter As Integer = 0
  
     For Each ending As String In str
  
       If ending.Contains(".") Or ending.Contains("?") Or ending.Contains("!") Then
  
         counter += 1
  
       End If
  
     Next
  
     Return counter
  
   End Function
  
   Private Function GetSpaceCount(ByRef str As String) As Integer
  
     Dim counter As Integer = 0
  
     For Each Space As String In str
  
       If Space.Contains(" ") Then
  
         counter += 1
  
       End If
  
     Next
  
     Return counter
  
   End Function
  
   Private Function GetWordCount(ByRef str As String) As Integer
  
     Dim counter As Integer = System.Text.RegularExpressions.Regex.Matches(str, "\S+").Count
  
     Return counter
  
   End Function
  
   Public Sub RefreshTextboxes()
  
     txtCharacters.Text = ncharcount
  
     txtSentences.Text = nsentencecount
  
     txtSpaces.Text = nspacecount
  
     txtWords.Text = nwordcount
  
   End Sub
  
 End Class
  

Methods 2 - Yahtzee

 Public Class Form1
  
   'Dim variables used in different Methods
  
   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)
  
     'Method for adding the differe labels to display the numbers rolled
  
     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 AddOutput(ByRef lbl As TextBox, ByVal outcome As String, ByVal x As Integer, ByVal y As Integer)
  
     'Method of adding the output from the roll into the label
  
     lbl.Text = outcome
  
     lbl.Location = New Point(x, y)
  
     lbl.Width = 150
  
     Me.Controls.Add(lbl)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Adding the labels to display the dice
  
     AddDice(lblDice1, 10, 20)
  
     AddDice(lblDice2, 70, 20)
  
     AddDice(lblDice3, 130, 20)
  
     AddDice(lblDice4, 190, 20)
  
     AddDice(lblDice5, 250, 20)
  
     'Adding the actual numbers that have been rolled into the dice labels
  
     AddOutput(lblYatzee, "Yahtzee: 0", 20, 140)
  
     AddOutput(lblFourOfAKind, "Four Of A Kind: 0", 20, 180)
  
     AddOutput(lblThreeOfAKind, "Three Of A Kind: 0", 20, 220)
  
     'Creating the Roll button
  
     butRoll.Text = "Roll"
  
     butRoll.Location = New Point(100, 90)
  
     'Adding the roll button and output lables
  
     Me.Controls.Add(butRoll)
  
     Me.Controls.Add(lblFourOfAKind)
  
     Me.Controls.Add(lblThreeOfAKind)
  
   End Sub
  
   Private Sub roll(ByRef lbl As Label)
  
     'Method telling how to roll the dice
  
     lbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     'Rolling the Dice
  
     roll(lblDice1)
  
     roll(lblDice2)
  
     roll(lblDice3)
  
     roll(lblDice3)
  
     roll(lblDice4)
  
     roll(lblDice5)
  
     'Getting the stats from rolling the dice
  
     stats()
  
     'Updating the textboxes as to what was rolled
  
     UpdateTextboxes()
  
   End Sub
  
   Private Sub stats()
  
     'Creating an array to keep track of what was rolled and deciding if a three of a kind, four of a kind, or yahtzee
  
     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
  
     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 UpdateTextboxes()
  
     'Method stating to update the text boxes after each roll
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
 End Class
  

Subquery Practice

 use AdventureWorks
  
 select PC.FirstName, PC.LastName, PC.EmailAddress, HE.Title
  
 from Person.Contact PC
  
 inner join HumanResources.Employee HE
  
 on PC.ContactID = HE.ContactID
  
 where HE.EmployeeID in
  
      (
  
      select SS.SalesPersonID
  
      from Sales.SalesPerson SS
  
      )
  
 select SP.salespersonID, PC.EmailAddress
  
 from Sales.SalesPerson SP
  
 inner join HumanResources.Employee HRE
  
 on SP.SalesPersonID = HRE.EmployeeID
  
 inner join Person.Contact PC
  
 on HRE.ContactID = PC.ContactID
  
 where SP.SalesPersonID in 
  
      (
  
      select SOH.SalesPersonID
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.TotalDue > '150000'
  
      )
  
 select PP.productID, (PP.ListPrice - PP.StandardCost) MarkUp
  
 from Production.Product PP
  
 where PP.ProductID not in
  
      (
  
      select SOD.ProductID
  
      from Sales.SalesOrderDetail SOD
  
      )
  
 order by MarkUp desc
  
 use AdventureWorks
  
 declare @top_sales_person int
  
 set @top_sales_person = 
  
      (
  
      select top 1 SP.SalesPersonID
  
      from Sales.SalesPerson SP
  
      order by SP.SalesLastYear desc
  
      )
  
 declare @customers int
  
 set @customers =
  
      (
  
      select COUNT (distinct customerid)
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.SalesPersonID = @top_sales_person
  
      )
  
 declare @total_sales int
  
 set @total_sales =
  
      (select COUNT (distinct SOH.SalesOrderID)
  
      from Sales.SalesOrderHeader SOH
  
      where SOH.SalesPersonID = @top_sales_person
  
      )
  
 select PC.FirstName, PC.LastName, DATEDIFF ( year, HRE.HireDate, GETDATE() ) Tenure, @customers Customers 
  
 , @total_sales Total_Sales
  
 from Person.Contact PC
  
 inner join HumanResources.Employee HRE
  
 on PC.ContactID = HRE.ContactID
  
 inner join Sales.SalesPerson SP
  
 on HRE.EmployeeID = SP.SalesPersonID
  
 where SP.SalesPersonID = @top_sales_person  

Monday, May 2, 2011

Programming Final

Calculator

 Public Class Form1
  
   Dim number1 As Decimal
  
   Dim number2 As Decimal
  
   Dim use As Integer
  
   Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
  
     txtDisplay.Text = txtDisplay.Text & btn1.Text
  
   End Sub
  
   Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
  
     txtDisplay.Text = txtDisplay.Text & btn2.Text
  
   End Sub
  
   Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
  
     txtDisplay.Text = txtDisplay.Text & btn3.Text
  
   End Sub
  
   Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
  
     txtDisplay.Text = txtDisplay.Text & btn4.Text
  
   End Sub
  
   Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
  
     txtDisplay.Text = txtDisplay.Text & btn5.Text
  
   End Sub
  
   Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
  
     txtDisplay.Text = txtDisplay.Text & btn6.Text
  
   End Sub
  
   Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
  
     txtDisplay.Text = txtDisplay.Text & btn7.Text
  
   End Sub
  
   Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
  
     txtDisplay.Text = txtDisplay.Text & btn8.Text
  
   End Sub
  
   Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
  
     txtDisplay.Text = txtDisplay.Text & btn9.Text
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     txtDisplay.Text = " "
  
   End Sub
  
   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 1
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 2
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 3
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
  
     number1 = number1 + Val(txtDisplay.Text)
  
     use = 4
  
     txtDisplay.Clear()
  
   End Sub
  
   Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
  
     number2 = number2 + Val(txtDisplay.Text)
  
     If use = 1 Then
  
       txtDisplay.Text = number1 + number2
  
     ElseIf use = 2 Then
  
       txtDisplay.Text = number1 - number2
  
     ElseIf use = 3 Then
  
       txtDisplay.Text = number1 * number2
  
     ElseIf use = 4 Then
  
       txtDisplay.Text = number1 / number2
  
     End If
  
     number1 = 0
  
     number2 = 0
  
     use = 0
  
   End Sub
  
   Private Sub btnDeciamal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeciamal.Click
  
     txtDisplay.Text = txtDisplay.Text & btnDeciamal.Text
  
   End Sub
  
 End Class