I need help in a few hours

profileDre_fingerz
appendix_b.pdf

A P P E N D I X B Answers to Mini-Quizzes

and TRY THIS Exercises

Chapter 1

TRY THIS 1 (Chapter 1)

1. repeat 5 times: walk forward

end repeat

2. jump over Ginger 3. repeat until you are directly in front of the chair:

walk forward

end repeat 4. repeat 2 times:

turn left 90 degrees end repeat

5. sit down in the chair

TRY THIS 2 (Chapter 1)

1. enter the customer’s age and the amount due 2. if the customer’s age is greater than or equal to 65, do this:

calculate the amount due by multiplying the amount due by 90% end if

3. display the amount due

Chapter 2

Mini-Quiz 2-1

1. Output: annual state income tax

Input: yearly taxable wages

state income tax rate

2. Output: savings

Input: number of CDs purchased club CD price store CD price

All Microsoft screenshots used with permission from Microsoft Corporation.

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

3. Output: total amount saved

B-2

Input: daily savings

number of days

Mini-Quiz 2-2

1. Algorithm: 1. enter the yearly taxable wages and state income tax rate 2. calculate the annual state income tax by multiplying the yearly taxable wages

by the state income tax rate

3. display the annual state income tax

2.

Algorithm:

start

enter yearly taxable wages and state income tax rate

calculate annual state income tax = yearly taxable wages times state income tax rate

display annual state income tax

stop

3. Algorithm:

1. enter the number of CDs purchased, the club CD price, and the store CD price 2. calculate the club cost by multiplying the number of CDs purchased by the club CD price 3. calculate the store cost by multiplying the number of CDs purchased by the store CD price 4. calculate the savings by subtracting the club cost from the store cost

5. display the savings

Mini-Quiz 2-3

1. Desk-check table

yearly taxable wages

23000

state income tax rate annual state income tax

.03 690

14000 .02 280

2. Desk-check table

number of CDs purchased club CD price store CD price club cost store cost savings

20 10.50

5 9.99

14.99

11

210

49.95

299.80

55

89.80

5.05

Chapter 2

TRY THIS 1 (Chapter 2)

Output: tip

Input: total bill liquor charge tip percentage

Algorithm: 1. enter the total bill, liquor charge, and tip percentage 2. calculate the tip by subtracting the liquor charge from the total bill, and

then multiplying the remainder by the tip percentage

3. display the tip

Desk-check table:

B-3

total bill liquor charge tip percentage tip

85 20 .2 13

35 0 .15 5.25

TRY THIS 2 (Chapter 2)

Output: total cost of purchase

Processing: total cup cost

total plate cost subtotal

Input: cup price

plate price number of cups number of plates sales tax rate

Algorithm: 1. enter the cup price, plate price, number of cups, number of plates, and sales tax rate 2. calculate the total cup cost by multiplying the number of cups by the cup price 3. calculate the total plate cost by multiplying the number of plates by the plate price 4. calculate the subtotal by adding together the total cup cost and total plate cost 5. calculate the total cost of purchase by multiplying the subtotal by the sales tax

rate, and then adding the result to the subtotal

6. display the total cost of purchase

Desk-check table:

cup price plate price number of cups number of plates sales tax rate

.50 1 35

.25 .75 20

35 .02

10 .06

total cup cost total plate cost subtotal total cost of purchase

17.50

5

35

7.50

52.50

12.50

53.55

13.25

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-4

Chapter 3

Mini-Quiz 3-1

1. label

2. Toolbox

3. graphical user interface

Mini-Quiz 3-2

1. Image

2. Text

3. upper-left

4. PictureBox2

5. Start Debugging

TRY THIS 1 (Chapter 3)

Set the form’s Font property to Segoe UI, 9pt. Set its StartPosition and Text properties to

CenterScreen and Scottsville Library, respectively. Set the Label1 control’s Text property to

WELCOME!, and set its Font property to Segoe UI, 16 pt. Set the PictureBox1 control’s Image

and SizeMode properties to Bookworm.png and StretchImage, respectively. Use the Format

menu to center the label and picture box controls horizontally on the form.

Chapter 4

Mini-Quiz 4-1

1. c. lblGross

2. txt

3. label

Mini-Quiz 4-2

1. 2

2. a. Alt+y

3. procedure footer

TRY THIS 1 (Chapter 4)

Interface:

txtSalespeople

txtMonthlyAllowance

lblAnnualAllowance btnCalc btnExit

Chapter 5

Tab order:

Code:

Chapter 5

Mini-Quiz 5-1

1. Val(lblSales.Text) + .05

2. 6

3. 1

Mini-Quiz 5-2

1. lblTax.Text = Val(txtSales.Text) * Val(txtTaxRate.Text)

2. lblNewRate.Text = Val(txtCurrentRate.Text) + 10

3. assignment operator

4. General Declarations section

B-5

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-6

TRY THIS 1 (Chapter 5)

Chapter 6

Mini-Quiz 6-1

1. Integer

2. b. dblPay.Rate

3. Dim decRevenue As Decimal

Mini-Quiz 6-2

1. Double.TryParse(txtPropertyValue.Text, dblPropValue)

2. Const intMAXIMUM As Integer = 100

3. lblBonus.Text = dblBonus.ToString("C0")

TRY THIS 1 (Chapter 6)

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displays the annual net income

Dim decRevenue As Decimal Dim decExpenses As Decimal Dim decNetIncome As Decimal

Decimal.TryParse(txtRevenue.Text, decRevenue) Decimal.TryParse(txtExpenses.Text, decExpenses)

decNetIncome = decRevenue - decExpenses lblNetIncome.Text = decNetIncome.ToString( "C2")

End Sub

Chapter 7

TRY THIS 2 (Chapter 6)

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

' calculates and displays the new weekly pay

' declare named constant Const dblRATE As Double = 0.03

' declare variables Dim dblNewPay As Double Dim dblRaise As Double Dim dblCurrentPay As Double

' assign input to a variable Double.TryParse(txtCurrentPay.Text, dblCurrentPay)

' calculate raise and new pay dblRaise = dblCurrentPay * dblRATE dblNewPay = dblCurrentPay + dblRaise

' display new pay lblNewPay.Text = dblNewPay.ToString( "C2")

