Chapter 4


[Page 650 (continued)]

Exercises 4.1

1.

Time's fun when you're having flies. Kermit the frog


3.

Why do clocks run clockwise? Since they were invented in the northern hemisphere where sundials go clockwise.



[Page 651]
5.

divorced beheaded died, divorced beheaded survived


7.

Keep cool, but don't freeze. Source: A jar of mayonnaise.


9.

88 keys on a piano

11.

It was the best of times. It was the worst of times.


13.

Your name has 7 letters. The first letter is G


15.

abcde

17.

144 items in a gross

19.

30% of M&M's Plain Chocolate Candies are brown.

21.

1440 minutes in a day

23.

t is the 6th letter of the word.

25.

A recent salary survey of readers of  Visual Basic Programmer's Journal gave  average salaries of database developers  according to the database used.  Sybase SQL Server programmers earned $75,633.  Oracle programmers earned $73,607.  Windows CE programmers earned $73,375.  Microsoft SQL Server programmers earned $68,295.


27.

President Clinton is a graduate of Georgetown University. President Bush is a graduate of Yale University.


29.

The first 6 letters are Visual.

31.

The negative of worldly is unworldly

33.

24 blackbirds baked in a pie.


35.

There is a parameter in the Sub procedure, but no argument in the statement calling the Sub procedure.

37.

Since Handles is a keyword, it cannot be used as the name of a Sub procedure.

39.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Display a lucky number.   Dim num As Integer = 7   Lucky(num) End Sub Sub Lucky(ByVal num As Integer)   'Display message.   txtOutput.Text = num & " is a lucky number." End Sub



[Page 652]
41.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Information about trees   Dim tree As String, ht As Double   Dim sr As IO.StreamReader = IO.File.OpenText("TREES.TXT")   lstBox.Items.Clear()   tree = sr.ReadLine   ht = CDbl(sr.ReadLine)   Tallest(tree, ht)   tree = sr.ReadLine   ht = CDbl(sr.ReadLine)   Tallest(tree, ht)   sr.Close() End Sub Sub Tallest(ByVal tree As String, ByVal ht As Double)   'Display information about tree.   lstBox.Items.Add("The tallest " & tree & " in the U.S. is " _                    & ht & " feet.") End Sub


43.

Private Sub btnTriple_Click(...) Handles btnTriple.Click   'Given a number, display its triple.   Dim num As Double   num = CDbl(InputBox("Enter a number:"))   Triple(num) End Sub Sub Triple(ByVal num As Double)  'Multiply the value of the number by 3.   txtResult.Text = "The triple of " & num & " is " & 3 * num & "." End Sub


45.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   Dim word As String, widthOfZone As Integer   'Enter a word and column number to display.   word = InputBox("Enter a word of at most ten letters.")   widthOfZone = CInt(InputBox("Enter a number between 10 and 20."))   PlaceNShow(word, widthOfZone) End Sub Sub PlaceNShow(ByVal word As String, _                ByVal widthOfZone As Integer)   'Display the word at the given column number.   Dim fmtStr As String = "{0," & widthOfZone & "}"   lstOutput.Items.Add("12345678901234567890")   lstOutput.Items.Add(String.Format(fmtStr, word)) End Sub


47.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Intended college majors   lstOutput.Items.Clear()   DisplaySource()   Majors(16, "business")   Majors(1.4, "computer science") End Sub Sub DisplaySource()  'Display the source of the information.   Dim phrase As String   phrase = "According to a 2004 survey of college freshmen" 
[Page 653]
lstOutput.Items.Add(phrase)
phrase = "taken by the Higher Educational Research Institute:" lstOutput.Items.Add(phrase) lstOutput.Items.Add("") End Sub Sub Majors(ByVal students As Double, ByVal field As String) lstOutput.Items.Add(students & _ " percent said they intend to major in " & field & ".") End Sub


49.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Favorite number   Dim num As Double   lstOutput.Items.Clear()   num = CDbl(txtBox.Text)   Sum(num)   Product(num) End Sub Sub Sum(ByVal num As Double)   Dim phrase As String   phrase = "The sum of your favorite number with itself is "   lstOutput.Items.Add(phrase & (num + num) & ".") End Sub Sub Product(ByVal num As Double)   Dim phrase As String   phrase = "The product of your favorite number with itself is "   lstOutput.Items.Add(phrase & (num * num) & ".") End Sub


