Chapter 3


[Page 638]

Exercises 3.1

1.

After a button is clicked, it has a blue border.

(In Exercises 3 through 24, begin by pressing Alt/F/N/P to begin a new project.)

3.

Click on the form to make it the selected object.

Click on the Properties window or Press F4 to activate the Properties window.

Move around with the up- and down-arrow keys until the Text property is highlighted.

Type "CHECKING ACCOUNT".

5.

Double-click the TextBox icon in the Toolbox.

Activate the Properties window, and highlight the BackColor property.

Click on the down-arrow button to the right of the Settings box.

Click on the Custom tab.

Click on the desired yellow in the palette.

Click on the form to see the empty, yellow text box.

7.

Double-click on the Label icon in the Toolbox.

Activate the Properties window, and highlight the AutoSize property.

Set the AutoSize property to False.

Highlight the Text property.

Type the requested sentence.

Highlight the TextAlign property.

Click on the down-arrow button to the right of the Settings box, and click on one of the center rectangles.

Click on the form.

Use the mouse to resize the label so that the sentence occupies three lines.

9.

Double-click on the TextBox icon in the Toolbox.

Activate the Properties window, and highlight the Text property.

Type "Visual Basic 2005".

Set the Name property to txtLanguage.

Highlight the Font property.

Click on the ellipsis to the right of the Settings box.

Scroll up the font list box, and click on Courier New in the Font box.

Click OK.

Resize the text box to accommodate its text.

Click on the form to see the resulting text box.

11.

Double-click on the Button icon in the Toolbox.

Activate the Properties window, and highlight the BackColor property.

Click on the down-arrow button to the right of the Settings box.

Click on the Custom tab, and then click on the white square in the palette.

Highlight the Text property.

Type "PUSH".

Highlight the Font property, and click on the ellipsis.


[Page 639]

Click on Italic in the Font style box.

Click on 24 in the Size box.

Click OK.

Click on the form to see the resulting button.

13.

Double-click on the Button icon in the Toolbox.

Activate the Properties window, and highlight the Text property.

Type "PUS&H".

Click on the form to see the resulting button.

15.

Double-click on the Label icon in the Toolbox.

Activate the Properties window, and highlight the Name property.

Type "lblAKA". (The name will appear in the Settings box.)

Highlight the Text property.

Type "ALIAS".

Highlight the AutoSize property.

Set the AutoSize property to False.

Highlight the Font property, and click on the ellipsis.

Click on Italic in the Font style box.

Click OK.

Highlight the TextAlign property.

Click on the down-arrow box to the right of the Settings box, and click on one of the center rectangles.

17.

Double-click on the Label icon in the Toolbox.

Highlight the AutoSize property.

Set the AutoSize property to False.

Activate the Properties window, and highlight the TextAlign property.

Click on the down-arrow box to the right of the Settings box, and click on one of the rectangles on the right.

Highlight the Text property.

Type "VISUAL BASIC" and press Enter.

If the words "VISUAL BASIC" are on one line, make the label narrower and longer, until the words appear on two lines.

19.

Double-click on the Label icon in the Toolbox.

Activate the Properties window, and highlight the Font property.

Click on the ellipsis to the right of the Settings box.

Click on Wingdings in the Font box.

Click on the largest size available (72) in the Size box.

Click OK.

Highlight the Text property.

Change the setting to a less than sign (<)

Click on the label.

Note: If you didn't know that the less than symbol corresponded to a diskette in the Wingdings font, you could double-click on the diskette character in the Character Map, click the Copy button, highlight the Text property, and press Ctrl + V. The less than character would appear in the Text settings box.


[Page 640]
21.

Double-click on the ListBox icon in the Toolbox.

Activate the Properties window, and highlight the BackColor property.

Click on the down-arrow button to the right of the Settings box.

Click on the Custom tab.

Click on the desired yellow square in the palette.

Click on the form to see the yellow list box.

23.

In the Solution Explorer window, right click on "Form1.vb" and select Rename from the Context menu.

Type "frmYellow.vb".

Right-click on the form in the Form Designer, and select Properties from the Context menu.

Click on the BackColor property in the Properties Window.

Click on the down-arrow button in the right part of the Settings box, and then click on the Custom tab.

Click on a square colored yellow.

25.

Begin a new project.

Change the text in the form's title bar to "Dynamic Duo".

Place two buttons on the form.

Enter as the text on the first Batman and on the second Robin.

