Tracing Variables, Assignment Statements WS 1

advertisement
Visual Basic
Written Work WS 1
Name__________________________________
Period_________
Part 1-Evaluating Assignment Statements
What is the final value of the variable result in the following assignment statements? You must show steps for full credit and
remember to follow the order of operations.
Dim num As Integer = 4
Dim var As Integer = 2
Dim result As Double = 0
1. result = 4 * 3 ^ 2
2. result = (3 + num - 2) * var
3. result = (9 Mod num) * 4 + var
4. result = 48 / 12 * var
5. result = -6 + var * (2 ^ num)
6. result = 4 * var – (num / 2) ^ 3
Part II-Writing Statements
Write the following assignment statements. Be sure to use the exact variable names given in the exercise. You may assume that
all variables have already been declared with declaration (Dim) statements. Be sure to use parentheses where necessary.
1. Write a declaration statement that declares a variable named numApples with the Integer data type and initializes it to zero.
Dim
2. Write a declaration statement that declares a variable named totalPrice as a Double and initializes it to zero.
3. Write a declaration statement that declares a constant named PRICE_GAL_GAS as a Double and initializes it to 3.359.
The declaration statement for a constant begins with Const instead of Dim. Constants should be named with uppercase letters and
underscores between separate words.
Const
4. Complete the assignment statement below so that it computes the sum of the variables numApples and numOranges and stores
(i.e. assigns) that sum to the variable named totalFruit.
totalFruit =
5. Complete the assignment statement below that computes the product of the variables named pricePerApple and numApples and
that stores (i.e. assigns) that result into the variable totalPrice.
totalPrice =
6. Write an assignment statement that computes the average of the variables game1, game2, and game3 and assigns that value into
the variable average. Make sure that you begin your assignment statement with the variable that is going to be assigned the final
value.
7. Write an assignment statement that multiplies the product of numSongs and pricePerSong by 1.06 and assigns that value into the
variable totalPriceWithTax.
8. Write an assignment statement that computes the price of purchasing numGallonsGas gallons of gas at a price of PRICE_GAL_GAS
and assigns that value into the variable gasSubtotal.
9. Complete the assignment statement below that concatenates a dollar symbol in front of the value stored in the variable
grandTotal and assigns that string into the Text property of the label named lblTotal.
lblTotal.Text =
Part III – Tracing Code
Answer the following questions based on this method.
' computes the cost of purchasing donuts
Private Sub btnCompute_Click( . . . )
' *********************** declaration statements *******
Dim numDonuts As Integer = 0 ' # of donuts purchased
Dim totalCost As Double = 0 ' total cost of purchases
Const PRICE_PER_DONUT As Double = 0.75 ' price per donut
Const TAX_RATE As Double = 1.06 ' PA state sales tax
' ********************* obtain user input **************
numDonuts = Val(txtDonuts.Text)
' ********************** calculations ******************
totalCost = numDonuts * PRICE_PER_DONUT
' ********************** display the output ************
lblTotalCost.Text = "$" + Str(totalCost)
End Sub
1. If the customer types the number 2 into the textbox named txtDonuts, what will display as the final cost in the label named
lblTotalCost?
2. If the customer types the number 5 into the textbox named txtDonuts, what will display as the final cost?
3. If the line of code :
totalCost = numDonuts * PRICE_PER_DONUT was changed to :
totalCost = numDonuts * PRICE_PER_DONUT * TAX_RATE
and the customer types the number 2 into txtDonuts, what will display as the final cost?
4. Explain why the Val function must be used in the statement
5. Explain why the Str function must be used in the statement.
6. Why are constants named in all UPPERCASE letters such as PRICE_PER_DONUT?
7. Why are constants used in this program? In other words, why is the statement totalCost = numDonuts * PRICE_PER_DONUT
better than the statement totalCost = numDonuts * 0.75
8. Why is typing out all of the statements above better than using the equivalent single line of code
lblTotalCost.Text = "$" + Str(Val(txtDonuts.Text) * 0.75 * 1.06)
Download