51.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Old McDonald Had a Farm.   Dim animal, sound As String   Dim sr As IO.StreamReader = IO.File.OpenText("FARM.TXT")   animal = sr.ReadLine   sound = sr.ReadLine   ShowVerse(animal, sound)   animal = sr.ReadLine   sound = sr.ReadLine   ShowVerse(animal, sound)   animal = sr.ReadLine   sound = sr.ReadLine   ShowVerse(animal, sound)   sr.Close() End Sub Sub ShowVerse(ByVal animal As String, ByVal sound As String)   lstOutput.Items.Add("Old McDonald had a farm. Eyi eyi oh.")   lstOutput.Items.Add("And on his farm he had a " & animal & _                     ".  Eyi eyi oh.")   lstOutput.Items.Add("With a " & sound & " " & sound & _                     " here, and a " & sound & " " & sound & " there.")   lstOutput.Items.Add("Here a " & sound & ", there a " & sound & _                     ", everywhere a " & sound & " " & sound & ".")   lstOutput.Items.Add("Old McDonald had a farm. Eyi eyi oh.")   1stOutput.Items.Add("") End Sub



[Page 654]
53.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   Dim occupation As String, num00, num12 As Double   Dim sr As IO.StreamReader = IO.File.OpenText("GROWTH.TXT")   Dim fmtStr As String = "{0,-30}  {1,4}       {2,4}      {3,8}"   lstOutput.Items.Clear()   lstOutput.Items.Add(String.Format(fmtStr, "Occupation", 2000, _                     2012, "Increase"))   occupation = sr.ReadLine   num00 = CDbl(sr.ReadLine)   num12 = CDbl(sr.ReadLine)   CalculateAndDisplay(occupation, num00, num12)   occupation = sr.ReadLine   num00 = CDbl(sr.ReadLine)   num12 = CDbl(sr.ReadLine)   CalculateAndDisplay(occupation, num00, num12)   occupation = sr.ReadLine   num00 = CDbl(sr.ReadLine)   num12 = CDbl(sr.ReadLine)   CalculateAndDisplay(occupation, num00, num12)   occupation = sr.ReadLine   num00 = CDbl(sr.ReadLine)   num12 = CDbl(sr.ReadLine)   CalculateAndDisplay(occupation, num00, num12)   sr.Close() End Sub Sub CalculateAndDisplay(ByVal occupation As String, _                         ByVal num00 As Double, ByVal num12 As Double)   Dim perIncrease As Double   Dim fmtStr As String = "{0,-30}  {1,4}       {2,4}      {3,8:P0}"   perIncrease = (num12 - num00) / num00   lstOutput.Items.Add(String.Format(fmtStr, occupation, num00, _                     num12, perIncrease)) End Sub


Exercises 4.2

1.

9

3.

Can Can

5.

25

7.

Less is more.

9.

Gabriel was born in the year 1980

11.

Buckeyes

13.

0

15.

1 1


17.

4 overwhelming 8 whelming 4 whelming 4 ming 4 whelming



[Page 655]
19.

The variable c should be a parameter in the Sub procedure. That is, the header for the Sub procedure should be

Sub Sum(ByVal x As Double, ByVal y As Double, ByRef c As Double)


Also, the Dim statement in the Sub procedure should be deleted.

21.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   'Calculate sales tax   Dim price, tax, cost As Double   lstOutput.Items.Clear()   InputPrice(price)   Compute(price, tax, cost)   ShowData(price, tax, cost) End Sub Sub InputPrice(ByRef price As Double)   'Get the price of the item   price = CDbl(InputBox("Enter the price of the item:")) End Sub Sub Compute(ByVal price As Double, ByRef tax As Double, _            ByRef cost As Double)   'Calculate the cost   tax = 0.05 * price   cost = price + tax End Sub Sub ShowData(ByVal price As Double, ByVal tax As Double, _             ByVal cost As Double)   'Display bill   lstOutput.Items.Add("Price: " & price)   lstOutput.Items.Add("Tax:   " & tax)   lstOutput.Items.Add("-------")   lstOutput.Items.Add("Cost:  " & cost) End Sub


23.

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Determine the area of a rectangle.   Dim length, width, area As Double   InputSize(length, width)   ComputeArea(length, width, area)   ShowArea(area) End Sub Sub ComputeArea(ByVal length As Double, ByVal width As Double, _                 ByRef area As Double)   'Calculate the area.   area = length * width End Sub Sub InputSize(ByRef length As Double, ByRef width As Double)   'Get the dimensions of the rectangle.   length = CDbl(txtLength.Text)   width = CDbl(txtWidth.Text) End Sub Sub ShowArea(ByVal area As Double)   'Display the area of the rectangle.   txtOutput.Text = "The area of the rectangle is " & area End Sub



[Page 656]
25.

Dim str As String 'Place in the Declarations section

27.