End Sub

B-7

dblRATE dblNewPay dblRaise dblCurrentPay

.03 0 0 0

206 6 200

.03 0 0 0

339.90 9.90 330

Chapter 7

Mini-Quiz 7-1

1. d. all of the above

2. Step Into

3. b. before

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 1 (Chapter 7)

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

' calculates and displays the quarterly commission

B-8

' declare named constant Const decRATE As Decimal = 0.1

' declare variables Dim decSales1 As Decimal Dim decSales2 As Decimal Dim decSales3 As Decimal Dim decCommission As Decimal

' assign input to variables Decimal.TryParse(txtSales1.Text, decSales1) Decimal.TryParse(txtSales2.Text, decSales2) Decimal.TryParse(txtSales3.Text, decSales3)

' calculate and display commission decCommission = (decSales1 + decSales2 + decSales3 ) * decRATE lblCommission.Text = decCommission.ToString( "C2")

End Sub

TRY THIS 2 (Chapter 7)

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displa ys the new weekly pay ' using a raise rate of 3%

Const dblRATE As Double = 0.03 Dim dblNewPay As Decimal Dim dblCurrentPay As Decimal

Double.TryParse(txtCurrentPay.Text, dblCurrentPay)

dblNewPay = dblCurrentPay + dblRATE * dblCurrentPay lblNewPay.Text = dblNewPay.ToString( "C2")

End Sub

Chapter 8

Mini-Quiz 8-1

1. The solution does not require a decision.

2. The solution requires a decision about whether the phone is ringing.

3. The solution does not require a decision.

4. The solution requires a decision about the color of the ball.

5. The solution requires a decision about the color of the dot on the price tag.

Mini-Quiz 8-2

1. If decHours > 40 Then lblMsg.Text = "Overtime pay"

End If

Chapter 8

2. If decHours > 40 Then lblMsg.Text = "Overtime pay"

Else

lblMsg.Text = "Regular pay only"

End If

3. d. only the selection structure’s false path

TRY THIS 1 (Chapter 8)

Output: answer

Input: first integer

second integer subtraction?

Algorithm: 1. enter the first integer, second integer, and subtraction? items 2. if subtraction?, do this:

calculate the answer by subtracting the second integer from the first integer otherwise, do this:

calculate the answer by adding the second integer to the first integer end if

3. display the answer

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates either the sum of or ' the difference between two numbers

Dim intNum1 As Integer Dim intNum2 As Integer Dim intAnswer As Integer

' assign numbers to variables Integer.TryParse(txtNum1.Text, intNum1) Integer.TryParse(txtNum2.Text, intNum2)

' calculate and display the difference or sum If chkSubtract.Checked = True Then

intAnswer = intNum1 – intNum2

B-9

Else intAnswer = intNum1 + intNum2

End If lblAnswer.Text = intAnswer

End Sub

TRY THIS 2 (Chapter 8)

intCode decCurrentPay decRate decRaise decNewPay

0 0 0 0 0

1 200 .03 6 206

0 0 0 0 0

3 200 .05 10 210

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

' calculates and displays the new weekly pay

B-10

Dim intCode As Integer Dim decCurrentPay As Decimal Dim decRate As Decimal Dim decRaise As Decimal Dim decNewPay As Decimal

' assign input to variables Integer.TryParse(txtCode.Text, intCode) Decimal.TryParse(txtCurrentPay.Text, decCurrentPay)

' calculate raise and new pay If intCode = 1 Then

decRate = 0.03 Else

decRate = 0.05 End If decRaise = decCurrentPay * decRate decNewPay = decCurrentPay + decRaise

' display new pay lblNewPay.Text = decNewPay.ToString( "C2")

End Sub

Chapter 9

Mini-Quiz 9-1

1. The solution does not require a nested selection structure.

2. The solution requires a nested selection structure whose condition determines whether the caller is a telemarketer.

3. if the bag contains trash, do this: if the lid is on the Trash container, do this:

lift the Trash container’s lid using your left hand end if drop the bag of trash in the Trash container put the lid back on the Trash container using your left hand

otherwise, do this:

if the lid is on the Recycle container, do this:

lift the Recycle container’s lid using your left hand end if drop the bag of recyclables in the Recycle container put the lid back on the Recycle container using your left hand

end if

4. ask the store clerk whether the store accepts the Discovery card if the store accepts the Discovery card, do this:

pay for your items using your Discovery card

otherwise, do this: ask the store clerk whether the store accepts the Vita card if the store accepts the Vita card, do this:

pay for your items using your Vita card otherwise, do this:

pay for your items using cash end if

end if

Chapter 9

Mini-Quiz 9-2

1. b. intNum < 0 OrElse intNum > 1000

2. c. dblPrice > 15.45 AndAlso dblPrice < 25.75

3. a. chkDiscount.Checked = True AndAlso chkCoupon.Checked = True

4. b. False

B-11

5. If radJanuary.Checked = True Then lblBirthstone.Text = "Garnet"

Else If radFebruary.Checked = True Then

lblBirthstone.Text = "Amethyst" Else

lblBirthstone.Text = "Aquamarine" End If

End If

TRY THIS 1 (Chapter 9)

To modify the btnCalc control’s Click event procedure, change the selection structure’s condition to chkEmployee.Checked = True OrElse intQuantity >= 10.

TRY THIS 2 (Chapter 9)

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displays the total amount due

Const decDISC_RATE_EMPLOYEES As Decimal = 0.12 Const decDISC_RATE_NON_EMPLOYEES As Decimal = 0.05 Dim intQuantity As Integer Dim decPrice As Decimal Dim decTotal As Decimal

' assign quantity and price to variables Integer.TryParse(txtQuantity.Text, intQuantity) Decimal.TryParse(txtPrice.Text, decPrice)

' calculate subtotal, discount, and total due decTotal = intQuantity * decPrice If chkEmployee.Checked = True Then

decTotal = decTotal * (1 - decDISC_RATE_EMPLOYEES ) Else

If intQuantity > 20 Then decTotal = decTotal * (1 - decDISC_RATE_NON_EMPLOYEES)

End If End If

' display total due lblTotalDue.Text = decTotal.ToString( "C2")

End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

Chapter 10

