Sie sind auf Seite 1von 8

Chapter 4 Critical Thinking

1. Assuming the comment is correct, determine the logic error in


the following statement:
If grade > 90 Then
Display A for grade greater than or
equal to 90
Me.lblGrade.Text = You have an A
End If
The logic error is that the programmer forgot to use the greater than or equal
to sign (grade >= 90) like the comment suggests.
2. What is displayed in the label after the following statement executes?
Does the label assignment reflect what was intended? If not, how
should the statement be rewritten to produce the intended result?
Dim score As Integer = 25
If score >= 100 Then
Me.lblMessage.Text = You Won!
ElseIf score < 100 Then
Me.lblMessage.Text = Good try.
ElseIf score < 50 Then
Me.lblMessage.Text = Practice more.
EndIf
Good try. Is displayed in the label after the statement executes. This is not
the intended result; the intended result is Practice more. The statement
should be written:
Dim score As Integer = 25
If score >= 100 Then
Me.lblMessage.Text = You Won!
ElseIf score < 100 And score > 50 Then
Me.lblMessage.Text = Good try.
ElseIf score < 50 Then
Me.lblMessage.Text = Practice more.
EndIf

3. Check boxes, radio buttons, and textboxes all accept user input.
a) List the differences in the way the three accept input.
b) Give an example of how each should be used in an application.
a) Check boxes accept input similar to radio buttons: they allow the user
to select options; if the check box is clicked, it is activated. The
difference between check boxes and radio buttons is that only one
radio button can be activated at a time, whereas multiple check boxes
can be activated at the same time. Text boxes accept text or numerical
value that is typed into the box by the user.
b) A check box should be used if the programmer wants the user to be
able to select multiple options at the same time; for example, an
ordering menu program. Radio buttons should be used if the user
needs to select options, but doesnt need to select multiple options; for
example, switching between operators in a calculator. Finally, a text
box should be used if the user needs to input text or a numerical value,
like typing in a number into a calculator.

4. Given the statements


Dim quantity As Integer = 20
Dim price As Double = 5
Determine the value, true or false, for each of the following
expressions:
a) Quantity > 10 And price > 5
b) Quantity = 15 Or price = 5
c) Quantity >= 20 And price >= 2 And quantity * price >= 40
d) Not price = 5
e) Quantity < 100 Or price > 4 And Not quantity = 20
a) False
b) True
c) True
d) False
e) True
5. Write an appropriate decision statement for each of the following:
a) Display Great Job in a label called lblMessage if grade is 90 or
above.
b) Display High Scorer in a label named lblHigh for total points
between 100 and 200, inclusive.
c) Display Number must be less than 100. In a message box if the
value in txtgrade is greater than 100

a) If grade >= 90 Then


Me.lblMessage.Text = Great job
b) If totalPoints > 100 Or totalPoints <200 Then
Me.lblHigh.Text = High Scorer
c) If Me.txtgrade.text > 100 Then
MessageBox.Show(Number must be less than 100.)
6. A) Which is an appropriate word for the first blank below, odd or even?
Which is the appropriate word for the second blank?
B) Rewrite the IfThenElse statement from part a as a SelectCase
statement.
If number Mod 2 = 0 Then
MessageBox.Show(Your number is _______)
Else
MessageBox.Show(Your number is _______)
EndIf

a) The appropriate word for the first blank is even because an even
number divided by 2 gives no remainders. The appropriate word for the
second blank is odd because an odd number divided by 2 will give a
remainder.
b) Select Case number Mod 2
Case 0
MessageBox.Show(Your number is even.)
Case Else
MessageBox.Show(Your number is odd.)

7. List the errors in the statement below and then rewrite the statement
so that it will execute as expected:
If 50 <= numTickets <= 100 And _
Me.radStu.Checked
MessageBox.Show = (Both discounts.)
ElseIf 50 <= numTickets <= 100
MessageBox.Show = (Volume discount.)
ElseIf Me.radStu.Checked
MessageBox.Show = (Student discount.)
Case Else
MessageBox.Show = (No discount.)
EndIf
EndIf

