TEST!!!!!
CS105 Supplemental Material
Lab 2: Addition
Provided Code:
Public Class Addition
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
Dim number1 As Integer 'first number entered by user
Dim number2 As Integer 'second number entered by user
Dim total As Integer 'sum of the two Integers
'collect user input
number1 = number1TextBox.Text 'get first number entered
number2 = number2TextBox.Text 'get second number entered
'calculate
total = number1 + number2 'add the two numbers
'display results to user
resultLabel.Text = “The sum is “ & total
End Sub
End Class
Lean Code:
Public Class Addition
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
'NOTE ABOUT CInt():
'CInt() takes a String and converts it to an int.
'Declaring a variable as Integer type Then assigning a String containing a number to it does this automatically.
'When not using explicitly declared variable types, using CInt() is a good idea for safety.
resultLabel.Text = CInt(number1TextBox.Text) + CInt(number2TextBox.Text)
End Sub
End Class
Hybrid Code:
Public Class Addition
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
Dim num1 As Integer = number1TextBox.Text, num2 As Integer = number2TextBox.Text
resultLabel.Text = num1 + num2
End Sub
End Class
Lab 3: GPA Average
Provided Code:
Public Class Average_GPA
Private Sub avgBtn_Click(sender As Object, e As EventArgs) Handles avgBtn.Click
Dim gpa1 As Double
Dim gpa2 As Double
Dim avgGpa As Double
gpa1 = gpa1TxtBox.Text
gpa2 = gpa2TxtBox.text
avgGpa = (gpa1 + gpa2) / 2.0
avgLbl.Text = avgGpa
End Sub
End Class
Lean Code:
Public Class Average_GPA
Private Sub avgBtn_Click(sender As Object, e As EventArgs) Handles avgBtn.Click
avgLbl.Text = (CDbl(gpa1TxtBox.Text) + CDbl(gpa2TextBox.Text)) / 2.0
End Sub
End Class
Hybrid Code:
Public Class Average_GPA
Private Sub avgBtn_Click(sender As Object, e As EventArgs) Handles avgBtn.Click
Dim gpa1 As Double = gpa1TxtBox.Text, gpa2 As Double = gpa2TextBox.Text
avgLbl.Text = (gpa1 + gpa2) / 2.0
End Sub
End Class
Lab 4: Decision Intro
Provided Code:
Public Class DecisionIntro
Private Sub modifyIntBtn_Click(sender As Object, e As EventArgs) Handles modifyIntBtn.Click
Dim intToMod As Integer
Dim msgStr As String
intToMod = Me.intTxt.Text
If (intToMod > 0) Then
intToMod = intToMod + 2
Else
intToMod = intToMod - 2
End If
msgStr = "The modified Integer is " & intToMod
MsgBox(msgStr)
End Sub
Private Sub testResultBtn_Click(sender As Object, e As EventArgs) Handles testResultBtn.Click
Const PASS_CUTTOFF As Integer = 60
Dim studentGrade As Integer
Dim passStr As String
studentGrade = Me.grdTxt.Text
If studentGrade >= PASS_CUTTOFF Then
passStr = "The sudent passed!"
Else
passStr = "The student diud not pass."
End If
MsgBox(passStr)
End Sub
End Class
Lean Code:
Public Class DecisionIntro
Private Sub modifyIntBtn_Click(sender As Object, e As EventArgs) Handles modifyIntBtn.Click
If CInt(Me.intTxt.Text) >= 0 Then MsgBox(CInt(Me.intTxt.Text) + 2) Else MsgBox(CInt(Me.intTxt.Text) - 2)
End Sub
Private Sub testResultBtn_Click(sender As Object, e As EventArgs) Handles testResultBtn.Click
Const PASS_CUTTOFF As Integer = 60
If CInt(Me.grdTxt.Text) >= PASS_CUTTOFF Then MsgBox("The student passed!") Else MsgBox("The student did not pass.")
End Sub
End Class
Hybrid Code:
Public Class DecisionIntro
Private Sub modifyIntBtn_Click(sender As Object, e As EventArgs) Handles modifyIntBtn.Click
Dim intToMod As Integer = Me.intTxt.Text
If intToMod >= 0 Then
MsgBox(intToMod + 2)
Else
MsgBox(intToMod - 2)
End If
End Sub
Private Sub testResultBtn_Click(sender As Object, e As EventArgs) Handles testResultBtn.Click
Const PASS_CUTTOFF As Integer = 60
Dim studentGrade As Integer = Me.grdTxt.Text
If studentGrade >= PASS_CUTTOFF Then
MsgBox("The student passed!")
Else
MsgBox("The student did not pass.")
End If
End Sub
End Class
Lab 5: What To Wear
Provided Code:
Public Class WhatToWear
Private Sub decideBtn_Click(sender As Object, e As EventArgs) Handles decideBtn.Click
Const COAT_CUTOFF As Integer = 45
Const JACKET_CUTOFF As Integer = 75
Dim inputTemp As Integer
Dim outputMsg As String
inputTemp = Me.tempTxt.Text
If inputTemp < COAT_CUTOFF Then
outputMsg = "It is chilly, wear a coat."
ElseIf inputTemp < JACKET_CUTOFF Then
outputMsg = "It is not warm, seems like jacket weather."
Else
outputMsg = "It is very warm, you don't need a jacket or coat."
End If
Me.resultLbl.Text = outputMsg
End Sub
End Class
Lean Code:
Public Class WhatToWear
Private Sub decideBtn_Click(sender As Object, e As EventArgs) Handles decideBtn.Click
Const COAT_CUTOFF As Integer = 45, JACKET_CUTOFF As Integer = 75
If CInt(Me.tempTxt.Text) < COAT_CUTOFF Then
Me.resultLbl.Text = "It is chilly, wear a coat."
ElseIf CInt(Me.tempTxt.Text) < JACKET_CUTOFF Then
Me.resultLbl.Text = "It is not warm, seems like jacket weather."
Else
Me.resultLbl.Text = "It is very warm, you don't need a jacket or coat."
End If
End Sub
End Class
Hybrid Code:
Public Class WhatToWear
Private Sub decideBtn_Click(sender As Object, e As EventArgs) Handles decideBtn.Click
Const COAT_CUTOFF As Integer = 45, JACKET_CUTOFF As Integer = 75
Dim currentTemp As Integer = Me.resultLbl.Text
If currentTemp < COAT_CUTOFF Then
Me.resultLbl.Text = "It is chilly, wear a coat."
ElseIf currentTemp < JACKET_CUTOFF Then
Me.resultLbl.Text = "It is not warm, seems like jacket weather."
Else
Me.resultLbl.Text = "It is very warm, you don't need a jacket or coat."
End If
End Sub
End Class
Lab 6: Get Letter Grade
Provided Code:
Public Class GetLetterGrade
Private Sub grdBtn_Click(sender As Object, e As EventArgs) Handles grdBtn.Click
Const A_CUTOFF As Integer = 90
Const B_CUTOFF As Integer = 80
Const C_CUTOFF As Integer = 70
Const D_CUTOFF As Integer = 60
Dim studentGrade As Integer
Dim letterGrade As String
studentGrade = Me.numGrdTxt.Text
letterGrade = "The student earned "
If (studentGrade < D_CUTOFF) Then
letterGrade += "an F"
ElseIf (studentGrade < C_CUTOFF) Then
letterGrade += "a D"
ElseIf (studentGrade < B_CUTOFF) Then
letterGrade += "a C"
ElseIf (studentGrade < A_CUTOFF) Then
letterGrade += "a B"
Else
letterGrade += "an A"
End If
Me.letGrdLbl.Text = letterGrade
End Sub
End Class
Lean Code:
Public Class GetLetterGrade
Private Sub grdBtn_Click(sender As Object, e As EventArgs) Handles grdBtn.Click
Const A_CUTOFF As Integer = 90, B_CUTOFF As Integer = 80, C_CUTOFF As Integer = 70, D_CUTOFF As Integer = 60
Select Case CInt(numGrdTxt.Text)
Case Is < D_CUTOFF
Me.letGrdLbl.Text = "The student earned an F"
Case Is < C_CUTOFF
Me.letGrdLbl.Text = "The student earned a D"
Case Is < B_CUTOFF
Me.letGrdLbl.Text = "The student earned a C"
Case Is < A_CUTOFF
Me.letGrdLbl.Text = "The student earned a B"
Case Else
Me.letGrdLbl.Text = "The student earned an A"
End Select
End Sub
End Class
Hybrid Code:
See above. A Select Case statement is (in my opinion) the best way to solve a conditional with more than 3 options.
Lab 7: Even Numbers
Provided Code:
Public Class EvenNumbers
Private Sub loopBtn_Click(sender As Object, e As EventArgs) Handles loopBtn.Click
Dim counter As Integer
Dim flag As Boolean
Dim currentHigh As Integer
Dim highest As Integer
Dim numlist As String
highest = Me.maxTxt.Text
currentHigh = 0
counter = 1
flag = highest > currentHigh
Do While flag
counter += 1
numlist += currentHigh & ", "
currentHigh += 2
flag = highest >= currentHigh
Loop
Me.displayNumsTxt.Text = numlist
End Sub
End Class
Lean Code:
Public Class EvenNumbers
Private Sub loopBtn_Click(sender As Object, e As EventArgs) Handles loopBtn.Click
Me.displayNumsTxt.Text = "" 'Clear the output in case multiple calculations are run
For counter = 0 To CInt(Me.maxTxt.Text) Step 2
Me.displayNumsTxt.Text += counter & ", "
Me.howManyLbl.Text = (counter / 2) + 1
Next
End Sub
End Class
Hybrid Code:
See above. This is a great problem for a FOR loop, where you know the starting, ending, and incremment conditions
Lab 8: Golden Ratio
Provided Code:
Public Class GoldenRatio
Private Sub fibsBtn_Click(sender As Object, e As EventArgs) Handles fibsBtn.Click
Dim fib1 As Integer
Dim fib2 As Integer
Dim fib3 As Integer
Dim numFibs As Integer
Dim flag As Boolean
Dim display As String
fib1 = 0
fib2 = 1
fib3 = 1
numFibs = Me.userInputTxt.Text
Me.numFibsLbl.Text = numFibs
display = fib1 & ", " & fib2 & ", " & fib3 & ", "
numFibs -= 3 'Account for first 3 numbers already being generated
flag = (numFibs) > 0
While flag
fib1 = fib2
fib2 = fib3
fib3 = fib1 + fib2
display += fib3 & ", "
numFibs -= 1
flag = numFibs > 0
End While
Me.displayTxt.Text = display
End Sub
End Class
Lean Code:
Public Class GoldenRatio
Private Sub fibsBtn_Click(sender As Object, e As EventArgs) Handles fibsBtn.Click
Dim fib1 As Integer = 0, fib2 As Integer = 1, fib3 As Integer = 1
Me.displayTxt.Text = "0, 1, 1, "
For count = 4 To CInt(Me.userInputTxt.Text) 'Count starts at 4 since we are initially generating the 4th fibonacci number
fib1 = fib2
fib2 = fib3
fib3 = fib1 + fib2
Me.displayTxt.Text += fib3 & ", "
Me.numFibsLbl.Text = count
Next
End Sub
End Class
Hybrid Code:
Public Class GoldenRatio
Private Sub fibsBtn_Click(sender As Object, e As EventArgs) Handles fibsBtn.Click
Dim fib1 As Integer = 0, fib2 As Integer = 1, fib3 As Integer = 1, total As Integer = 3
Dim results As String = "0, 1, 1, "
For count = 4 To CInt(Me.userInputTxt.Text) 'Count starts at 4 since we are initially generating the 4th fibonacci number
fib1 = fib2
fib2 = fib3
fib3 = fib1 + fib2
results += fib3 & ", "
total = count
Next
Me.displayTxt.Text = results
Me.numFibsLbl.Text = total
End Sub
End Class
Lab 9: Learn Functions
Provided Code:
Public Class LearnFunctions
'class scope constants
'constants needed for GetWhatToWear( )
Const JACKET_CUTOFF As Integer = 75
Const COAT_CUTOFF As Integer = 45
'constants needed for GetLetterGrade( )
Const A As Integer = 90
Const B As Integer = 80
Const C As Integer = 70
Const D As Integer = 60
'click event to call the function(s) selected by the user
Private Sub callFunctionBtn_Click(sender As Object, e As EventArgs) Handles callFunctionBtn.Click
Dim arg1 As Integer, arg2 As Integer, arg3 As Integer
Dim response As String
If Me.areaChBx.Checked Then
arg1 = InputBox("What is the length?")
arg2 = InputBox("What is the width?")
response = GetArea(arg1, arg2)
MsgBox(response)
End If
If Me.wearChBx.Checked Then
arg1 = InputBox("What is the temperature?")
response = GetWhatToWear(arg1)
MsgBox(response)
End If
If Me.grdChBx.Checked Then
arg1 = InputBox("What is the numerical grade?")
response = GetLetterGrade(arg1)
MsgBox(response)
End If
If Me.maxChBx.Checked Then
arg1 = InputBox("What is the first number?")
arg2 = InputBox("What is the second number?")
arg3 = InputBox("What is the third number?")
response = GetMaxNumber(arg1, arg2, arg3)
MsgBox("The max of the three is " & response)
End If
End Sub
Function GetArea(len As Integer, wid As Integer) As Integer
Return len * wid
End Function
Function GetWhatToWear(temp As Integer) As String
If temp < COAT_CUTOFF Then
Return "It is chilly, wear a coat."
ElseIf temp < JACKET_CUTOFF Then
Return "It is not warm, seems like jacket weather."
Else
Return "It is very warm, you don't need a jacket or coat."
End If
End Function
Function GetLetterGrade(numGrade As Integer) As String
Dim letterGrade As String = "The student earned "
If (numGrade < D) Then
letterGrade += "an F"
ElseIf (numGrade < C) Then
letterGrade += "a D"
ElseIf (numGrade < B) Then
letterGrade += "a C"
ElseIf (numGrade < A) Then
letterGrade += "a B"
Else
letterGrade += "an A"
End If
Return letterGrade
End Function
Function GetMaxNumber(num1 As Integer, num2 As Integer, num3 As Integer) As Integer
Return Math.Max(Math.Max(num1, num2), num3)
End Function
End Class
Lean Code:
Public Class LearnFunctions
Const JACKET_CUTOFF As Integer = 75, COAT_CUTOFF As Integer = 45, A As Integer = 90, B As Integer = 80, C As Integer = 70, D As Integer = 60
Private Sub callFunctionBtn_Click(sender As Object, e As EventArgs) Handles callFunctionBtn.Click
If Me.areaChBx.Checked Then
MsgBox(GetArea(InputBox("What is the length?"), InputBox("What is the width?")))
End If
If Me.wearChBx.Checked Then
MsgBox(GetWhatToWear(InputBox("What is the temperature?")))
End If
If Me.grdChBx.Checked Then
MsgBox(GetLetterGrade(InputBox("What is the numerical grade?")))
End If
If Me.maxChBx.Checked Then
MsgBox("The max of the three is " & GetMaxNumber(InputBox("What is the first number?"), InputBox("What is the second number?"), InputBox("What is the third number?")))
End If
End Sub
Function GetArea(len As Integer, wid As Integer) As Integer
Return len * wid
End Function
Function GetWhatToWear(temp As Integer) As String
If temp < COAT_CUTOFF Then
Return "It is chilly, wear a coat."
ElseIf temp < JACKET_CUTOFF Then
Return "It is not warm, seems like jacket weather."
Else
Return "It is very warm, you don't need a jacket or coat."
End If
End Function
Function GetLetterGrade(numGrade As Integer) As String
Dim letterGrade As String = "The student earned "
If (numGrade < D) Then
Return letterGrade + "an F"
ElseIf (numGrade < C) Then
Return letterGrade + "a D"
ElseIf (numGrade < B) Then
Return letterGrade + "a C"
ElseIf (numGrade < A) Then
Return letterGrade + "a B"
Else
Return letterGrade + "an A"
End If
End Function
Function GetMaxNumber(num1 As Integer, num2 As Integer, num3 As Integer) As Integer
Return Math.Max(Math.Max(num1, num2), num3)
End Function
End Class
Hybrid Code:
Lab 10: Checkboxes
Provided Code:
Imports System.IO
Public Class DentalPayment
'declare constants with class scope
Const CLEAN_COST As Decimal = 80.0
Const FILL_COST As Decimal = 100.0
Const XRAY_COST As Decimal = 120.0
'declare class scope variables for writing and reading files
Dim fileName As String = "dental.txt"
Dim fileInfo As String = ""
'click event to compute total cost
Private Sub computeBtn_Click(sender As Object, e As EventArgs) Handles computeBtn.Click
'declare local variable to store totalAmount
Dim totalAmount As Decimal
'call the GetTotalPayment() function and assign returned value to totalAmount
totalAmount = GetTotalPayment()
'now that we know what the total amount is, append this, formatted as currency, to fileInfo
fileInfo &= "TOTAL: " & String.Format("{0:C}", totalAmount)
'make the writeBtn visible
Me.writeBtn.Visible = True
End Sub
'function definition, GetTotalPayment, to determine total cost of dental visit
'no parameters are needed because nothing needs to be given to the function
'the constants are class scope, and the CheckBoxes, by default, are class scope
'return type: Decimal (since this is a financial calculation)
'determines the total cost based on which CheckBoxes are checked, and the
'cost of each procedure which is stored in a constant
Function GetTotalPayment() As Decimal
'declare local variable to store the totalAmt
Dim totalAmount As Decimal = 0.00
'begin putting information in the fileInfo variable
fileInfo = "The following procedures were performed:" & vbCrLf
'this is what will be written to file
'first determine If the cleanChBx has been checked
If Me.cleanChBx.Checked Then fileInfo &= "Cleaning, $80" & vbCrLf : totalAmount += 80
'next determine If the fillChBx has been checked
If Me.fillChBx.Checked Then fileInfo &= "Filling, $100" & vbCrLf : totalAmount += 100
'finally determine If the xrayChBx has been checked
If Me.xrayChBx.Checked Then fileInfo &= "X-Rays, $120" & vbCrLf : totalAmount += 120
'return the info stored in the local variable which contains the totalAmt
Return totalAmount
End Function
'click event to write information to file
Private Sub writeBtn_Click(sender As Object, e As EventArgs) Handles writeBtn.Click
Dim fWriter As New StreamWriter(fileName, False)
fWriter.Write(fileInfo)
fWriter.Close()
Me.readBtn.Visible = True
MsgBox("The receipt for this procedure was successfully written to: " & fileName)
End Sub
'click event to read information stored in a file and display on the form
Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
Me.resultTxt.Visible = True
Dim fReader As New StreamReader(fileName)
resultTxt.Text = fReader.ReadToEnd
fReader.Close()
End Sub
End Class
Lean Code:
Imports System.IO
Public Class DentalPayment
Const CLEAN_COST As Decimal = 80.0, FILL_COST As Decimal = 100.0, XRAY_COST As Decimal = 120.0
Dim fileName As String = "dental.txt", fileInfo As String = ""
Private Sub computeBtn_Click(sender As Object, e As EventArgs) Handles computeBtn.Click
Dim total As Decimal = GetTotalPayment() 'Necessary to set up the fileInfo String correctly
fileInfo &= "TOTAL: " & String.Format("{0:C}", total)
Me.writeBtn.Visible = True
End Sub
Function GetTotalPayment() As Decimal
Dim totalAmount As Decimal = 0.00
fileInfo = "The following procedures were performed:" & vbCrLf
If Me.cleanChBx.Checked Then fileInfo &= "Cleaning, $80" & vbCrLf : totalAmount += 80
If Me.fillChBx.Checked Then fileInfo &= "Filling, $100" & vbCrLf : totalAmount += 100
If Me.xrayChBx.Checked Then fileInfo &= "X-Rays, $120" & vbCrLf : totalAmount += 120
Return totalAmount
End Function
Private Sub writeBtn_Click(sender As Object, e As EventArgs) Handles writeBtn.Click
Dim fWriter As New StreamWriter(fileName, False)
fWriter.Write(fileInfo)
fWriter.Close()
Me.readBtn.Visible = True
MsgBox("The receipt for this procedure was successfully written to: " & fileName)
End Sub
Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
Me.resultTxt.Visible = True
Dim fReader As New StreamReader(fileName)
resultTxt.Text = fReader.ReadToEnd
fReader.Close()
End Sub
End Class
Hybrid Code:
Lab 11: Starry Night (Arrays)
Provided Code:
Public Class StarryNight
Dim starNames() As String = {"Butterfly Nebula", "Crab Nebula", "Horsehead Nebula", "Monocerotix", "Sombrero Nebula"}
Dim starImages() As Image = {My.Resources.butterfly, My.Resources.crabNebula, My.Resources.horsehead, My.Resources.monocerotis, My.Resources.sombrero}
Dim starIndex As Integer
Private Sub starsBtn_Click(sender As Object, e As EventArgs) Handles starsBtn.Click
starIndex = Me.starsCmBx.SelectedIndex
ShowPicture()
End Sub
Private Sub StarryNight_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.starsCmBx.DataSource = starNames
End Sub
Private Sub ShowPicture()
Me.starsPcBx.Image = starImages(starIndex)
End Sub
Private Sub ShowName()
MsgBox("This is an Image of " & starNames(starIndex), , starNames(starIndex))
End Sub
Private Sub starsPcBx_Click(sender As Object, e As EventArgs) Handles starsPcBx.Click
ShowName()
End Sub
End Class
Lean Code:
Not many improvements to make here! I think this is about as lean as possible with the given constraints.
Lab 12
Provided Code:
Public Class StarryNight
' Global vars
Dim starNames() As String = {"Butterfly Nebula", "Crab Nebula", "Horsehead Nebula", "Monocerotix", "Sombrero Nebula"}
Dim starImages() As Image = {My.Resources.butterfly, My.Resources.crabNebula, My.Resources.horsehead, My.Resources.monocerotis, My.Resources.sombrero}
Dim starIndex As Integer
Dim rand As New Random()
' Cick handlers
Private Sub starsBtn_Click(sender As Object, e As EventArgs) Handles starsBtn.Click
updateStarIndex()
ShowPicture()
End Sub
Private Sub starsPcBx_Click(sender As Object, e As EventArgs) Handles starsPcBx.Click
ShowName()
End Sub
Private Sub shownebRB_Click(sender As Object, e As EventArgs) Handles shownebRB_BTN.Click
updateStarIndex("radioButton")
ShowPicture()
End Sub
Private Sub randomNeb_btn_Click(sender As Object, e As EventArgs) Handles randomNeb_btn.Click
updateStarIndex("random")
ShowPicture()
End Sub
Private Sub nebLoop_btn_Click(sender As Object, e As EventArgs) Handles nebLoop_btn.Click
For i = 0 To starImages.Length - 1 Step 1
starIndex = i
ShowPicture()
ShowName()
Next
End Sub
' Functions that actually make things happen
Private Sub StarryNight_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.starsCmBx.DataSource = starNames
End Sub
Private Sub ShowPicture()
Me.starsPcBx.Image = starImages(starIndex)
End Sub
Private Sub ShowName()
MsgBox("This is an Image of " & starNames(starIndex), , starNames(starIndex))
End Sub
Private Sub updateStarIndex(Optional source As String = "comboBox") ' Optional argument is used if an arguement is not provided
If source.Equals("comboBox") Then starIndex = Me.starsCmBx.SelectedIndex ' If we are updating the starIndex via the comboBox, use the selected combobox index
If source.Equals("radioButton") Then starIndex = getCheckedRB() ' If we are using the radioButtons, call the getCheckedRB() function
If source.Equals("random") Then starIndex = getRandom() ' if random, get a random index
End Sub
Private Function getCheckedRB() As Integer
If butterflyRB.Checked Then Return 0
If crabRB.Checked Then Return 1
If horseheadRB.Checked Then Return 2
If monoRB.Checked Then Return 3
If sombreroRB.Checked Then Return 4
Return 0 'Default, resets if the button is clicked without a radiobutton being checked
End Function
Private Function getRandom() As Integer
Return rand.Next(0, starImages.Length) ' Random index from 0 to starImages length - 1
End Function
End Class
Explanation:
Again, this is a problem that lends itself to being solved somewhat simply.
The heavy emphasis on modularization via functions and subs may be a bit jarring,
but is a very good idea for code-reuse in the future. You never know when you'll run into
a problem in the future that shares a solution with something you've already written, and
well-modularized abstract code is a good idea for that reason.
This lab helps demonstrate the importance of reducing to component pieces for that purpose.
Lab 13: OR & AND
Provided Code:
Public Class SetRange
Dim minValue As Integer
Dim maxValue As Integer
Dim testValue As Integer
Dim msg As String = ""
Dim randomObject As New Random()
Private Sub userTestBtn_Click(sender As Object, e As EventArgs) Handles userTestBtn.Click
testValue = InputBox("Enter test value")
DisplayTestValue()
End Sub
Private Sub randomTestBtn_Click(sender As Object, e As EventArgs) Handles randomTestBtn.Click
Dim randMin As Integer = -50
Dim randMax As Integer = 151
testValue = randomObject.Next(randMin, randMax)
DisplayTestValue()
End Sub
Private Sub andBtn_Click(sender As Object, e As EventArgs) Handles andBtn.Click
SetRange()
If (testValue >= minValue And testValue <= maxValue) Then
msg = testValue & " is within range."
Else
msg = testValue & " is out of range."
End If
MessageBox.Show(msg, "Using: AND")
End Sub
Private Sub orBtn_Click(sender As Object, e As EventArgs) Handles orBtn.Click
SetRange()
If (testValue < minValue Or testValue > maxValue) Then
msg = testValue & " is out of range."
Else
msg = testValue & " is within range."
End If
MessageBox.Show(msg, "Using: OR")
End Sub
Private Sub SetRange()
minValue = minTxt.Text
maxValue = maxTxt.Text
End Sub
Private Sub DisplayTestValue()
msg = "Test value is " & testValue
MessageBox.Show(msg, "Test value set")
End Sub
End Class
Lean Code:
Public Class SetRange
Const randMin As Integer = -50
Const randMax As Integer = 151
Dim minValue As Integer, maxValue As Integer, testValue As Integer
Dim randomObject As New Random()
Private Sub userTestBtn_Click(sender As Object, e As EventArgs) Handles userTestBtn.Click
DisplayTestValue(InputBox(“Enter test value“))
End Sub
Private Sub randomTestBtn_Click(sender As Object, e As EventArgs) Handles randomTestBtn.Click
testValue = randomObject.Next(randMin, randMax)
DisplayTestValue(testValue)
End Sub
Private Sub andBtn_Click(sender As Object, e As EventArgs) Handles andBtn.Click
SetRange()
If (testValue >= minValue And testValue <= maxValue) Then MessageBox.Show(testValue & “ is within range.“, “Using: AND“) Else MessageBox.Show(testValue & “ is out of range.“, “Using: AND“)
End Sub
Private Sub orBtn_Click(sender As Object, e As EventArgs) Handles orBtn.Click
SetRange()
If (testValue < minValue Or testValue > maxValue) Then MessageBox.Show(testValue & “ is out of range.“, “Using: OR“) Else MessageBox.Show(testValue & “ is within range.“, “Using: OR“)
End Sub
Private Sub SetRange()
minValue = minTxt.Text
maxValue = maxTxt.Text
End Sub
Private Sub DisplayTestValue(testVal As Integer)
MessageBox.Show(“Test value is “ & testVal, “Test value set“)
End Sub
End Class
Explanation:
Not many changes to be made here that keep the code within the confines of the assignment.