Increase the font size for both buttons to 14.

27.

Begin a new project.

Change the text in the form's title bar to "Fill in the Blank".

Place a label, a text box, and another label on the form at appropriate locations.

Change the Text setting of the first label to "Toto, I don't think we're in" and of the second label to "A Quote from the Wizard of Oz".

Position the labels and text box as needed.

29.

Begin a new project.

Change the text in the form's title bar to "An Uncle's Advice".

Place on the form five labels and three buttons.

Set the AutoSize properties of the first and last labels to False.

Set the BackColor property of the last label to Blue, and set its ForeColor property to Yellow.

Change the Text property of each label as indicated.

Change the settings of the buttons' Text properties to "1", "2", and "3". Resize and position the labels and buttons as is appropriate.

33.

Each arrow key moves the text box in the indicated direction.

35.

"Center" refers to the midpoint horizontally, whereas "middle" refers to the midpoint vertically

37.

(a) All three buttons move left. (b) All three buttons become narrower. (c) The color of the text on all three buttons changes to blue at the same time. (d) The button with white sizing handles does not move or change size. The buttons with black sizing handles align with, and assume the size of, the button with white sizing handles.

39.

The setting toggles between True and False.


[Page 641]

Exercises 3.2

1.

The word "Hello"

3.

The word "Hello" on an orange-colored background

5.

The text box vanishes; nothing is visible.

7.

The word "Hello" in green letters

9.

The word "Hello" on a gold background

11.

Form1.Text should be Me.Text.

13.

Red should be replaced with Color.Red.

15.

Font.Size is a read-only property. The statement txtOutput.Text = txtBox.Font.Size is valid since it is reading the value of txtBox.Font.Size. However, txtBox.Font.Size = 20 is not valid since it is setting the value of txtBox.Font.Size.

17.

lblTwo.Text = "E.T. phone home."

19.

txtBox.ForeColor = Color.Red txtBox.Text = "The stuff that dreams are made of."


21.

txtBox.Enabled = False

23.

lblTwo.Visible = False

25.

btnOutcome.Enabled = True

27.

Me.BackColor = Color.White

29.

The Enter event occurs when the object gets the focus.

33.

Private Sub btnLeft_Click(...) Handles btnLeft.Click   txtBox.Text = "Left Justify"   txtBox.TextAlign = HorizontalAlignment.Left End Sub Private Sub btnCenter_Click(...) Handles btnCenter.Click   txtBox.Text = "Center"   txtBox.TextAlign = HorizontalAlignment.Center End Sub Private Sub btnRight_Click(...) Handles btnRight.Click   txtBox.Text = "Right Justify"   txtBox.TextAlign = HorizontalAlignment.Right End Sub


35.

Private Sub btnRed_Click(...) Handles btnRed.Click   txtBox.BackColor = Color.Red End Sub Private Sub btnBlue_Click(...) Handles btnBlue.Click   txtBox.BackColor = Color.Blue End Sub 
[Page 642]
Private Sub btnWhite_Click(...) Handles btnWhite.Click txtBox.ForeColor = Color.White End Sub


37.

Private Sub txtLife_Enter(...) Handles txtLife.Enter   txtQuote.Text = "I like life, it's something to do." End Sub Private Sub txtFuture_Enter(...) Handles txtFuture.Enter   txtQuote.Text = "The future isn't what it used to be." End Sub Private Sub txtTruth_Enter(...) Handles txtTruth.Enter   txtQuote.Text = "Tell the truth and run." End Sub


39.

 Private Sub btnOne_Click(...) Handles btnOne.Click    btnOne.Visible = False    btnTwo.Visible = True    btnThree.Visible = True    btnFour.Visible = True  End Sub  Private Sub btnTwo_Click(...) Handles btnTwo.Click    btnOne.Visible = True    btnTwo.Visible = False    btnThree.Visible = True    btnFour.Visible = True  End Sub  Private Sub btnThree_Click(...) Handles btnThree.Click    btnOne.Visible = True    btnTwo.Visible = True    btnThree.Visible = False    btnFour.Visible = True  End Sub  Private Sub btnFour_Click(...) Handles btnFour.Click    btnOne.Visible = True    btnTwo.Visible = True    btnThree.Visible = True    btnFour.Visible = False  End Sub


41.