One error is that the programmer forgot to use Then at the end of
each If. Another issue is that the programmer forgot to add a second
quotation mark at the end of the intended message box quote. Finally,
the Case Else should be changed to just Else. The code should look
as follows:
If 50 <= numTickets <= 100 And Me.radStu.Checked Then
MessageBox.Show(Both discounts.)
ElseIf 50 <= numTickets <= 100 Then
MessageBox.Show(Volume Discount.)
ElseIf Me.radStu.Checked Then
MessageBox.Show(Student discount.)
Else
MessageBox.Show (No discount.)
8. Rewrite the following statement so that it does not include the nested
IfThen statement:
If Me.chkValue1.Checked Then
If Me.chkVslue2.Checked Then
MessageBox.Show = (Both Applied.)
End If
End IF

If Me.chkValue1.Checked = True And Me.chkValue2.Checked = True Then


MessageBox.Show("Both Applied.")
End If

9. Assume txtTest1, txtTest2, and txtTest3 contain numeric values. Write


an IfThenElseIf statement that displays in a label the average of
the three numbers only if all the numbers are between 0 and 100,
otherwise a message box with an appropriate message should be
displayed and the text boxes cleared.

If Val(Me.txtTest1.text) >= 0 And Val(me.txtTest1.text) <=100 And_


Val(Me.txtTest2.text) >= 0 And Val(Me.txtTest2.text) <=100 And_
Val(Me.txtTest2.text) >= 0 And Val(Me.txtTest3.text) <=100 Then
Average = (Val(Me.txtTest1.text) + Val(Me.txtTest2.text)_
+ Val(Me.txtTest3.text)) / 3
Me.lblResult.text = average
Else
MessageBox.Show(The three scores must be between 1 and
100)

10. Assume txtTest1 and txtTest2 contain numeric values. Write an


IfThenElseIf statement that displays a message box with one of the
following messages as appropriate:
First number is larger
Second number is larger
Both numbers are equal

num1 = Val(Me.TextBox1.Text)
num2 = Val(Me.TextBox2.Text)
If num1 > num2 Then
MessageBox.Show("First number is larger")
ElseIf num1 < num2 Then
MessageBox.Show("Second number is larger")
ElseIf num1 = num2 Then
MessageBox.Show("Both numbers are equal")
End If

11. Write a statement that generates a random whole number


between 5 and 50, inclusive.
Randomize()
Int(Rnd()*46 + 5)

12. a) List the errors in the statement below and then rewrite the
statement so that it will execute as expected:
Select Case num
Case 2 Or 3, num > 10
MessageBox.Show(1st Case)
Case 20 <= num < 30
MessageBox.Show(2nd Case)

a) End Select is used to end the SelectCase statement, not End Case.
A comma is used to separate the two values, not Or; and the To
keyword is used to include a range of values, not the greater than or
equal to(=>) or less than (<) symbols. Finally, the Is keyword is used
instead of num to give the statement meaning.

Select Case num


Case 2, 3, Is > 10
MessageBox.Show(1st Case)
Case 20 To 29
MessageBox.Show(2nd Case)

b) If num = 2 Or 3 Or num > 10 Then


MessageBox.Show(1st Case)
ElseIf num >= 20 And num <=29 Then
MessageBox.Show(2nd Case)
End If
13. Assume txtMonth contains all uppercase text that is a month of
the year, for example, SEPTEMBET. Another text box, txtYear, contains
the year. Write a SelectCase statement that displays in a message
box the number of days in the month entered.
Select Case Me.txtMonth.Text
Case "JANUARY", "MARCH", "MAY", "JULY", "AUGUST", "OCTOBER", "DECEMBER"
MessageBox.Show("There are 31 days in this month.")
Case "APRIL", "JUNE", "SEPTEMBER", "NOVEMBER"
MessageBox.Show("There are 30 days in this month.")
Case "FEBRUARY"
If Val(Me.txtYear.Text) Mod 4 <> 0 Then
MessageBox.Show("There are 28 days in this month.")
ElseIf Val(Me.txtYear.Text) Mod 400 = 0 Then
MessageBox.Show("There are 29 days in this month.")
ElseIf Val(Me.txtYear.Text) Mod 100 = 0 Then
MessageBox.Show("There are 28 days in this month.")
Else
MessageBox.Show("There are 29 days in this month.")

