1 / 39

Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop Repetition Statements

9. Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop Repetition Statements. Outline. 9.1 Test-Driving the Car Payment Calculator Application 9.2 Do While ... Loop Repetition Statement 9.3 Do Until ... Loop Repetition Statement

ismet
Télécharger la présentation

Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop Repetition Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9 • Car Payment Calculator Application • Introducing the DoWhile...Loopand DoUntil...Loop Repetition Statements

  2. Outline • 9.1 Test-Driving the CarPaymentCalculatorApplication • 9.2DoWhile...Loop Repetition Statement • 9.3DoUntil...Loop Repetition Statement • 9.4 Constructing the CarPaymentCalculatorApplication

  3. In this tutorial you will learn: Use the DoWhile...Loop andDoUntil...Loop repetition statements toexecute statements in an application repeatedly. Use counter-controlled repetition. Display information in ListBoxes. Concatenate strings. Objectives

  4. 9.1 Test-Driving the CarPaymentCalculator Application • Typically, banks offer car loans for periods ranging from two to five years. Borrowers repay the loans in monthly installments. The amount of each monthly payment is based on the length of the loan, the amount borrowed and the interest rate. Create an application that allows the customer to enter the price of a car, the down-payment amount and the annual interest rate of the loan. The application should display the loan’s duration in months and the monthly payments for two-, three-, four- and five-year loans. The variety of options allows the user to easily compare repayment plans and choose the most appropriate.

  5. Test-Driving the Car PaymentCalculator Application • Note the new GUI control—the ListBox control, which allows users to view and/or select from multiple items in a list (Fig. 9.1). ListBox control Figure 9.1|CarPaymentCalculator application before data has been entered.

  6. Test-Driving the Car PaymentCalculator Application (Cont.) • Enter data to test the application (Fig. 9.2). Figure 9.2|CarPaymentCalculator application after data has been entered.

  7. Test-Driving the Car PaymentCalculator Application (Cont.) • Click the CalculateButton. • The application displays the monthly payment amounts in the ListBox (Fig. 9.3) in a tabular format. Results displayed in tabular format Figure 9.3|CarPaymentCalculator application displaying calculation results.

  8. 9.2 DoWhile...Loop Repetition Statement • A repetition statement can repeat actions, depending on the value of a condition. • If you go to the grocery store with a list of items to purchase, you go through the list until you have each item: Do while there are more items on my shopping list Put next item in cart Cross it off my list

  9. 9.2 DoWhile...Loop Repetition Statement (Cont.) • Using a DoWhile...Loop statement, this codefinds the first power of 3 greater than 50. Dim product As Integer = 3 Do While product <= 50 product *= 3Loop • The condition in the DoWhile...Loopstatement, product<=50, is referred to as theloop-continuationcondition. • When the loop-continuation condition becomes false, the DoWhile...Loop statement finishes executing.

  10. Good Programming Error • Provide in the body of every DoWhile...Loop statement an action that eventually causes the condition to become false. If you do not, the repetition statement never terminates, causing an error called an infinite loop. Such an error causes the application to “hang up.” When an infinite loop occurs in your application, return to the IDE and select Debug > Stop Debugging.

  11. 9.2 DoWhile...Loop Repetition Statement (Cont.) • The UML activity diagram in Fig. 9.4 illustrates the flow of control in the preceding DoWhile...Loop repetition statement. Figure 9.4|DoWhile...Loop repetition statement UML activity diagram.

  12. 9.2 DoWhile...Loop Repetition Statement (Cont.) • The transition arrow emerging from the action state points back to the merge, creating a loop. • The UML’s diamond-shaped mergesymbol joins two flows of activity into one.

  13. 9.3 DoUntil...Loop Repetition Statement • These statements describe the repetitive actions that occur during a shopping trip: Do until there are no more items on my shopping list Put next item in cart Cross it off my list • Statements in the body of a DoUntil...Loop are executed repeatedly for as long as the loop-termination condition remains False. • This is known as aloop-termination condition.

  14. 9.3 DoUntil...Loop Repetition Statement (Cont.) • Using aDoUntil...Loop, this code finds the first power of3 larger than 50: Dim product As Integer = 3 Do Until product > 50 product *= 3Loop

  15. Good Programming Error • Failure to provide the body of a DoUntil...Loop statement with an action that eventually causes the condition in the DoUntil...Loop to become true creates an infinite loop.

  16. 9.3 DoUntil...Loop Repetition Statement (Cont.) • The UML activity diagram in Fig. 9.5 illustrates the flow of control for the DoUntil...Loop repetition statement. Figure 9.5|DoUntil...Loop repetition statement UML activity diagram.

  17. 9.4 Constructing the CarPaymentCalculator Application When the user clicks the Calculate Button Initialize loan length to two years Clear the ListBox of any previous calculation results Add a header to the ListBox Get down payment from a TextBox Get sticker price from a TextBox Get annual interest rate from a TextBox Calculate loan amount (sticker price – down payment) Calculate monthly interest rate (annual interest rate / 12)

  18. 9.4 Constructing the CarPaymentCalculator Application (Cont.) Do while loan length is less than or equal to five years Convert the loan length in years to number of months Calculate monthly payment based on loan amount, monthly interest rate and loan length in months Insert result into ListBox Increment loan length in years by one year

  19. Action/Control/Event (ACE) Table for theCarPaymentCalculator • Use an ACE table to convert pseudocode into Visual Basic (Fig. 9.6). Figure 9.6 | CarPaymentCalculator application ACE table. Part (1 of 2.)

  20. Action/Control/Event (ACE) Table for theCarPaymentCalculator (Cont.) Figure 9.6 | CarPaymentCalculator application ACE table. Part (2 of 2.)

  21. Adding a ListBox to the CarPaymentCalculator Application • Add a ListBox control from the Toolbox. • Change the ListBox’s Name property to paymentsListBox. Set the Location property to24, 166 and the Size property to 230, 94 (Fig. 9.7). Figure 9.7|ListBox added to CarPaymentCalculator application’s Form.

  22. Good Programming Practice • Append the ListBox suffix to all ListBox control names.

  23. GUI Design Tip • A ListBox should be large enough to display all of its contents or large enough that scrollbars can be used easily.

  24. Using Code to Change a ListBox’s Contents • Double click the CalculateButton to generate the empty event handler (Fig. 9.8). • Content can be added and deleted from theListBoxby using itsItems property. • The Items property returns an object that contains a list of items displayed in the ListBox. Figure 9.8|Clearing the contents of aListBox.

  25. Using Code to Change aListBox’s Contents (Cont.) • The ListBox displays the number of monthly payments and the amount per payment. • To clarify what information is being displayed, a line of text—called aheader—is added to the ListBox by the Method Add (lines 10–11 of Fig. 9.9) Figure 9.9|Adding a header to aListBox.

  26. GUI Design Tip • Use headers in a ListBox when you are displaying tabular data. Adding headers improves readability by describing the information that is displayed in the ListBox.

  27. Using Code to Change aListBox’s Contents (Cont.) • The ampersand symbol (&) is called the string-concatenation operator. This operator combines its two operands into one string value. • The constant ControlChars.Tab inserts a tab character into the string.

  28. Declaring Variables and Retrieving User Input • The calculation requires the length in months, but the loop-continuation condition uses the number of years (Fig. 9.10). Variables to store the length of the loan Variables to store user input Variables to storecalculation results Figure 9.10|Variables for the CarPaymentCalculator application.

  29. Declaring Variables and RetrievingUser Input (Cont.) • Note that line 26 (Fig. 9.11) divides the interest rate by 100 to obtain the decimal equivalent • For example, 5% becomes .05 Figure 9.11|Retrieving input in the CarPaymentCalculator application.

  30. Declaring Variables and RetrievingUser Input (Cont.) • The application computes the amount of the loan by subtracting the down payment from the price. • These calculations need to occur only once, so they are placed before theDoWhile...Loop statement (Fig. 9.12). Figure 9.12|Determining amount borrowed and monthly interest rate.

  31. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement • After you type line 33 and press Enter, the IDE will close the repetition statement by adding the keyword Loop in line 35 (Fig. 9.13). Figure 9.13|Loop-continuation condition.

  32. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement (Cont.) • This loop is an example ofcounter-controlledrepetition. • This uses a counter (years) to control the number of times that a set of statements executes. • Counter-controlled repetition also is called definite repetition, because the number of repetitions is known before the repetition statement begins.

  33. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement (Cont.) • The number of months changes with each iteration of this loop, and the calculation result changes based on the length of the payment period (Fig. 9.14). Figure 9.14|Converting the loan duration from years to months.

  34. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement (Cont.) • The Pmt function returns a Double value that specifies the monthly payment amount on a loan for a constant interest rate and a given time period (Fig. 9.15). • Borrowed amounts are represented by negativevalues. Figure 9.15|Pmt function returns monthly payment.

  35. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement (Cont.) • The number of monthly payments and the monthly payment amounts are displayed beneath the header in the ListBox. • String.Format is used to display monthlyPayment in currency format (Fig. 9.16). Figure 9.16|Displaying the number of months and the amount of each monthly payment.

  36. Calculating the Monthly Payment Amounts witha DoWhile...Loop Repetition Statement (Cont.) • The counter variable years is incremented in each iteration until it equals 6 (Fig. 9.17). • Then the loop-continuation condition (years<=5) evaluates to False and the repetition ends. Figure 9.17|Incrementing the counter.

  37. Outline • Figure 9.18 presents the source code for the application. (1 of 3 ) Clear theListBox

  38. Outline (2 of 3 ) Add a header to the ListBox

  39. Outline (3 of 3 ) DoWhile...Loop repeats its body while years is less than or equal to 5 Calculate term in months Calculate monthlypayment amount Display number of months and monthly payment amount Increment counter years so the loop-continuation condition eventually becomes false

More Related