Private Sub btnVanish_Click(...) Handles btnVanish.Click   lblFace.Visible = False End Sub Private Sub btnReappear_Click(...) Handles btnReappear.Click    lblFace.Visible = True End Sub


43.

Private Sub btnAny_Click(...) Handles btnOne.Click, btnTwo.Click   txtOutput.Text = "You just clicked on a button." End Sub



[Page 643]

Exercises 3.3

1.

12

3.

.125

5.

8

7.

0

9.

1

11.

Not valid

13.

Valid

15.

Not valid

17.

10

19.

16

21.

9

23.

Private Sub btnCompute_Click(...) Handles btnCompute.Click    lstOutput.Items.Add((7 * 8) + 5)  End Sub


25.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   lstOutput.Items.Add(0.055 * 20) End Sub


27.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   lstOutput.Items.Add(17 * (3 + 162)) End Sub


29.

 

x

y

Private Sub btnEvaluate_Click(...) Handles            btnEvaluate.Click


  

Dim x, y As Double

0

0

x = 2

2

0

y = 3 * x

2

6

x = y + 5

11

6

lstResults.Items.Clear()

11

6

lstResults.Items.Add(x + 4)

11

6

y = y + 1

11

7

End Sub

 


31.

6

33.

1 8 9


35.

1 64


37.

25 20



[Page 644]
39.

The third line should read c = a + b

41.

The first assignment statement should not contain a comma. The second assignment statement should not contain a dollar sign.

43.

9W is not a valid variable name.

45.

Dim quantity As Integer = 12

47.

10

49.

6

51.

3.128

53.

3

55.

0

57.

6

59.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim revenue, costs, profit As Double   revenue = 98456   costs = 45000   profit = revenue - costs   lstOutput.Items.Add(profit) End Sub


61.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim price, discountPercent, markdown As Double   price = 19.95   discountPercent = 30   markdown = (discountPercent / 100) * price   price = price - markdown   lstOutput.Items.Add(price) End Sub


63.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim balance As Double   balance = 100   balance += 0.05 * balance   balance += 0.05 * balance   balance += 0.05 * balance   lstOutput.Items.Add(balance) End Sub


65.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim balance As Double   balance = 100   balance = balance * (1.05 ^ 10)   lstOutput.Items.Add(balance) End Sub


67.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim acres, yieldPerAcre, corn As Double   acres = 30   yieldPerAcre = 18   corn = yieldPerAcre * acres   lstOutput.Items.Add(corn) End Sub



[Page 645]
69.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim distance, elapsedTime, averageSpeed As Double   distance = 233   elapsedTime = 7 - 2   averageSpeed = distance / elapsedTime   lstOutput.Items.Add(averageSpeed) End Sub


71.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim waterPerPersonPerDay, people, days, waterUsed As Double   waterPerPersonPerDay = 1600   people = 300000000   days = 365   waterUsed = waterPerPersonPerDay * people * days   lstOutput.Items.Add(waterUsed) End Sub


Exercises 3.4

1.

Visual Basic

3.

Ernie

5.

flute

7.

123

9.

Your age is 21.

11.

A ROSE IS A ROSE IS A ROSE

13.

5.5

15.

goodbye

17.

WALLAWALLA

19.

ABC 2 4 55 mph STU


21.

12 MUNICIPALITY city 6


23.

8 (0 through 7)

25.

True

27.

The variable phoneNumber should be declared as type String, not Double.

29.

End is a keyword and cannot be used as a variable name.

31.

The IndexOf method cannot be applied to a number, only a String.

33.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   Dim firstName, middleName, lastName As String   Dim yearOfBirth As Integer   firstName = "Thomas" 
[Page 646]
middleName = "Alva" lastName = "Edison" yearOfBirth = 1847 txtOutput.Text = firstName & " " & middleName & " " & lastName & _ ", " & yearOfBirth End Sub


35.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   Dim publisher As String   publisher = "Prentice-Hall, Inc."   txtOutput.Text = "(c)" & " " & publisher End Sub


37.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim distance As Double   distance = CDbl(txtNumSec.Text) / 5   distance = Math.Round(distance, 2)   txtOutput.Text = "The distance of the storm is " & distance & _                   " miles." End Sub


39.

Private Sub btnCompute_Click(...) Handles btnCompute.Click    Dim cycling, running, swimming, pounds As Double    cycling = CDbl(txtCycle.Text)    running = CDbl(txtRun.Text)    swimming = CDbl(txtSwim.Text)    pounds = (200 * cycling + 475 * running + 275 * swimming) / 3500    pounds = Math.Round(pounds, 1)    txtWtLoss.Text = pounds & " pounds were lost."  End Sub