Mini-Quiz 10-1

B-12

1. If intPromoCode = 1 Then decRate = .02

ElseIf intPromoCode = 2 Then decRate = .05

ElseIf intPromoCode = 3 Then decRate = .1

ElseIf intPromoCode = 4 Then decRate = .25

Else decRate = 0

End If

2. strId = txtId.Text.ToUpper

3. If strId = "12A" Then lblFirst.Text = "Jerry" lblLast.Text = "Jones"

ElseIf strId = "45B" Then lblFirst.Text = "Mark" lblLast.Text = "Smith"

ElseIf strId = "67X" Then lblFirst.Text = "Jill" lblLast.Text = "Batist"

ElseIf strId = "78Y" Then lblFirst.Text = "Cheryl" lblLast.Text = "Sworski"

Else lblFirst.Text = "?" lblLast.Text = "?"

End If

Mini-Quiz 10-2

1. Select Case intPromoCode Case 1

decRate = .02 Case 2

decRate = .05 Case 3

decRate = .1 Case 4

decRate = .25 Case Else

decRate = 0 End Select

2. Select Case strId Case "12A"

lblFirst.Text = "Jerry" lblLast.Text = "Jones"

Case "45B" lblFirst.Text = "Mark" lblLast.Text = "Smith"

Case "67X" lblFirst.Text = "Jill" lblLast.Text = "Batist"

Case "78Y" lblFirst.Text = "Cheryl" lblLast.Text = "Sworski"

Case Else lblFirst.Text = "?" lblLast.Text = "?"

End Select

Chapter 10

3. d. none of the above

4. Select Case True Case radJanuary.Checked

lblBirthstone.Text = "Garnet" Case radFebruary.Checked

lblBirthstone.Text = "Amethyst" Case Else

lblBirthstone.Text = "Aquamarine" End Select

B-13

TRY THIS 1 (Chapter 10)

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the department name corresponding to a code

Dim intCode As Integer Integer.TryParse(txtCode.Text, intCode)

Select Case intCode

Case 1 lblName.Text = "Payroll"

Case 2 lblName.Text = "Personnel"

Case 3 lblName.Text = "IT"

Case Else lblName.Text = "Invalid code"

End Select End Sub

TRY THIS 2 (Chapter 10)

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displays the total am ount due

Const decDISC_RATE_EMPLOYEES As Decimal = 0.12 Const decDISC_RATE_NON_EMPLOYEES As Decimal = 0.05 Dim intQuantity As Integer Dim decPrice As Decimal Dim decTotal As Decimal

' assign quantity and price to variables Integer.TryParse(txtQuantity.Text, intQuantity) Decimal.TryParse(txtPrice.Text, decPrice)

' calculate subtotal, discount, and total due decTotal = intQuantity * decPrice Select Case True

Case chkEmployee.Checked decTotal = decTotal * (1 - decDISC_RATE_EMPLOYEES)

Case Else If intQuantity > 20 Then

decTotal = decTotal * (1 - decDISC_RATE_NON_EMPLOYEES) End If

End Select

' display total due lblTotalDue.Text = decTota l.ToString("C2")

End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

Chapter 11

B-14

Mini-Quiz 11-1

1. Test the application without entering any data.

2. 0

3. e

Mini-Quiz 11-2