Private Sub btnDetermine_Click(...) Handles btnDetermine.Click   'Get names, and display initials.   Dim firstName As String = "", lastName As String = ""   Dim firstInitial As String = "", lastInitial As String = ""   InputNames(firstName, lastName)   ExtractInitials(firstName, lastName, firstInitial, lastInitial)   DisplayInitials(firstInitial, lastInitial) End Sub Sub InputNames(ByRef first As String, ByRef last As String)   'Get the user's first and last name.   first = InputBox("Enter your first name:")   last = InputBox("Enter your last name:") End Sub Sub ExtractInitials(ByVal first As String, ByVal last As String, _                    ByRef fInit As String, ByRef lInit As String)   'Determine the initials of the first and last names.   fInit = first.Substring(0, 1)   lInit = last.Substring(0, 1) End Sub Sub DisplayInitials(ByVal fInit As String, _                    ByVal lInit As String)   'Display the initials.   txtInitials.Text = fInit & "." & lInit & "." End Sub


29.

Private Sub btnDetermine_Click(...) Handles btnDetermine.Click   'Get input and display percentage markup.   Dim cost, price, markup As Double   InputAmounts(cost, price)   ComputeMarkup(cost, price, markup)   DisplayMarkup(markup) End Sub Sub InputAmounts(ByRef cost As Double, ByRef price As Double)   cost = CDbl(InputBox("Enter the cost:"))   price = CDbl(InputBox("Enter the selling price:")) End Sub Sub ComputeMarkup(ByVal cost As Double, ByVal price As Double, _                  ByRef markup As Double)   markup = (price - cost) / cost End Sub Sub DisplayMarkup(ByVal markup As Double)   txtMarkup.Text = FormatPercent(markup) End Sub


31.

Private Sub btnDetermine_Click(...) Handles btnDetermine.Click   'Calculate batting average.   Dim name As String = ""   Dim atBats, hits As Integer   Dim ave As Double   ReadStats(name, atBats, hits)   ComputeAverage(atBats, hits, ave)   DisplayInfo(name, ave) End Sub 
[Page 657]
Sub ReadStats(ByRef name As String, ByRef atBats As Integer, _ ByRef hits As Integer) Dim sr As IO.StreamReader = IO.File.OpenText("4-2-E31.TXT") name = sr.ReadLine atBats = CInt(sr.ReadLine) hits = CInt(sr.ReadLine) sr.Close() End Sub Sub ComputeAverage(ByVal atBats As Integer, _ ByVal hits As Integer, ByRef ave As Double) ave = hits / atBats End Sub Sub DisplayInfo(ByVal name As String, ByVal ave As Double) Dim fmtStr As String = "{0,-15}{1,-10:N3}" lstOutput.Items.Add(String.Format(fmtStr, "Name", _ "Batting Average")) lstOutput.Items.Add(String.Format(fmtStr, name, ave)) End Sub


33.

Dim fmtStr As String = "{0,-14}{1,-10:C}{2,-10}{3,-10:C}" Dim sr As IO.StreamReader = IO.File.OpenText("MALLS.TXT") Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Display Hat Rack mall comparison table.   lstOutput.Items.Clear()   lstOutput.Items.Add(String.Format(fmtStr, "", "Rent per", "", ""))   lstOutput.Items.Add(String.Format(fmtStr, "", "Square", "Total", _                                  "Monthly"))   lstOutput.Items.Add(String.Format(fmtStr, "Mall Name", "Foot", _                                  "Feet", "Rent"))   lstOutput.Items.Add("")   DisplayInfo()   DisplayInfo()   DisplayInfo()   sr.Close() End Sub Sub ComputeRent(ByVal rentPerSqFoot As Double, _                ByVal squareFeet As Double, ByRef total As Double)   'Compute monthly rent given rent/sq. foot and number of square feet.   total = rentPerSqFoot * squareFeet End Sub Sub DisplayInfo()   'Display the information for a single mall.   Dim mall As String   Dim rentPerSqFoot, squareFeet, rent As Double   mall = sr.ReadLine   rentPerSqFoot = CDbl(sr.ReadLine)   squareFeet = CDbl(sr.ReadLine)   ComputeRent(rentPerSqFoot, squareFeet, rent)   lstOutput.Items.Add(String.Format(fmtStr, mall, rentPerSqFoot, _                       squareFeet, rent)) End Sub



[Page 658]
35.

Dim total As Double   'In Declarations section of Code window Private Sub btnProcessItem_Click(...) Handles btnProcessItem.Click   'Process item; display part of sales receipt.   Dim item As String = "", price As Double   InputData(item, price)   total = total + price   ShowData(item, price)   txtItem.Clear()   txtPrice.Clear()   txtItem.Focus() End Sub Private Sub btnDisplay_Click(...) Handles btnDisplay.Click   'Display sum, tax, and total.   Dim tax As Double   tax = total * 0.05   tax = Math.Round(tax, 2)   lstReceipt.Items.Add("                -------")   ShowData("Sum", total)   ShowData("Tax", tax)   ShowData("Total", total + tax) End Sub Sub InputData(ByRef item As String, ByRef price As Double)   'Input item name and price.   item = txtItem.Text   price = CDbl(txtPrice.Text) End Sub Sub ShowData(ByVal item As String, ByVal price As Double)   'Display data on specified line.   Dim fmtStr As String = "{0,-15}{1,8:C}"   lstReceipt.Items.Add(String.Format(fmtStr, item, price)) End Sub


