# Introduction to Problem Solving and Control Statements in Visual Basic 2010

• Print
This chapter is from the book

## Exercises

### Selection Statement Exercises

4.10

What does this code do? Assume that the user entered the value 27 into ageTextBox.

 ``` 1 Dim age As Integer 2 3 age = ageTextBox.Text 4 5 If age < 0 Then 6 ageLabel.Text = "Enter a value greater than or equal to zero." 7 ElseIf age < 13 Then 8 ageLabel.Text = "Child" 9 ElseIf age < 20 Then 10 ageLabel.Text = "Teenager" 11 ElseIf age < 30 Then 12 ageLabel.Text = "Young Adult" 13 ElseIf age < 65 Then 14 ageLabel.Text = "Adult" 15 Else 16 ageLabel.Text = "Senior Citizen" 17 End If ```

4.11

(Wage Calculator with Tax Calculations) Develop an application that calculates an employee's earnings, as shown in Fig. 4.23. The user should provide the hourly wage and number of hours worked per week. When the Calculate Button is clicked, the employee's gross earnings should display in the Gross earnings: TextBox. The Less FWT: TextBox should display the amount deducted for federal taxes and the Net earnings: TextBox should display the difference between the gross earnings and the federal tax amount. You can use the format specifier C with String method Format, to format the result as currency. Assume that overtime is calculated for hours over 40 hours worked, overtime wages are 1.5 times the hourly wage and federal taxes are 15% of gross earnings. The Clear Button should clear all fields. Set the TextAlign properties of the Labels that show the results to MiddleRight so that the values are aligned at their decimal points. [Note: In the next chapter, we'll introduce the data type Decimal for financial calculations.]

4.12

( Credit Checker Application) Develop an application (as shown in Fig. 4.24) that determines whether a department-store customer has exceeded the credit limit on a charge account. Each customer enters an account number (an Integer), a balance at the beginning of the month (a Double), the total of all items charged this month (a Double), the total of all credits applied to the customer's account this month (a Double), and the customer's allowed credit limit (a Double). The application should input each of these facts, calculate the new balance (= beginning balance – credits + charges), display the new balance and determine whether the new balance exceeds the customer's credit limit. You can use the format "{0:C}" with String method Format, to format the result as currency (see Fig. 4.24). If the customer's credit limit is exceeded, display a message (in messageLabel at the bottom of the Form) informing the customer of this fact; otherwise, display the customer's remaining credit amount. If the user presses the Clear Fields Button, the application should clear the TextBoxes and Labels. [Note: In the next chapter, we'll introduce the data type Decimal for financial calculations.]

#### Repetition Statement Exercises

4.13

(Enhanced Exam Analysis Application) Modify the program of Fig. 4.15 to process the four Strings: "P", "p", "F" and "f". If any other String input is encountered, increment a counter that tracks the number of invalid inputs. Display a message indicating the number of invalid inputs. Test your new program.

4.14

(Miles Per Gallon Application) Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording the miles driven and the gallons used for each tankful. Each time the user enters the miles driven and gallons used (both as Doubles) for a tankful and presses the Calculate MPG Button, the program should calculate the miles per gallon then display the miles driven, gallons used and miles per gallon in ListBoxes as shown in Fig. 4.25. The program should also display the total miles per gallon. (Be careful—this is not the average of the miles per gallon for each tankful.) All average calculations should produce floating-point results. Avoid division by zero—if the user enters zero for the number of gallons, inform the user that the input value for gallons must be greater than zero.

4.15

(Table of Powers Application) Write an application that displays a table of numbers from 1 to an upper limit, along with each number's squared value (for example, the number n to the power 2, or n ^ 2) and cubed value (the number n to the power 3, or n ^ 3). The user specifies the upper limit, and the results are displayed in a ListBox, as in Fig. 4.26.

4.16

(Using the Debugger: Odd Numbers Application) The Odd Numbers application should display all of the odd integers between one and the number input by the user. Open the Odd Numbers application from the ex04_16 folder located with this chapter's examples. Run the application. Note that an infinite loop occurs after you enter a value into the Upper limit: TextBox and click the Display Button. Select Debug > Stop Debugging to close the running application. Use the debugger to find and fix the error(s) in the application. Figure 4.27 displays the correct output for the application.