End If
End Select
End Sub
End Class

14. Write a btnPurchase_Click event procedure that calculates the


cost of tickets and gives free tickets on every 100th purchase. The
txtNumTickets text box contains the number of tickets for a purchase
and each ticket price is $8.00. A counter variable should be updated by
one each time Purchase is clicked. On the 100th purchase, a message
box should display Congratulations, the tickets are free! The counter
should then be reset to 0. If the purchase is not the 100th, a message
box should display the cost of the ticket. Use appropriate constants
and variables.

Private Sub btnPurchase_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles btnPurchase.Click
Dim tickets As Integer
Dim cost As Decimal
Const TicketCost As Decimal = 8
Static purchase As Integer = 0

purchase += 1

If purchase = 100 Then


MessageBox.Show("Congratulations, the tickets are free!")
purchase = 0
Else
tickets = Val(Me.txtNumberTickets.Text)
cost = TicketCost * tickets
MessageBox.Show(cost)
End If
End Sub

15. Write a btnMessage_Click event procedure that displays one of


the messages below in a message box:
You win $100 2% of the time
You win $10 10% of the time
You win $1 50% of the time
Thanks for trying. The rest of the time

Private Sub btnPurchase_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles btnPurchase.Click
Dim numRandom As Integer
Randomize()
numRandom = Int(Rnd() * 100 + 1)

Select Case numRandom


Case 1, 2
MessageBox.Show("You win $100")
Case 3 To 12
MessageBox.Show("You win $10")
Case 13 To 62
MessageBox.Show("You win $1")
Case Else
MessageBox.Show("Thanks for trying.")
End Select
End Sub

16. Determine if each of the following statements is true or false. If


false, explain why.
a) The condition of an IfThen statement is a Boolean expression
True
b) A decision structure must have an Else clause.
False, an IfThen statement does not include an Else/
c) It is good programming style to line up the If, the Else, and the End
If in a decision structure, and to indent the lines in between.
True
d) The SelectCase statement must have the Case Else clause.
False, the Case Else clause is optional.
e) The SelectCase statement can only be used if you have more
than 2 cases.
False, you can have two or less cases with a SelectCase
statement.
f) Using Rnd() without including the Randomize() will produce a run-
time error.
False; however, it will provide the same sequence of random
numbers without initializing it with Randomize().
g) Numbers generated by the statemet Rnd() are integers.
False, they are floating point numbers.
h) Algorithms are designed after the source code is typed.
False, it is designed before.

i) The value of local variables are always retained in memory for the
duration of program execution.
False, the value of a local variable is retained in memory for the
duration of the procedure in which it is declared.
j) A compound Boolean expression uses more than one Boolean
expression to determine whether a condition is true or false.
True
k) In a logical And expression, both operands must be true for the
expression to evaluate to true.
True
l) In a logical expression, Or is evaluated before Not.
False, Not is evaluated before And, and Or is evaluated last
(after Not).
m) Message boxes can only be used in decision statements.
False, they can be used outside of decision statements.
n) Counter variables are useful for keeping track of the number of
times a specific event occurs.
True
o) Sum, as assigned as sum = 1 + 2 + 3, is a counter variable.
False, sum would have to be assigned as sum += 1.
p) Only one check box can be selected at a time.
False, multiple check boxes can be selected at a time. Only one
radio button may be selected at one time, however.
q) A Visual Basic statement must be typed in its entirety on a single
line.
False, you can use an _ at the end of the line to continue writing
the line of code on the next line.

Das könnte Ihnen auch gefallen