41.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim income As Double   income = CDbl(txtRevenue.Text) - CDbl(txtExpenses.Text)   txtNetIncome.Text = CStr(income) End Sub


43.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim tip As Double   tip = CDbl(txtAmount.Text) * CDbl(txtPercentage.Text) / 100   txtTip.Text = CStr(Math.Round(tip, 2)) End Sub


45.

Private Sub btnPressMe_Click(...) Handles btnPressMe.Click   txtOutput.Text = CStr(CInt(txtOutput.Text) + 1) End Sub


47.

Private Sub btnModifySentence_Click(...) Handles _             btnModifySentence.Click   'Replace the first occurrence of a word   'in a given sentence with another word.   Dim sentence, oldWord, newWord As String   Dim position As Integer   sentence = txtSentence.Text   oldWord = txtOriginalWord.Text   newWord = txtReplacementWord.Text   position = sentence.IndexOf(oldWord)   txtOutput.Text = sentence.Substring(0, position) & newWord & _                    sentence.Substring(position + oldWord.Length) End Sub



[Page 647]
49.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Estimate the speed of a car given the stopping distance.   Dim speed, distance As Double   distance = CDbl(txtDistanceSkidded.Text)   speed = Math.Sqrt(24 * distance)   txtEstimatedSpeed.Text = CStr(speed) & "mph" End Sub


Exercises 3.5

1.

1,235


3.

1,234.0


5.

0.0


7.

-0.67

9.

12,346.000

11.

12

13.

$12,346

15.

($0.23)

17.

$0.80

19.

7.50%


21.

100.00%


23.

66.67%


25.

Pay to France $27,267,622.00


27.

25.6% of the U.S. population 25+ years old are college graduates.

29.

The likelihood of Heads is 50%

31.

12345678901234567890 1        2


33.

12345678901234567890     12


35.

12345678901234567890   A     Alice


37.

12345678901234567890     College   Mascot Univ. of MD   Terrapins        Duke    Blue Devils


39.

12345678901234567890 Element  Weight    Percent Oxygen   97.5      65.0 % Carbon   27.0      18.0 %


41.

16

43.

baseball

45.

Age: 20

47.

The White House has 132 rooms.


[Page 648]
49.

Sum: 444

51.

Harvard Harvard


53.

You might win 360 dollars.

55.

Hello John Jones

57.

MsgBox("Keep cool, but don't freeze.", 0, "Good Advice")

59.

In the text file, an expression was used where a numerical literal was expected.

61.

The second line should use CDbl to convert the right-hand side to Double.

63.

FormatNumber(123456) is a string and therefore cannot be assigned to a Double variable.

65.

