Chapter four selected exercises with solutions
4.2
6. NE
8. Approximate Annual Wage: $20,000.00
10. If you fail to plan then you plan to fail
14. Hello Ray and Bob and Bob
Private Sub cmdDisplay_Click() Dim first As String, last As String, fInit As String, lInit As String 'Display initials picOutput.Cls Call InputNames(first, last) Call ExtractInitials(first, last, fInit, lInit) Call DisplayInitials(fInit, lInit) End Sub Private Sub InputNames(first As String, last As String) 'Get the persons first and last name first = InputBox("Enter your first name:") last = InputBox("Enter your last name:") End Sub
Private Sub ExtractInitials(first As String, last As String, fInit As String, lInit As String) 'Determine the initials of the first and last names fInit = Left(first, 1) lInit = Left(last, 1) End Sub Private Sub DisplayInitials(fInit As String, lInit As String) 'Display the initials picOutput.Print "Your initials are "; fInit; "."; lInit; "." End Sub
Or without calls to sub procedures Private Sub cmdDisplay_Click() Dim first As String, last As String, fInit As String, lInit As String 'Display initials picOutput.Cls first = InputBox("Enter your first name:") last = InputBox("Enter your last name:") fInit = Left(first, 1) lInit = Left(last, 1) picOutput.Print "Your initials are "; fInit; "."; lInit; "." End Sub
Dim total As Single 'In (Declarations) section of (General) Private Sub cmdProcessItem_Click() Dim item As String, price As Single 'Process item; display part of sales receipt Call InputData(item, price) total = total + price Call ShowData(item, price) txtItem.Text = "" txtPrice.Text = "" txtItem.SetFocus End Sub
Private Sub InputData(item As String, price As Single) 'Input item name and price item = txtItem.Text price = Val(txtPrice.Text) End Sub Private Sub ShowData(strItem As String, numItem As Single) 'Display data on specified line picReceipt.Print strItem; Tab(15); FormatNumber(numItem) End Sub
Private Sub cmdDisplay_Click() Dim tax As Single 'Display sum, tax, and total tax = total * 0.05 tax = Round(tax, 2) picReceipt.Print Tab(15); " " Call ShowData("Sum", total) Call ShowData("Tax", tax) Call ShowData("Total", total + tax) End Sub
4.3
4. 400
6. The day is Wed
Private Sub cmdCalcIdealAge_Click() Dim manAge As Integer 'Calculate ideal age for wife, according to Plato picIdealAge.Cls Call InputManAge(manAge) Call ShowWifeAge(manAge) End Sub Private Sub InputManAge(manAge As Integer) manAge = Val(InputBox("Enter your age: ")) End Sub Private Sub ShowWifeAge(manAge As Integer) picIdealAge.Print "The ideal age of your wife is"; Int(manAge / 2 + 7) End Sub
Private Sub cmdCalculate_Click() picResult.Cls picResult.Print "Your BMI is"; BMI(Val(txtWeight), Val(txtHeight)) End Sub Private Function BMI(w As Single, h As Single) As Single 'Calculute body mass index BMI = Round(703 * w / h ^ 2) End Function