30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

242 Procedures Chapter 6<br />

EXERCISES<br />

Return x + y<br />

or add the following statement at the end of the method body:<br />

Return result<br />

c) Error: Parameter value is redefined in the procedure definition.<br />

Correction: Delete the declaration Dim value As Single.<br />

d) Error: The procedure returns a value, but is defined as a Sub procedure.<br />

Correction: Change the procedure <strong>to</strong> a Function procedure with return type Integer.<br />

e) Error: The result of value += Sum(value - 1) is not returned by this recursive method,<br />

resulting in a logic error.<br />

Correction: Rewrite the statement in the Else clause as<br />

Return value + sum(value - 1)<br />

6.7 What is the value of x after each of the following statements is performed?<br />

a) x = Math.Abs(7.5)<br />

b) x = Math.Floor(7.5)<br />

c) x = Math.Abs(0.0)<br />

d) x = Math.Ceiling(0.0)<br />

e) x = Math.Abs(-6.4)<br />

f) x = Math.Ceiling(-6.4)<br />

g) x = Math.Ceiling(-Math.Abs(-8 + Math.Floor(-5.5)))<br />

6.8 A parking garage charges a $2.00 minimum fee <strong>to</strong> park for up <strong>to</strong> three hours. The garage<br />

charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum<br />

charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24<br />

hours at a time. Write a program that calculates and displays the parking charges for each cus<strong>to</strong>mer<br />

who parked a car in this garage yesterday. You should enter in a TextBox the hours parked for each<br />

cus<strong>to</strong>mer. The program should display the charge for the current cus<strong>to</strong>mer. The program should use<br />

the method CalculateCharges <strong>to</strong> determine the charge for each cus<strong>to</strong>mer. Use the techniques<br />

described in the chapter <strong>to</strong> read the Double value from a TextBox.<br />

6.9 Write a method IntegerPower(base, exponent) that returns the value of<br />

base exponent<br />

For example, IntegerPower(3, 4) = 3 * 3*3*3. Assume that exponent is a positive integer<br />

and that base is an integer. Method IntegerPower should use a For/Next loop or While loop <strong>to</strong><br />

control the calculation. Do not use any Math library methods or the exponentiation opera<strong>to</strong>r, ^. Incorporate<br />

this method in<strong>to</strong> a Windows application that reads integer values from TextBoxes for base<br />

and exponent from the user and performs the calculation by calling method IntegerPower.<br />

6.10 Define a method Hypotenuse that calculates the length of the hypotenuse of a right triangle<br />

when the other two sides are given. The method should take two arguments of type Double and<br />

return the hypotenuse as a Double. Incorporate this method in<strong>to</strong> a Windows application that reads<br />

integer values for side1 and side2 from TextBoxes and performs the calculation with the Hypotenuse<br />

method. Determine the length of the hypotenuse for each of the following triangles:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!