You must insert String.Format( after the left parenthesis.

67.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Displays information about Americans' eating habits   Dim food, units As String, quantityPerDay As Double   Dim sr As IO.StreamReader = IO.File.OpenText("SODA.TXT")   food = sr.ReadLine   units = sr.ReadLine   quantityPerDay = CDbl(sr.ReadLine)   lstOutput.Items.Add("Americans consume " & quantityPerDay & _                       " " & units)   lstOutput.Items.Add("of " & food & " per day.")   sr.Close() End Sub


69.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Report percent increase for a basket of goods.   Dim begOfYearPrice, endOfYearPrice As Double   Dim percentIncrease As Double   begOfYearPrice = 200   endOfYearPrice = CDbl(InputBox("Enter price at the end of year:"))   percentIncrease = 100 * (endOfYearPrice - _                    begOfYearPrice) / begOfYearPrice   txtOutput.Text = "The percent increase for the year is " & _                   percentIncrease & "." End Sub


71.

000

73.

LLL000

75.

0-00-000000-&

77.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Report checking account balances.   Dim account As String   Dim beginningBalance, deposits, withdrawals As Double   Dim endOfMonthBalance, total As Double   Dim sr As IO.StreamReader = IO.File.OpenText("3-5-E77.TXT")   Dim fmtStr As String = "{0,-10}{1,10:C}"   lstOutput.Items.Clear()   lstOutput.Items.Add(String.Format(fmtStr, "Account", "Balance"))   '1st account   account = sr.ReadLine 
[Page 649]
beginningBalance = CDbl(sr.ReadLine) deposits = CDbl(sr.ReadLine) withdrawals = CDbl(sr.ReadLine) endOfMonthBalance = beginningBalance + deposits - withdrawals lstOutput.Items.Add(String.Format(fmtStr, account, _ endOfMonthBalance)) total = endOfMonthBalance '2nd account account = sr.ReadLine beginningBalance = CDbl(sr.ReadLine) deposits = CDbl(sr.ReadLine) withdrawals = CDbl(sr.ReadLine) endOfMonthBalance = beginningBalance + deposits - withdrawals lstOutput.Items.Add(String.Format(fmtStr, account, _ endOfMonthBalance)) total += endOfMonthBalance 'Increase total by endOfMonthBalance. '3rd account account = sr.ReadLine beginningBalance = CDbl(sr.ReadLine) deposits = CDbl(sr.ReadLine) withdrawals = CDbl(sr.ReadLine) endOfMonthBalance = beginningBalance + deposits - withdrawals lstOutput.Items.Add(String.Format(fmtStr, account, _ endOfMonthBalance)) total += endOfMonthBalance 'Increase total by endOfMonthBalance. lstOutput.Items.Add(String.Format(fmtStr, "Total", total)) sr.Close() End Sub


79.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Compute semester averages.   Dim socNmb As String   Dim exam1, exam2, exam3, final, average, total As Double   Dim fmtStr As String = "{0,-15}{1,8:N0}"   Dim sr As IO.StreamReader = IO.File.OpenText("3-5-E79.TXT")   lstOutput.Items.Clear()   lstOutput.Items.Add(String.Format(fmtStr, "Soc. Sec. No.", _                       "Average"))   '1st student   socNmb = sr.ReadLine   exam1 = CDbl(sr.ReadLine)   exam2 = CDbl(sr.ReadLine)   exam3 = CDbl(sr.ReadLine)   final = CDbl(sr.ReadLine)   average = (exam1 + exam2 + exam3 + final * 2) / 5   lstOutput.Items.Add(String.Format(fmtStr, socNmb, average))   total = average   '2nd student   socNmb = sr.ReadLine   exam1 = CDbl(sr.ReadLine)   exam2 = CDbl(sr.ReadLine)   exam3 = CDbl(sr.ReadLine)   final = CDbl(sr.ReadLine)   average = (exam1 + exam2 + exam3 + final * 2) / 5   lstOutput.Items.Add(String.Format(fmtStr, socNmb, average))   total += average  'Increase value of total by value of average. 
[Page 650]
'3rd student socNmb = sr.ReadLine exam1 = CDbl(sr.ReadLine) exam2 = CDbl(sr.ReadLine) exam3 = CDbl(sr.ReadLine) final = CDbl(sr.ReadLine) average = (exam1 + exam2 + exam3 + final * 2) / 5 lstOutput.Items.Add(String.Format(fmtStr, socNmb, average)) total += average 'Increase value of total by value of average. lstOutput.Items.Add(String.Format(fmtStr, "Class Average", _ total / 3)) sr.Close() End Sub


81.

Private Sub txtPhoneNumber_Enter(...) Handles txtPhoneNumber.Enter   MsgBox("Be sure to include the area code!", 0, "Reminder") End Sub


83.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   'Show growth of money in a savings account.   Dim principal, intRate, yrs, amt As Double   lstOutput.Items.Clear()   principal = CDbl(txtPrincipal.Text)   intRate = CDbl(txtIntRate.Text)   yrs = 10   amt = principal * (1 + intRate) ^ yrs   lstOutput.Items.Add("When " & FormatCurrency(principal))   lstOutput.Items.Add("is invested at " & FormatPercent(intRate))   lstOutput.Items.Add("interest for " & yrs & " years,")   lstOutput.Items.Add("the balance is " & FormatCurrency(amt)) End Sub


85.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   Dim price, quantity, revenue As Double   Dim sr As IO.StreamReader = IO.File.OpenText("3-5-E85.TXT")   price = CDbl(sr.ReadLine)   quantity = CDbl(sr.ReadLine)   revenue = price * quantity   txtOutput.Text = "Revenue: " & FormatCurrency(revenue)   sr.Close() End Sub





An Introduction to Programming Using Visual Basic 2005
Introduction to Programming Using Visual Basic 2005, An (6th Edition)
ISBN: 0130306541
EAN: 2147483647
Year: 2006
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net