2006 FINAL PRACTICE PROBLEMS ANSWERS 1. ' Assume that Painter is the default radio button. Public Sub btnSubmit_Click(...) Handles btnSubmit.Click If radElectrician.Value Then txtTotal.Text = txtHoursWorked.Text * 85 ElseIf radPlumber.Value Then txtTotal.Text = txtHoursWorked.Text * 75 Else txtTotal.Text = txtHoursWorked.Text * 40 End If End Sub 2. Variable Trace Table: Output: x y 100 -----+----- 41 150 100 17 50 16 4 16 25 5 12 1 3 3. Assumption (a): One word per line. Public Sub btnSubmit_Click(...) Handles btnSubmit.Click Dim line As String Dim count As Integer = 0 Dim sr As New StreamReader("words.txt") While sr.Peek( ) > -1 line = sr.ReadLine( ) If line.Length = 4 Then count += 1 End If End While sr.Close( ) End Sub Assumption (b): Multiple space delimited words per line. Public Sub btnSubmit_Click(...) Handles btnSubmit.Click Dim line, fields( ), word As String Dim count As Integer = 0 Dim sr As New StreamReader("words.txt") While sr.Peek( ) > -1 line = sr.ReadLine( ) fields = Split(line, " ") For Each word in fields If word.Length = 4 Then count += 1 End If Next End While sr.Close( ) End Sub 4. Public Function ReplaceSpaces(s As String) As String Dim t As String = "" Dim c As String Dim i As Integer For i = 0 To s.Length - 1 c = s.Substring(i, 1) If c = " " Then t &= "*" Else t &= c End If Next Return t End Function Public Sub Main( ) Dim s = "test line" WriteLine(s & " " & ReplaceStrings(s)) s = " " 'Three spaces. WriteLine(s & " " & ReplaceStrings(s)) End Sub 5. Private c As New OleDbConnection Private sc As OleDbCommand Private da As New OleDbDataAdapter Private ds As New Dataset Public Sub Form1_Load(...) Handles MyBase.Load c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\Database\grades.mdb" c.Open( ) sc = c.CreateCommand() sc.CommandType = CommandType.Text sc.CommandText = "SELECT Name FROM GradeData WHERE Grade = 'A'" da.SelectCommand = c Dim n As Integer = da.Fill(ds, "GradeData") Dim i As Integer For i = 0 To n - 1 lstNames.Items.Add(ds.Tables(0).Rows(i).Item(0) Next End Sub 6. Public Sub Main( ) Dim i As Integer Dim r As New Random For i = 1 To 1000 WriteLine(r.Next(0, 2)) Next End Sub 7. Public Sub picDisplay_Paint(...) Handles picDisplay.Paint Dim g As Graphics = e.Graphics Dim rb As New SolidBrush(Color.Red) Dim wb As New SolidBrush(Color.White) Dim gb As New SolidBrush(Color.Blue) g.FillEllipse(rb, 60, 60, 80, 80) g.FillEllipse(wb, 170, 270, 60, 60) g.FillEllipse(bb, 280, 280, 40, 40) End Sub