1. MessageBox.Show("You win!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information)

2. txtState.Text = txtState.Text.Trim

3. strDept = strDept.Trim.ToUpper

TRY THIS 1 (Chapter 11)

Test data Expected result

Gold Club member check box not selected Standard radio button selected

$90

Deluxe radio button selected $115 Suite radio button selected $130

Gold Club member check box selected Standard radio button selected Deluxe radio button selected Suite radio button selected

$80

$105 $120

TRY THIS 2 (Chapter 11)

Some of the values listed in the first column in your testing chart may be different from those

shown here. However, your values should include 0, a negative integer, a negative non-integer, a

positive integer, a positive non-integer, and alphanumeric text. Your values should test each

path in the selection structure. Recall that when a selection structure’s condition contains a

range of values, the test data should include the lowest and highest values in the range, as well as

a value within the range. Therefore, your values should include the numbers 100 and 5000

(which are the lowest and highest values in the first condition) and also a number within that

range. Your values should also include the number 5000.01 (which is the lowest value in the

second condition), as well as another number that would make the second condition evaluate to

True.

Test data

No data entered Expected result

$0.00

Valid values 0

$0.00

100 $5.00 2500.75 $125.04 5000 $250.00 5000.01 $500.00 12000 $1,200.00 10 $0.00 –5 $0.00 –6.5 $0.00

Invalid values x, $6, 3A

$0.00

Chapter 12

Chapter 12

Mini-Quiz 12-1

1. The solution does not require a repetition structure.

2. The solution requires a repetition structure. The walk forward instruction will need to

be repeated until Carl is directly in front of the table (or while Carl is not directly in

front of the table).

3. repeat while there are flowers to pick and repeat until there are no more flowers to pick

Mini-Quiz 12-2

1. Do While intQuantity > 0

2. Do Until intQuantity <= 0

3. intNumEmployees = intNumEmployees + 1

4. System.Threading.Thread.Sleep(1000)

Mini-Quiz 12-3

1. intTotal = intTotal + intQuantity (or intTotal += intQuantity)

2. String.Empty

3. strItem = InputBox("Enter the item:", "Item Name")

4. intNum -= 5

TRY THIS 1 (Chapter 12)

Step b:

Private Sub btnClickHere_Click(sender As Object, e As EventArgs) Handles btnClickHere.Click

' blinks the message in the lblMsg control

' declare counter Dim intCount As Integer

' begin counting intCount = 1 Do Until intCount > 20

If lblMsg.Visible = False Then lblMsg.Visible = True

B-15

Else lblMsg.Visible = False

Loop End Sub

End If Me.Refresh() System.Threading.Thread.Sleep(250) ' update the counter intCount = intCount + 1

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

Step c:

Private Sub btnClickHere_Click(sender As Object, e As EventArgs) Handles btnClickHere.Click

' blinks the message in the lblMsg control

B-16

' declare counter Dim intCount As Integer

' begin counting intCount = 20 Do Until intCount < 1

If lblMsg.Visible = False Then lblMsg.Visible = True

Else lblMsg.Visible = False

Loop End Sub

End If Me.Refresh() System.Threading.Thread.Sleep(250) ' update the counter intCount = intCount - 1

TRY THIS 2 (Chapter 12)

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displays sales tax amounts

Const strPROMPT As String = "Enter a sales amount. Click Cancel to end."

Const strTITLE As String = "Sales Entry" Const decTAX_RATE As Decimal = 0.06 Dim strInputSales As String Dim decSales As Decimal Dim decTax As Decimal

' get the first sales am ount strInputSales = InputBox(strPROMPT, strTITLE, "0.00") Do Until strInputSales = String.Empty

Decimal.TryParse(strInputSales, decSales) decTax = decSales * decTAX_RATE MessageBox.Show(decTax.ToString( "C2"), "Sales Tax",

MessageBoxButtons.OK, MessageBoxIcon.Information) ' get another sales amount strInputSales = InputBox(strPROMPT, strTITLE, "0.00")

Loop End Sub

Chapter 13

Mini-Quiz 13-1

1. Loop While intQuantity > 0

2. Loop Until intQuantity <= 0

TRY THIS 1 (Chapter 13)

Processing steps

1. The intNum variable is initialized to 1.

2. The Do clause marks the beginning of the loop.

Chapter 14

3. The loop body instructions display the number 1 in a message box and then update the intNum variable’s value by adding 1 to it, giving 2.

4. The Loop clause checks whether the value in the intNum variable (2) is less than or equal

to 3. It is, so processing returns to the Do clause.

5. The Do clause marks the beginning of the loop.

6. The loop body instructions display the number 2 in a message box and then update the intNum variable’s value by adding 1 to it, giving 3.

7. The Loop clause checks whether the value in the intNum variable (3) is less than or equal

to 3. It is, so processing returns to the Do clause.

8. The Do clause marks the beginning of the loop.

9. The loop body instructions display the number 3 in a message box and then update the intNum variable’s value by adding 1 to it, giving 4.

10. The Loop clause checks whether the value in the intNum variable (4) is less than or equal

to 3. It’s not, so the loop ends.

TRY THIS 2 (Chapter 13)

Make the changes shaded in the following figure:

Private Sub btnClickHere_Click(sender As Object, e As EventArgs) Handles btnClickHere.Click

' blinks the message in the lblMsg control

' declare counter Dim intCount As Integer

' begin counting intCount = 1 Do

B-17

If lblMsg.Visible = False Then lblMsg.Visible = True

Else lblMsg.Visible = False

End If Me.Refresh() System.Threading.Thread.Sleep(250) ' update the counter intCount = intCount + 1

Loop Until intCount > 20 End Sub

Chapter 14

Mini-Quiz 14-1

1. For intX As Integer = 10 To 20 Step 2

2. For intX As Integer = 30 To 0 Step –2

3. c. negative number

Mini-Quiz 14-2

1. My.Computer.Audio.Play("Giggle.wav")

2. c. use the annual interest rate

3. lblCity.Text = "She lives in " & strCity

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-18

TRY THIS 1 (Chapter 14)

Processing steps

1. The For clause creates the decX variable and initializes it to 6.5.

2. The For clause compares the decX value (6.5) with the endValue (8.5) to determine

whether the loop should end. 6.5 is not greater than 8.5, so the MessageBox.Show

method displays the number 6.5 in a message box, and then the For clause increments decX by 1, giving 7.5.

3. The For clause compares the decX value (7.5) with the endValue (8.5) to determine

whether the loop should end. 7.5 is not greater than 8.5, so the MessageBox.Show

method displays the number 7.5 in a message box, and then the For clause increments decX by 1, giving 8.5.

4. The For clause compares the decX value (8.5) with the endValue (8.5) to determine

whether the loop should end. 8.5 is not greater than 8.5, so the MessageBox.Show

method displays the number 8.5 in a message box, and then the For clause increments decX by 1, giving 9.5.

5. The For clause compares the decX value (9.5) with the endValue (8.5) to determine

whether the loop should end. 9.5 is greater than 8.5, so the loop ends. Processing will

continue with the statement following the Next clause.

TRY THIS 2 (Chapter 14)

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the odd and even numbers ' from one integer to another integer

Dim intNum1 As Integer Dim intNum2 As Integer Dim intStep As Integer

Integer.TryParse(txtNum1.Text, intNum1) Integer.TryParse(txtNum2.Text, intNum2)

' determine stepValue If intNum1 > intNum2 Then

intStep = -1 Else

intStep = 1 End If

lblOdd.Text = String.Empty lblEven.Text = String.Empty

For intNumber As Integer = intNum1 To intNum2 Step intStep

If intNumber Mod 2 = 0 Then lblEven.Text = lblEven.Text &

intNumber & ControlChars.NewLine Else

lblOdd.Text = lblOdd.Text & intNumber & ControlChars.NewLine

End If Next intNumber

End Sub

Chapter 15

Chapter 15

Mini-Quiz 15-1

1. a. can be either a pretest loop or a posttest loop

2. repeat for each table that needs to be waited on: go to a table that needs to be waited on tell the customers at the table about the daily specials repeat for each customer at the table:

ask the customer for his or her order record the order on the order slip for that table

end repeat

go over to the cook at the counter tear the appropriate order slip from the order pad give the order slip to the cook

end repeat

B-19

Note: The go over to the cook at the counter and tear the appropriate order slip from the order pad

instructions can be reversed.

Mini-Quiz 15-2

1. Dim intOuter As Integer intOuter = 1 Do While intOuter < 4

For intNested As Integer = 1 To 4 lblPattern.Text =

lblPattern.Text & "X" Next intNested lblPattern.Text =

lblPattern.Text & ControlChars.NewLine intOuter += 1

Loop

2. Dim intNested As Integer For intOuter As Integer = 1 To 3

intNested = 1 Do While intNested < 5

lblPattern.Text = lblPattern.Text & "X"

intNested += 1 Loop lblPattern.Text =

lblPattern.Text & ControlChars.NewLine Next intOuter

3. b. Multiline

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 1 (Chapter 15)

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

' displays minutes (from 0 through 2 only) ' and seconds (from 0 through 5 only)

B-20 Dim intMinutes As Integer Dim intSeconds As Integer

Do Until intMinutes > 2

lblMinutes.Text = intMinutes intSeconds = 0 Do Until intSeconds > 5

lblSeconds.Text = intSeconds Me.Refresh() System.Threading.Thread.Sleep(500) intSeconds += 1

Loop End Sub

Loop intMinutes += 1

TRY THIS 2 (Chapter 15)

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

' displays minutes (from 0 through 2 only) ' and seconds (from 0 through 5 only)

Dim intSeconds As Integer

For intMinutes As Integer = 0 To 2

lblMinutes.Text = intMinutes intSeconds = 0 Do

lblSeconds.Text = intSeconds Me.Refresh() System.Threading.Thread.Sleep(500) intSeconds += 1

Loop While intSeconds <= 5 Next intMinutes

End Sub

Chapter 16

Mini-Quiz 16-1

1. a. arguments

2. b. above the first event procedure

3. d. TextChanged

Mini-Quiz 16-2

1. c. Private Sub Display(ByVal decX As Decimal, ByVal strY As String)

2. Call Display(decSales, strState)

3. ByRef

Chapter 16

TRY THIS 1 (Chapter 16)

Private Sub CalcAndDisplayBonus( ByVal decTotal As Decimal) ' calculates and displays the bonus

Dim decBonus As Decimal

If decTotal > 1200 Then decBonus = decTotal * 0.1

B-21

Else decBonus = decTotal * 0.08

End If lblBonus.Text = decBonus.ToString( "C2")

End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' prompts the user to enter two sales amounts, ' then totals both amounts, and then calls a ' Sub procedure to calculate and display the bonus

Dim strInputSale1 As String Dim strInputSale2 As String Dim decSale1 As Decimal Dim decSale2 As Decimal Dim decSum As Decimal

strInputSale1 = InputBox( "First sale amount", "Bonus Solution") strInputSale2 = InputBox( "Second sale amount", "Bonus Solution")

Decimal.TryParse(strInputSale1, decSale1) Decimal.TryParse(strInputSale2, decSale2) decSum = decSale1 + decSale2 Call CalcAndDisplayBonus(decSum)

End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 2 (Chapter 16)

B-22

Private Sub CalcBonus(ByVal decS1 As Decimal, ByVal decS2 As Decimal, ByRef decBonusAmt As Decimal)

' calculates the bonus

Dim decSum As Decimal

decSum = decS1 + decS2 If decSum > 1200 Then

decBonusAmt = decSum * 0.1 Else

decBonusAmt = decSum * 0.08 End If

End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' prompts the user to enter two sales amounts, ' then calls a Sub procedure to calculate the ' bonus, and then displays the bonus

Dim strInputSale1 As String Dim strInputSale2 As String Dim decSale1 As Decimal Dim decSale2 As Decimal Dim decBonus As Decimal

strInputSale1 = InputBox( "First sale amount", "Bonus Solution") strInputSale2 = InputBox( "Second sale amount", "Bonus Solution")

Decimal.TryParse(strInputSale1, decSale1) Decimal.TryParse(strInputSale2, decSale2) Call CalcBonus(decSale1, decSale2, decBonus) lblBonus.Text = decBonus.ToString( "C2")

End Sub

Chapter 17

Mini-Quiz 17-1

1. c. Private Function GetBonus(ByVal dblSold As Double) As Double

2. Return decIncome

3. decTax = GetSales(decSale1, decSale2) * .08

Chapter 17

TRY THIS 1 (Chapter 17)

Private Function GetBonus(ByVal decTotal As Decimal) As Decimal ' calculates the bonus

Dim decBonus As Decimal

If decTotal > 1200 Then decBonus = decTotal * 0.1

B-23

Else decBonus = decTotal * 0.08

End If Return decBonus

End Function

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' prompts the user to enter two sales amounts, ' then totals both amounts and calls a ' function to calculate the bonus, ' and then displays the bonus

Dim strSale1 As String Dim strSale2 As String Dim decSale1 As Decimal Dim decSale2 As Decimal Dim decSum As Decimal Dim decBonus As Decimal

strSale1 = InputBox("First sale amount", "Bonus Solution") strSale2 = InputBox("Second sale amount", "Bonus Solution")

Decimal.TryParse(strSale1, decSale1) Decimal.TryParse(strSale2, decSale2) decSum = decSale1 + decSale2 decBonus = GetBonus(decSum) lblBonus.Text = decBonus.ToString( "C2")

End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 2 (Chapter 17)

Private Function GetSum(ByVal decS1 As Decimal, ByVal decS2 As Decimal) As Decimal

' totals the sales amounts

B-24

Return decS1 + decS2 End Function

Private Function GetBonus(ByVal decTotal As Decimal) As Decimal

' calculates the bonus

Dim decBonus As Decimal

If decTotal > 1200 Then decBonus = decTotal * 0.1

Else decBonus = decTotal * 0.08

End If Return decBonus

End Function

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' prompts the user to enter two sales amounts, ' then calls a function to total both amounts, ' then calls a function to calculate the bonus, ' and then displays the bonus

Dim strSale1 As String Dim strSale2 As String Dim decSale1 As Decimal Dim decSale2 As Decimal Dim decSum As Decimal Dim decBonus As Decimal

strSale1 = InputBox("First sale amount", "Bonus Solution") strSale2 = InputBox("Second sale amount", "Bonus Solution")

Decimal.TryParse(strSale1, decSale1) Decimal.TryParse(strSale2, decSale2) decSum = GetSum(decSale1, decSale2) decBonus = GetBonus(decSum) lblBonus.Text = decBonus.ToString( "C2")

End Sub

Chapter 18

Mini-Quiz 18-1

1. Dim intQuantities(19) As Integer

2. 19

3. intQuantities(3) = 7

Chapter 18

Mini-Quiz 18-2

1. Array.Sort(strStates)

2. Length

3. a run time error will occur

TRY THIS 1 (Chapter 18)

The array declaration statement is entered in the form’s Declarations section.

Private strPartyList() As String = {"Jacob", "Karen", "Gregory", "Jerome", "Susan", "Michele", "Heather", "Jennifer", "George"}

Private Sub btnVerify_Click(sender As Object, e As EventArgs) Handles btnVerify.Click

' searches the array for a name and then ' displays an appropriate message

Dim strSearchFor As String Dim intSub As Integer Dim strFound As String

strSearchFor = txtGuest.Text.Trim.ToUpper

intSub = 0 strFound = "N" Do While strFound <> "Y" AndAlso intSub < strPartyList.Length

If strPartyList(intSub).ToUpper = strSearchFor Then strFound = "Y"

B-25

Else intSub += 1

Loop

End If

If strFound = "Y" Then MessageBox.Show(strSearchFor & " is invited.",

"Party List", MessageBoxButtons.OK, MessageBoxIcon.Information)

Else MessageBox.Show(strSearchFor & " is not invited.",

"Party List", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 2 (Chapter 18)

The array declaration statement is entered in the form’s Declarations section.

Private strGrades(10) As String

B-26

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load

' fill array with values

strGrades(0) = "A" strGrades(1) = "B" strGrades(2) = "C" strGrades(3) = "A" strGrades(4) = "B" strGrades(5) = "A" strGrades(6) = "F" strGrades(7) = "A" strGrades(8) = "D" strGrades(9) = "B" strGrades(10) = "C"

End Sub

Private Sub btnCount_Click(sender As Object, e As EventArgs) Handles btnCount.Click

' displays the number of times a specific ' letter appears in the array

Dim strSearchFor As String Dim intCount As Integer ' counter

strSearchFor = txtLetterGrade.Text.Trim.ToUpper

For intSub As Integer = 0 To strGrades.Length - 1 If strGrades(intSub) = strSearchFor Then

intCount += 1 End If

Next intSub

MessageBox.Show(strSearchFor & ": " & intCount.ToString, "Grades", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Chapter 19

Mini-Quiz 19-1

1. b. False

2. a. strCapital(1)

3. a. True

Chapter 19

TRY THIS 1 (Chapter 19)

The array declaration statements are entered in the form’s Declarations section.

Private strIds() As String = {"BX35", "CR20", "FE15", "KW10", "MM67"}

Private intPrices() As Integer = {13, 10, 12, 24, 4}

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the price associated with the product ' ID entered by the user

Dim strSearchFor As String Dim strFound As String Dim intSub As Integer

strSearchFor = txtId.Text.Trim.ToUpper

' search the strIds array for the product ID ' continue searching until there are ' no more array elements to search or ' the product ID is found intSub = 0 strFound = "N" Do Until intSub = strIds.Length OrElse strFound = "Y"

If strIds(intSub) = strSearchFor Then strFound = "Y"

B-27

Else intSub += 1

Loop

End If

' determine whether the product ID ' was found in the strIds array If strFound = "Y" Then

lblPrice.Text = intPrices(intSub).ToString( "C0") Else

MessageBox.Show("Invalid product ID", "Bluebird Gift Shop", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 2 (Chapter 19)

The array declaration statements are entered in the form’s Declarations section.

Private intTemps(9) As Integer Private intHighSub As Integer = intTemps.Length - 1

B-28 Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click

' gets the temperatures and stores them in the array

Dim strInputTemp As String

For intSub As Integer = 0 To intHighSub strInputTemp =

InputBox("Temperature " & intSub + 1, "Temperatures") Integer.TryParse(strInputTemp, intTemps(intSub))

Next intSub

lblHighest.Text = String.Empty lblLowest.Text = String.Empty

End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' display the highest and lowest temperature

Dim intHigh As Integer Dim intLow As Integer

intHigh = intTemps(0) intLow = intTemps(0)

For intSub As Integer = 0 To intHighSub

If intTemps(intSub) > intHigh Then intHigh = intTemps(intSub)

End If If intTemps(intSub) < intLow Then

intLow = intTemps(intSub) End If

Next intSub

lblHighest.Text = intHigh.ToString lblLowest.Text = intLow.ToString

End Sub

Chapter 20

Mini-Quiz 20-1

1. Private intQuantities(3, 1) As Integer

2. 3

3. intQuantities(2, 0) = 7

Mini-Quiz 20-2

1. d. none of the above

2. a. decSales(0, 1) += 20

3. a. For intRow As Integer = 0 To intNum.GetUpperBound(0)

Chapter 20

TRY THIS 1 (Chapter 20)

Private strPriceList(,) As String = {{"BX35", "13"}, {"CR20", "10"}, {"FE15", "12"}, {"KW10", "24"}, {"MM67", "4"}}

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the price associated with the product ' ID entered by the user

Dim strSearchFor As String Dim strFound As String Dim intRow As Integer Dim intNumRows As Integer

strSearchFor = txtId.Text.Trim.ToUpper

' search for the product ID in the first column ' of the array; continue searching until there are ' no more rows to search or the product ID is found intRow = 0 strFound = "N" intNumRows = strPriceList.GetUpperBound(0) + 1

Do Until intRow = intNumRows OrElse strFound = "Y"

If strPriceList(intRow, 0) = strSearchFor Then strFound = "Y"

B-29

Else intRow += 1

Loop

End If

' determine whether the product ID ' was found in the array If strFound = "Y" Then

Dim intPrice As Integer intPrice = strPriceList(intRow, 1) lblPrice.Text = intPrice.ToString( "C0")

Else MessageBox.Show("Invalid product ID",

"Bluebird Gift Shop", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 2 (Chapter 20)

Private intInventory(,) As Integer = {{34, 56}, {75, 67}, {5, 6}}

B-30

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the sum of the values stored in the array

Dim intTotal As Integer ' accumulator

' total the array values For intRow As Integer = 0 To intInventory.GetUpperBound(0)

For intCol As Integer = 0 To intInventory.GetUpperBound(1) intTotal = intTotal + intInventory(intRow, intCol)

Next intCol Next intRow

' display the total lblTotal.Text = intTotal.ToString

End Sub

Chapter 21

Mini-Quiz 21-1

1. Declarations

2. address.strStreet = "Maple"

3. inventory(4).intQuantity = 100

TRY THIS 1 (Chapter 21)

Structure SalesInfo Public decSale1 As Decimal Public decSale2 As Decimal Public decSale3 As Decimal

End Structure

Private Function GetCommission(ByVal company As SalesInfo) As Decimal ' calculates and returns the commission amount

Dim decTotal As Decimal

decTotal = company.decSale1 + company.decSale2 + company.decSale3 Return decTotal * 0.03

End Function

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' displays the commission

Dim companySales As SalesInfo Dim decCommission As Decimal

Decimal.TryParse(txtRegion1.Text, companySales.decSale1) Decimal.TryParse(txtRegion2.Text, companySales.decSale2) Decimal.TryParse(txtRegion3.Text, companySales.decSale3)

decCommission = GetCommission(com panySales) lblComm.Text = decCommission.ToString( "C2")

End Sub

Chapter 21

TRY THIS 2 (Chapter 21)

Structure Item Public strId As String Public intPrice As Integer

End Structure

' declare an array of structure variables Private gifts(4) As Item

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the price associated with the product ' ID entered by the user

' declare variables Dim strSearchFor As String Dim strFound As String Dim intSub As Integer

' assign the product ID to a variable strSearchFor = txtId.Text.Trim.ToUpper

' search the array for the product ID ' continue searching until there are ' no more array elements to search or ' the product ID is foun d intSub = 0 strFound = "N" Do Until intSub = gifts.Length OrElse strFound = "Y"

If gifts(intSub).strId = strSearchFor Then strFound = "Y"

B-31

Else intSub += 1

Loop

End If

' determine whether the product ID ' was found in the array If strFound = "Y" Then

lblPrice.Text = gifts(intSub).intPrice.ToString( "C0")

Else MessageBox.Show("Invalid product ID",

"Bluebird Gift Shop", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load

' assign IDs and prices to the array

gifts(0).strId = "BX35"

gifts(0).intPrice = 13

gifts(1).strId = "CR20"

gifts(1).intPrice = 10

gifts(2).strId = "FE15"

gifts(2).intPrice = 12

gifts(3).strId = "KW10"

gifts(3).intPrice = 24

gifts(4).strId = "MM67"

gifts(4).intPrice = 4

End Sub

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-32

Chapter 22

Mini-Quiz 22-1

1. Dim outFile As IO.StreamWriter

2. StreamWriter

3. txtCity.Focus()

Mini-Quiz 22-2

1. Dim inFile As IO.StreamReader

2. StreamReader

3. b. False

TRY THIS 1 (Chapter 22)

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

' writes a gross pay amount to a sequential access file

If txtGrossPay.Text <> String.Empty Then ' declare a StreamWriter variable Dim outFile As IO.StreamWriter ' open the file for append outFile = IO.File.AppendText("gross.txt") ' write the amount on a separate line in the file outFile.WriteLine(txtGrossPay.Text) ' close the file outFile.Close() ' clear the text boxes and then set the focus txtGrossPay.Text = String.Empty txtContents.Text = String.Empty txtGrossPay.Focus()

Else MessageBox.Show("Please enter a gross pay amount." ,

"ABC Company", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

Chapter 22

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays the gross pay amounts stored in ' a sequential access file

' declare variables Dim inFile As IO.StreamReader Dim strGrossPay As String Dim decGrossPay As Decimal

' clear the contents box txtContents.Text = String.Empty

' determine whether the file exists If IO.File.Exists("gross.txt") = True Then

' open the file for input inFile = IO.File.OpenText("gross.txt") ' process the loop instructions ' until the end of the file Do Until inFile.Peek = -1

' read an amount strGrossPay = inFile.ReadLine ' display the amount Decimal.TryParse(strGrossPay, decGrossPay) txtContents.Text = txtContents.Text &

decGrossPay.ToString("C2") & ControlChars.NewLine

B-33

Else

Loop

' close the file

inFile.Close()

MessageBox.Show("Can't find the gross.txt file",

"ABC Company",

MessageBoxButtons.OK,

MessageBoxIcon.Information)

End If

End Sub

gross.txt file

400

763

957.75

235.72

679.89

250

700

548

1100

1500.67

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-34

TRY THIS 2 (Chapter 22)

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' reads the names from a sequential ' access file and stores them in an array ' sorts the names and then displays them

Dim strFriends(4) As String Dim inFile As IO.StreamReader Dim intSub As Integer

lblFriends.Text = String.Empty

' determine whether the file exists If IO.File.Exists("names.txt") = True Then

' open the file for input inFile = IO.File.OpenText("names.txt") ' start the subscript at 0 intSub = 0 ' process the loop instructions until the ' end of the file or the array is filled Do Until inFile.Peek = -1 OrElse

intSub = strFriends.Length ' read a name and store it in the array strFriends(intSub) = inFile.ReadLine intSub += 1

Loop ' close the file inFile.Close()

Array.Sort(strFriends)

Else

' display the names For intSub = 0 To strFriends.Length - 1

lblFriends.Text = lblFriends.Text & strFriends(intSub) & ControlChars.NewLine

Next intSub

MessageBox.Show("Can't find the file" ,

"Friends", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If End Sub

Chapter 23

Mini-Quiz 23-1

1. strDollar = strDollar.Insert(0, "$")

2. strWord = strWord.Insert(2, "an")

3. strWord = strWord.Insert(1, "r")

4. intNum = strState.Length

Mini-Quiz 23-2

1. 5

2. –1

3. strCode = strPartNum.Substring(2, 3)

4. strCode = strPartNum.Remove(2, 3)

Chapter 24

TRY THIS 1 (Chapter 23)

strAmount = strAmount.Insert(0, "$")

strAmount = strAmount.Insert(2, ",")

strAmount = strAmount.Insert(6, ",")

TRY THIS 2 (Chapter 23)

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

' displays a shipping charge based on a ZIP code

Dim strZip As String Dim intShipping As Integer

strZip = txtZip.Text

' validate the ZIP code If strZip Like "605##" Then

intShipping = 25 ElseIf strZip Like "606##" Then

intShipping = 30

B-35

Else intShipping = 0 MessageBox.Show("Invalid ZIP code.",

"ZIP Code", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

' display shipping charge and set the focus lblShipping.Text = intShipping.ToString( "C0") txtZip.Focus()

End Sub

Chapter 24

Mini-Quiz 24-1

1. field

2. b. BindingSource

3. binding

Mini-Quiz 24-2

1. TblInventoryBindingSource.MoveFirst()

2. ex

3. b. False

TRY THIS 1 (Chapter 24)

Steps to connect the application to the database:

1. If necessary, open the Data Sources window by clicking VIEW on the menu bar, pointing

to Other Windows, and then clicking Data Sources.

2. Click Add New Data Source in the Data Sources window to start the Data Source

Configuration Wizard. If necessary, click Database on the Choose a Data Source Type

screen.

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

B-36

3. Click the Next button to display the Choose a Database Model screen. If necessary, click

Dataset.

4. Click the Next button to display the Choose Your Data Connection screen. Click the

New Connection button to open the Add Connection dialog box. If Microsoft Access

Database File (OLE DB) does not appear in the Data source box, click the Change button

to open the Change Data Source dialog box, click Microsoft Access Database File, and

then click the OK button to return to the Add Connection dialog box.

5. Click the Browse button in the Add Connection dialog box. Open the ClearlyVB2012\ Chap24\Access Databases folder and then click Employees.accdb in the list of filenames.

Click the Open button.

6. Click the Test Connection button. The “Test connection succeeded.” message appears in a message box. Close the message box.

7. Click the OK button to close the Add Connection dialog box. Employees.accdb appears

in the Choose Your Data Connection screen. Click the Next button.

8. Click the Yes button to add the Employees.accdb file to the application’s project folder. The Save the Connection String to the Application Configuration File screen appears

next. If necessary, select the Yes, save the connection as check box. Click the Next

button to display the Choose Your Database Objects screen.

9. Expand the Tables node and then expand the tblEmploy node. Click the empty box next

to tblEmploy and then click the Finish button.

Private Sub TblEmployBindingNavigatorSaveItem_Click( sender As Object, e As EventArgs) Handles TblEmployBindingNavigatorSaveItem.Click

Try Me.Validate() Me.TblEmployBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll( Me.EmployeesDataSet) MessageBox.Show("Changes saved", "Morgan Industries",

MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception

MessageBox.Show(ex.Message, "Morgan Industries", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'EmployeesDataSet.tblEmploy' table. You can move, or remove it, as needed.

Me.TblEmployTableAdapter.Fill( Me.EmployeesDataSet.tblEmploy)

End Sub

Chapter 25

Chapter 25

Mini-Quiz 25-1

1. name.LastName Like "A*"

2. Order By name.LastName Descending

3. Language Integrated Query

4. b. Order By

5. d. Where

Mini-Quiz 25-2

1. sales.JanSales

2. Select points.PointsEarned Into Average()

3. Min

B-37

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

TRY THIS 1 (Chapter 25)

Private Sub btnCode_Click(sender As Object, e As EventArgs) Handles btnCode.Click

' display record whose Code field contains EX33

B-38 Dim records = From magazine In MagazinesDataSet.tblMagazine

Where magazine.Code.ToUpper = "EX33" Select magazine

TblMagazineBindingSource.DataSource = records.AsDataView End Sub

Private Sub btnName_Click(sender As Object, e As EventArgs) Handles btnName.Click

' display Visual Basic re cord

Dim records = From magazine In MagazinesDataSet.tblMagazine

Where magazine.MagName.ToUpper = "VISUAL BASIC" Select magazine

TblMagazineBindingSource.DataSource = records.AsDataView End Sub

Private Sub btnAll_Click(sender As Object, e As EventArgs) Handles btnAll.Click

' display all records

Dim records = From magazine In MagazinesDataSet.tblMagazine Select magazine

TblMagazineBindingSource.DataSource = records.AsDataView End Sub

TRY THIS 2 (Chapter 25)

Private Sub btnAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAll.Click

' displays all records

Dim records = From magazine In MagazinesDataSet.tblMagazine

Select magazine TblMagazineBindingSource.DataSource = records.AsDataView

End Sub

Private Sub btnCost_Click(sender As Object, e As EventArgs) Handles btnCost.Click

' displays records having a cost of at least $4

Dim records = From magazine In MagazinesDataSet.tblMagazine Where magazine.Cost >= 4 Select magazine

TblMagazineBindingSource.DataSource = records.AsDataView End Sub

Chapter 26

Private Sub btnName_Click(sender As Object, e As EventArgs) Handles btnName.Click

' displays records whose nam es begin with C

Dim records = From magazine In MagazinesDataSet.tblMagazine Where magazine.MagName.ToUpper Like "C*" Select magazine

TblMagazineBindingSource.DataSource = records.AsDataView End Sub

Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click

' displays average cost

Dim avgCost =

Aggregate magazine In MagazinesDataSet.tblMagazine Select magazine.Cost Into Average()

MessageBox.Show("Average cost of a magazine: " & avgCost.ToString("C2"), "Magazines", MessageBoxButtons.OK, MessageBoxIcon.Information)

B-39

End Sub

Chapter 26

Mini-Quiz 26-1

1. object-oriented programming

2. b. Set

3. c. Return _strCity

Mini-Quiz 26-2

1. New

2. Dim dog As New Animal

3. a. True

TRY THIS 1 (Chapter 26)

Public Class Square Private _decSide As Decimal

Public Property Side As Decimal

Get Return _decSide

End Get Set(value As Decimal)

If value > 0 Then _decSide = value

Else _decSide = 0

End If End Set

End Property

Public Sub New() _decSide = 0

End Sub

Public Function GetArea() As Decimal

Return _decSide * _decSide End Function

End Class

A P P E N D I X B Answers to Mini-Quizzes and TRY THIS Exercises

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

' calculates and displays the area of a square

' instantiate a Square object Dim mySquare As New Square

B-40 ' declare a variable to store the area Dim decArea As Decimal

' assign input to the Square object's property Decimal.TryParse(txtSide.Text, mySquare.Side)

' use the Square object's method to calculate the area decArea = mySquare.GetArea

' display the area with two decimal places lblArea.Text = decArea.ToString( "N2")

txtSide.Focus()

End Sub

Chapter 27

Mini-Quiz 27-1

1. active server pages

2. Hypertext Markup Language

3. a. client computer

Mini-Quiz 27-2

1. c. Document object’s Title

2. b. ImageUrl

3. a. PostBackUrl

Mini-Quiz 27-3

1. b. Web server

2. a. RequiredFieldValidator