ProjPrac 8 : Ex 4.3 no. 14 (p 180), Ex 4.3 no. 16 (or Ex 3.4 no 44 –same),

Programming Projects no. 1 & 3 (p 187 & 188)

14.

Private Sub btnDetermine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermine.Click

'Calculate ideal age for wife, according to Plato

Dim manAge As Double

manAge = CDbl(InputBox("Enter your age: "))

txtIdeal.Text = CStr(WifeAge(manAge))

End Sub

Function WifeAge(ByVal manAge As Double) As Double

Return Int((manAge / 2) + 7)

End Function

16.

Private Sub btnDetermine_Click(…) Handles btnDetermine.Click

'Calculate exercise heart rate

Dim age As Integer, restRate As Double

age = CInt(InputBox("Enter your age: "))

restRate = CDbl(InputBox("Enter your resting heart rate: "))

txtTHR.Text = CStr(THR(age, restRate))

End Sub

Function THR(ByVal age As Integer, ByVal restRate As Double) As Double

Return (((220 - age) - restRate) * 0.6) + restRate

End Function

PP 1.

Private Sub btnCompute_Click(…) Handles btnCompute.Click

'Report caloric count

Dim carbo, fat, prot As Double

InputGrams(carbo, fat, prot)

ShowCalories(carbo, fat, prot)

End Sub

Function Calories(ByVal carbo As Double, ByVal fat As Double, _

ByVal prot As Double) As Double

Return (4 * carbo) + (9 * fat) + (4 * prot)

End Function

Sub InputGrams(ByRef carbo As Double, ByRef fat As Double, _

ByRef prot As Double)

carbo = CDbl(txtCarbo.Text)

fat = CDbl(txtFat.Text)

prot = CDbl(txtProtein.Text)

End Sub

Sub ShowCalories(ByVal carbo As Double, ByVal fat As Double, _

ByVal prot As Double)

txtOutput.Text = "The serving contains " & Calories(carbo, fat, prot) & " calories."

End Sub

Pp 2.

Dim sr As IO.StreamReader = IO.File.OpenText("4-PP-3.TXT")

Dim fmtStr As String = "{0, -20}{1, 10:P}"

Private Sub btnDisplay_Click(…) Handles btnDisplay.Click

'Percentage change in expenditures of 4 most

'advertised soft drinks from 1998 to 1998

Dim name As String

Dim oldExpend, newExpend As Double

lstOutput.Items.Clear()

ShowHeader()

ReadData(name, oldExpend, newExpend)

ShowChange(name, oldExpend, newExpend)

ReadData(name, oldExpend, newExpend)

ShowChange(name, oldExpend, newExpend)

ReadData(name, oldExpend, newExpend)

ShowChange(name, oldExpend, newExpend)

ReadData(name, oldExpend, newExpend)

ShowChange(name, oldExpend, newExpend)

sr.Close()

End Sub

Sub ShowHeader()

lstOutput.Items.Add(String.Format(fmtStr, "", "Change"))

lstOutput.Items.Add(String.Format(fmtStr, "Brand", "1998-99"))

lstOutput.Items.Add("")

End Sub

Sub ReadData(ByRef name As String, ByRef oldExpend As Double, ByRef newExpend As Double)

'Read information

name = sr.ReadLine

oldExpend = CDbl(sr.ReadLine)

newExpend = CDbl(sr.ReadLine)

End Sub

Sub ShowChange(ByVal name As String, ByVal oldExpend As Double, ByVal newExpend As Double)

'Compute change in advertising expenditures

lstOutput.Items.Add(String.Format(fmtStr, name, PctChange(oldExpend, newExpend)))

End Sub

Function PctChange(ByVal oldVal As Double, ByVal newVal As Double) As Double

Return (newVal - oldVal) / oldVal

End Function