[Page 660 (continued)] Exercises 5.1 1. | hi | 3. | The letter before G is F | 5. | "We're all in this alone." Lily Tomlin | 7. | True | 9. | True | 11. | True | 13. | True | 15. | False | | [Page 661] | 17. | False | 19. | True | 21. | True | 23. | False | 25. | False | 27. | False | 29. | True | 31. | Equivalent | 33. | Not Equivalent | 35. | Equivalent | 37. | Not Equivalent | 39. | Equivalent | 41. | a <= b | 43. | (a >= b) Or (c = d) | 45. | (a = "") Or (a >= b) Or (a.Length >= 5) | 47. | True | 49. | False | Exercises 5.2 1. | Less than ten. | 3. | tomorrow is another day. | 5. | 10 | 7. | Cost of call: $11.26 | 9. | The number of vowels is 2 | 11. | positive | 13. | Incorrect condition. Should be If ((1 < num) And (num < 3)) Then | 15. | No Then in second line | 17. | Comparing numeric and string values in line 5 | 19. | Incorrect condition. Should be If ((j = 4) Or (k = 4)) Then | 21. | a = 5 | 23. | If j = 7 Then b = 1 Else b = 2 End If | | [Page 662] | 25. | message = "Is Alaska bigger than Texas and California combined?" answer = InputBox(message) If (answer.Substring(0, 1) = "Y") Then txtOutput.Text ="Correct" Else txtOutput.Text ="Wrong" End If | 27. | Private Sub btnCompute_Click(...) Handles btnCompute.Click 'Give server a tip. Dim cost, tip As Double cost = CDbl(InputBox("Enter cost of meal:")) tip = cost * 0.15 If tip < 1 Then tip = 1 End If txtOutput.Text = "Leave " & FormatCurrency(tip) & " for the tip." End Sub | 29. | Private Sub btnCompute_Click(...) Handles btnCompute.Click 'Order diskettes. Dim num, cost As Double num = CDbl(InputBox("Number of diskettes:")) If num < 100 Then cost = 0.25 * num '25 cents each Else cost = 0.2 * num '20 cents each End If txtOutput.Text = "The cost is " & FormatCurrency(cost) & "." End Sub | 31. | Private Sub btnAskQuestion_Click(...) Handles btnAskQuestion.Click 'Ask for first Ronald McDonald. Dim name As String name = (InputBox("Who was the first Ronald McDonald?")).ToUpper If name = "WILLARD SCOTT" Then txtOutput.Text = "Correct." Else txtOutput.Text = "Nice try." End If End Sub | 33. | Private Sub btnCompute_Click(...) Handles btnCompute.Click 'Savings account withdrawal Dim balance, amountOfWithdrawal As Double balance = CDbl(InputBox("Current balance:")) amountOfWithdrawal = CDbl(InputBox("Amount of withdrawal:")) If (balance >= amountOfWithdrawal) Then balance = balance - amountOfWithdrawal txtOutput.Text = "New balance is " & FormatCurrency(balance) & "." If balance < 150 Then MsgBox("Balance below $150", 0, "Warning") End If Else txtOutput.Text = "Withdrawal denied." End If End Sub | | [Page 663] | 35. | Private Sub btnCompute_Click(...) Handles btnCompute.Click 'Display change from a transaction. Dim weight, cost, amount, change As Double weight = CDbl(txtWeight.Text) amount = CDbl(txtAmount.Text) cost = weight * 0.79 If (amount >= cost) Then change = amount - cost txtOutput.Text = "Your change is " & FormatCurrency(change) & "." Else txtOutput.Text = "I need " & FormatCurrency(cost - amount) & _ " more." End If End Sub | 37. | Private Sub btnCompute_Click(...) Handles btnTranslate.Click 'Convert to Pig Latin. Dim word, first As String word = InputBox("Enter a word (use all lowercase):") first = word.Substring(0, 1) If "aeiou".IndexOf(first) <> -1 Then word &= "way" Else word = word.Substring(1) & first & "ay" End If txtOutput.Text = "The word in pig latin is " & word & "." End Sub | 39. | Dim numLines As Integer = 0 'In Declarations section 'numLines tells the number of lines displayed. Private Sub btnBogart_Click(...) Handles btnBogart.Click If numLines = 0 Then lstOutput.Items.Add("I came to Casablanca for the waters.") numLines += 1 ElseIf numLines = 2 Then lstOutput.Items.Add("I was misinformed.") numLines += 1 End If End Sub Private Sub btnRaines_Click(...) Handles btnRaines.Click If numLines = 1 Then lstOutput.Items.Add("But we're in the middle of the desert.") numLines += 1 End If End Sub | 41. | Private Sub btnEvaluate_Click(...) Handles btnEvaluate.Click 'Assume that the Text property of txtNumberOfGuesses 'was set to 0 in the Form Designer Dim msg As String txtNumberOfGuesses.Text = CStr(CInt(txtNumberOfGuesses.Text) + 1) If txtAnswer.Text.ToUpper.IndexOf("COOLIDGE") <> -1 Then MsgBox("Calvin Coolidge was born on July 4, 1872.", 0, "Correct") End ElseIf CInt(txtNumberOfGuesses.Text) = 3 Then msg = "He once said, 'If you don't say anything," & _ " you won't be called upon to repeat it.'" [Page 664] MsgBox(msg, 0, "Hint") ElseIf CInt(txtNumberOfGuesses.Text) = 7 Then MsgBox("His nickname was 'Silent Cal.'", 0, "Hint") ElseIf CInt(txtNumberOfGuesses.Text) = 10 Then msg = "Calvin Coolidge was born on July 4, 1872." MsgBox(msg, 0, "You've run out of guesses") End End If txtAnswer.Clear() txtAnswer.Focus() End Sub | 43. | Private Sub btnCalculateTax_Click(...) Handles btnCalculateTax.Click 'Calculate New Jersey state income tax. Dim income, tax As Double income = CDbl(InputBox("Taxable income:")) If income <= 20000 Then tax = 0.02 * income Else If income <= 50000 Then tax = 400 + 0.025 * (income - 20000) Else tax = 1150 + 0.035 * (income - 50000) End If End If txtOutput.Text = "Tax is " & FormatCurrency(tax) End Sub | Exercises 5.3 1. | The price is $3.75 The price is $3.75 | 3. | Mesozoic Era Paleozoic Era ? | 5. | Nope. He worked with the developer, von Neumann, on the ENIAC. Correct. | 7. | The more things change, the less they remain the same. Less is more. Time keeps everything from happening at once. | 9. | Should have a Case clause before the 4th line | 11. | Case a = "Bob" should be Case "Bob" | 13. | Error in second Case clause | 15. | Logical error: >= "Peach" should be >= "PEACH". Syntax error: "ORANGE TO PEACH" should be "ORANGE" To "PEACH". | 17. | Valid | 19. | Invalid | 21. | Valid | | [Page 665] | 23. | Select Case a Case 1 txtOutput.Text = "one" Case Is > 5 txtOutput.Text = "two" End Select | 25. | Select Case a Case 2 txtOutput.Text = "yes" Case Is < 5 txtOutput.Text = "no" End Select | 27. | Private Sub btnDescribe_Click(...) Handles btnDescribe.Click 'Determine degree of cloudiness. Dim percent As Double percent = CDbl(InputBox("Percentage of cloud cover:")) Select Case percent Case 0 To 30 txtOutput.Text = "Clear" Case 31 To 70 txtOutput.Text = "Partly cloudy" Case 71 To 99 txtOutput.Text = "Cloudy" Case 100 txtOutput.Text = "Overcast" Case Else txtOutput.Text = "Percentage must be between 0 and 100." End Select End Sub | 29. | Private Sub btnFindNumDays_Click(...) Handles btnFindNumDays.Click Dim monthName As String monthName = InputBox("Enter a month (do not abbreviate):") txtOutput.Text = monthName & " has " & GetDays(monthName) & " days." End Sub Function GetDays(ByVal monthName As String) As Integer 'Compute number of days in the month. Dim answer As String, numberOfDays As Integer Select Case monthName.ToUpper Case "FEBRUARY" answer = InputBox("Is it a leap year?") If answer.ToUpper.Substring(0, 1) = "Y" Then numberOfDays = 29 Else numberOfDays = 28 End If Case "APRIL", "JUNE", "SEPTEMBER", "NOVEMBER" numberOfDays = 30 Case "JANUARY", "MARCH", "MAY", "JULY", "AUGUST", _ "OCTOBER", "DECEMBER" numberOfDays = 31 End Select Return numberOfDays End Function | | [Page 666] | 31. | Private Sub btnAssign_Click(...) Handles btnAssign.Click 'Give letter grade for number score. Dim score As Integer score = CInt(InputBox("What is the score?")) txtOutput.Text = "The letter grade is " & Grade(score) & "." End Sub Function Grade(ByVal score As Integer) As String 'Return letter grade for score. Dim letterGrade As String Select Case score Case 90 To 100 letterGrade = "A" Case 80 To 89 letterGrade = "B" Case 70 To 79 letterGrade = "C" Case 60 To 69 letterGrade = "D" Case 0 To 59 letterGrade = "F" Case Else letterGrade = "Invalid" End Select Return letterGrade End Function | 33. | Private Sub btnDescribe_Click(...) Handles btnDetermine.Click 'Determine cash reward. Dim amountRecovered As Double amountRecovered = CDbl(InputBox("How much was recovered?")) txtOutput.Text = "The amount given as reward is " & _ FormatCurrency(Reward(amountRecovered)) & "." End Sub Function Reward(ByVal amountRecovered As Double) As Double Dim payment As Double Select Case amountRecovered Case Is <= 75000 payment = 0.1 * amountRecovered Case Is <= 100000 payment = 7500 + 0.05 * (amountRecovered - 75000) Case Is > 100000 payment = 8750 + 0.01 * (amountRecovered - 100000) If payment > 50000 Then payment = 50000 End If End Select Return payment End Function | 35. | Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim pres, state, trivia, whichBush As String pres = txtLastName.Text Select Case pres.ToUpper Case "CARTER" state = "Georgia" [Page 667] trivia = "The only soft drink served in the Carter " & _ "White House was Coca-Cola." Case "REAGAN" state = "California" trivia = "His secret service code name was Rawhide." Case "BUSH" state = "Texas" whichBush = InputBox("Are his middle initials HW or W?") Select Case whichBush Case "HW" trivia = "He was the third left-handed president." Case "W" trivia = "He once owned the Texas Rangers baseball team." End Select state = "Texas" Case "CLINTON" state = "Arkansas" trivia = "In college he did a good imitation of Elvis Presley." Case Else state = "" trivia = "" End Select If state <> "" Then lstOutput.Items.Clear() lstOutput.Items.Add("President " & pres & "'s " & _ " home state was " & state & ".") lstOutput.Items.Add(trivia) End If txtLastName.Clear() txtLastName.Focus() End Sub | 37. | Private Sub btnHumor_Click(...) Handles btnHumor.Click If (CInt(mtxtNumber.Text) >= 1) And (CInt(mtxtNumber.Text) <= 3) Then txtOutput.Text = HumorMsg(CInt(mtxtNumber.Text)) End If txtNumber.Clear() txtNumber.Focus() End Sub Private Sub btnInsult_Click(...) Handles btnInsult.Click If (CInt(mtxtNumber.Text) >= 1) And (CInt(mtxtNumber.Text) <= 3) Then txtOutput.Text = InsultMsg(CInt(mtxtNumber.Text)) End If txtNumber.Clear() txtNumber.Focus() End Sub Function HumorMsg(ByVal num As Integer) As String Dim sentence As String Select Case num Case 1 sentence = "I can resist everything except temptation." Case 2 sentence = "I just heard from Bill Bailey." & _ " He's not coming home." [Page 668] Case 3 sentence = "I have enough money to last the rest of my life," _ & " unless I buy something." End Select Return sentence End Function Function InsultMsg(ByVal num As Integer) As String Dim sentence As String Select Case num Case 1 sentence = "How much would you charge to haunt a house?" Case 2 sentence = "I bet you have no more friends than an alarm clock." Case 3 sentence = "When your IQ rises to 30, sell." End Select Return sentence End Function | |