Exercises 4.3

1.

203

3.

The population will double in 24 years.

5.

Volume of cylinder having base area 3.14159 and height 2 is 6.28318 Volume of cylinder having base area 28.27431 and height 4 is 113.09724


7.

TRain

9.

moral has the negative amoral political has the negative apolitical


11.

The function header should end with "As String", not "As Integer".

13.

Dim total As Double   'In Declarations section of Code window Private Sub btnDetermine_Click(...) Handles btnDetermine.Click   'Tin Needed for a Tin Can   Dim radius, height As Double   lstOutput.Items.Clear()   InputDims(radius, height)   ShowAmount(radius, height) End Sub 
[Page 659]
Sub InputDims(ByRef radius As Double, ByRef height As Double) radius = CDbl(InputBox("Enter radius of can:")) height = CDbl(InputBox("Enter height of can:")) End Sub Sub ShowAmount(ByVal radius As Double, ByVal height As Double) lstOutput.Items.Add("A can of radius " & radius & " and height " & _ height) lstOutput.Items.Add("requires " & CanArea(radius, height) & _ " square centimeters to make.") End Sub Function CanArea(ByVal radius As Double, ByVal height As Double) _ As Double 'Calculate surface area of a cylindrical can. Return 6.283 * (radius ^ 2 + radius * height) End Function


15.

Private Sub btnCalculate_Click(...) Handles btnCalculate.Click   txtBMI.Text = CStr(BMI(CDbl(txtWeight.Text), CDbl(txtHeight.Text))) End Sub Function BMI(ByVal w As Double, ByVal h As Double) As Double   'Calculate body mass index.   Return Math.Round((703 * w) / (h ^ 2)) End Function


17.

Private Sub btnDetermine_Click(...) Handles btnDetermine.Click   'Popcorn profits   Dim popcorn, butter, bucket, price As Double   InputAmounts(popcorn, butter, bucket, price)   ShowProfit(popcorn, butter, bucket, price) End Sub Sub InputAmounts(ByRef popcorn As Double, ByRef butter As Double, _                  ByRef bucket As Double, ByRef price As Double)   Dim phrase As String = "What is the cost (in dollars) of the "   popcorn = CDbl(InputBox(phrase & "popcorn kernels?"))   butter = CDbl(InputBox(phrase & "butter?"))   bucket = CDbl(InputBox(phrase & "bucket?"))   price = CDbl(InputBox("What is the sale price?")) End Sub Function Profit(ByVal popcorn As Double, ByVal butter As Double, _                ByVal bucket As Double, ByVal price As Double) As Double   'Calculate the profit on a bucket of popcorn.   Return price - (popcorn + butter + bucket) End Function Sub ShowProfit(ByVal popcorn As Double, ByVal butter As Double, _               ByVal bucket As Double, ByVal price As Double)   txtProfit.Text = FormatCurrency(Profit(popcorn, butter, bucket, _                                price)) End Sub


19.

Private Sub btnCompute_Click(...) Handles btnCompute.Click   'Original Cost of Airmail   Dim weight As Double   InputWeight(weight)   ShowCost(weight) End Sub  
[Page 660]
Function Ceil(ByVal x As Double) As Double Ceil = -Int(-x) End Function Function Cost(ByVal weight As Double) As Double 'Calculate the cost of an airmail letter. Return 0.05 + 0.1 * Ceil(weight - 1) End Function Sub InputWeight(ByRef weight As Double) weight = CDbl(txtWeight.Text) End Sub Sub ShowCost(ByVal weight As Double) txtOutput.Text = "The cost of mailing the letter was " & _ FormatCurrency(Cost(weight)) & "." End Sub


21.

Private Sub btnAddressNGreet_Click(...) Handles btnAddressNGreet.Click   'Display a greeting for a senator.   Dim name As String   name = InputBox("Enter the senator's name:")   lstOutput.Items.Clear()   lstOutput.Items.Add("The Honorable " & name)   lstOutput.Items.Add("United States Senate")   lstOutput.Items.Add("Washington, DC 20001")   lstOutput.Items.Add("")   lstOutput.Items.Add("Dear Senator " & LastName(name) & ",") End Sub Function LastName(ByVal name As String) As String   'Determine the last name of a two part name.   Dim spaceNmb As Integer   spaceNmb = name.IndexOf(" ")   Return name.Substring(spaceNmb + 1) End Function





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