Lecture 7 Code Samples Eric J. Schwabe IT 236 Spring 2008
Lecture 7 Code Samples Eric J. Schwabe IT 236 Spring 2008
Eric J. Schwabe
IT 236 Spring 2008
=====
' CeilingFunction.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a user-defined function to round numbers up
Option Strict On
' When the button is clicked, the value in the upper text box
' is stored in a variable, and the Ceiling function is called
' to round it up to the next integer. The result is displayed
' in the lower text box.
newNumber = Ceiling(oldNumber)
outTextBox.Text = newNumber.ToString()
End Sub
Return result
End Function
End Class
=====
=====
' CountLetters.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Sample for Lecture 6 in-class example
Option Strict On
' When the button is clicked, counts the number of each vowel and the total number of
' consonants in an input string, and reports these values. This process is repeated
' until the user enters an empty string.
Do
aCount = 0
eCount = 0
iCount = 0
oCount = 0
uCount = 0
consonantCount = 0
For i = 0 To n - 1
letter = input.Substring(i, 1)
letter = letter.ToUpper
' outputListBox.Items.Add(i.ToString() & " : " & letter)
Select Case letter
Case "A"
aCount = aCount + 1
Case "E"
eCount = eCount + 1
Case "I"
iCount = iCount + 1
Case "O"
oCount = oCount + 1
Case "U"
uCount = uCount + 1
Case Else
If isConsonant(letter) Then
consonantCount = consonantCount + 1
End If
End Select
Next
End Sub
' This function returns a true if the input argument is an upper case consonant,
' and false otherwise
Return result
End Function
End Class
=====
=====
' PizzaPrice.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a user-defined function
Option Strict On
' When the button is clicked, read the pizza diameter and price
' from the two text boxes, and call the PricePerSquareInch function
' to compute the unit cost per square inch
outputListBox.Items.Add(output)
End Sub
' Given a diameter d and price p (given in dollars), computes the price
' per square inch (returned in cents)
' MessageBox.Show("d is " & d.ToString() & " , p is " & p.ToString, "Input
Arguments")
Return cost
End Function
End Class
=====
=====
' LandsEnd.sln
' Eric J. Schwabe
' IT 236 Winter 2008
' Simple demonstration of arrays of strings and numbers
Option Strict On
itemPrice(0) = 39.0
itemPrice(1) = 21.0
itemPrice(2) = 7.75
itemPrice(3) = 33.0
itemPrice(4) = 26.0
End Sub
' Displays all item names and prices when the button is clicked
'Dim i As Integer
'For i = 0 To 4
' output = itemName(i) + ": " + itemPrice(i).ToString("C")
' outputListBox.Items.Add(output)
' Next
'Dim i As Integer
'Do
' i = Integer.Parse(InputBox("Enter an index:", "ARRAY INDEX PROMPT"))'
'
' doing bounds checking...
' If (i >= 0) And (i <= 4) Then
'output = itemName(i) + ": " + itemPrice(i).ToString("C")
'outputListBox.Items.Add(output)
'End If
'
' Loop Until (i = -1)
' ... finally, traversing the entire array (names only) with a For Each loop...
' ... note that in this case, we do not know the index of the element...
End Sub
End Class
=====
=====
' Searching.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrate linear and binary searches of arrays
Option Strict On
'Dim value() As Integer = {1, 3, 5, 8, 12, 15, 21, 22, 28, 34, 41, 49, _
' 55, 88, 91, 99}
'Dim size As Integer = 16
' Linear search: Check all positions of the array in order, keeping
' track of whether or not (and if so, where) the target value was found
Dim i As Integer
Dim found As Boolean = False
Dim position As Integer
Dim target As Integer = Integer.Parse(inTextBox.Text)
outputListBox.Items.Clear()
For i = 0 To size - 1
outputListBox.Items.Add("Position " & i & ":")
If value(i) = target Then
position = i
found = True
outputListBox.Items.Add(" YES!")
Else
outputListBox.Items.Add(" No.")
End If
Next
End Sub
' Binary Search: For a sorted array, repeatedly check the middle element,
' and if it is not the target value, eliminate half of the values from
' consideration and try again. When either the target value is found or
' there are no elements unchecked, end the search and report the results.
outputListBox.Items.Clear()
If (found) Then
outputListBox.Items.Add("Target found in position " & position)
Else
outputListBox.Items.Add("Target not found")
End If
End Sub
End Class
=====
=====
' MultiplicationTable.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a two-dimensional array
Option Strict On
' Prompt user for a positive value for the rows to display
Do
rows = Integer.Parse(InputBox("Enter the number of rows:", "ROW PROMPT"))
Loop Until (rows >= 1) And (rows <= 10)
' Prompt user for a positive value for the columns to display
Do
columns = Integer.Parse(InputBox("Enter the number of columns:", "COLUMN
PROMPT"))
Loop Until (columns >= 1) And (columns <= 10)
End Sub
End Class
=====
=====
' CurrencyExchange.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Uses a two-dimensional array to store exchange rates
Option Strict On
' When the button is clicked, reads the currencies in the two text boxes
' and uses them to find the correct row and column in the table of
' rates. If both currencies are valid, reads the input money amount
' and converts is using the appropriate exchange rate.
End Sub
' Clear the output text box when the contents of any
' input text box are changed
End Class
=====
=====
' RandomTest.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Test quality of random number generator using a control array
Option Strict On
' Assigns the six text boxes to the six elements of the control array
' Generates 6000 random numbers from 1 to 6, and keeps track of the number
' of occurrences of each number in the array count. Displays the values
' in the count array in the six text boxes.
For i = 1 To 6
count(i) = 0
Next
For i = 1 To trials
nextOne = generator.Next(1, 7) ' does not include 7!
count(nextOne) = count(nextOne) + 1
Next
For i = 1 To 6
output(i).Text = count(i).ToString()
Next
End Sub
End Class
=====