Title: Mgmt 362a Interest Calculator Application
111
- Mgmt 362a Interest CalculatorApplication
- Introducing the For...NextRepetition Statement
2Objectives
- In this tutorial you will learn
- Execute statements repeatedly with the For...Next
repetition statement. - Obtain user input with the NumericUpDown control.
- Display information, using a multiline TextBox.
- Use type String.
311.1 Test-Driving the Interest Calculator
Application
- You are considering investing 1,000.00 in a
savings account that yields 5 interest, and you
want to forecast how your investment will grow.
Assuming that you will leave all interest on
deposit, calculate and print the amount of money
in the account at the end of each year over a
period of n years. To compute these amounts, use
the following formula - a p (1 r)n
- where
- p is the original amount of money invested
(the principal) r is the annual interest rate
(for example, .05 is equivalent to 5) n is the
number of years a is the amount on deposit at
the end of the nth year.
411.1 Test-Driving the Interest Calculator
Application (Cont.)
- Figure 11.1 Completed Interest Calculator
application.
- Executing the application
511.1 Test-Driving the Interest Calculator
Application (Cont.)
- Enter data into application
- Principal and interest rate entered
- Using NumericUpDown control to increase years
- Program output
- Output displayed in multiline TextBox
- Vertical scrollbar enabled
611.1 Test-Driving the Interest Calculator
Application (Cont.)
- Figure 11.2 Output of completed Interest
Calculator application.
711.2 Essentials of Counter-Controlled Repetition
- Four essential elements of counter-controlled
repetition - The name of a control variable (or loop counter)
that is used to determine the whether the loop
continues to iterate - The initial value of a control variable
- The increment (or decrement) by which the control
variable is modified after each iteration - The condition that tests for the final value of
the control variable
8Outline
911.3 Introducing the For...Next Repetition
Statement
- Figure 11.4 For...Next header components.
10Good Programming Practice
- Place a blank line before and after each control
statement to make it stand out in the code.
11Error-Prevention Tip
- Although the value of the control variable can be
changed in the body of a For...Next loop,avoid
doing so, because this practice canlead to
subtle errors.
12Good Programming Practice
- Vertical spacing above and below
controlstatements, as well as indentation of the
bodiesof control statements, enhances
readability.
13Error-Prevention Tip
- If you use a For...Next loop for
counter-controlled repetition, off-by-one errors
(which occur when a loop is executed for one more
or one fewer iteration than is necessary) are
normally avoided, because the terminating value
is clear.
14Common Programming Error
- Counter-controlled loops should not be
controlledwith floating-point variables. These
are represented only approximately in the
computers memory, possibly resulting in
imprecise counter valuesand inaccurate tests for
termination that couldlead to logic errors.
15Outline
1611.5 Adding and Customizing a NumericUpDown
Control
- Figure 11.8 Template Interest Calculator
application Form in design view.
- Template Form in Design view
- Adding NumericUpDown control
- Adding a multiline TextBox
1711.5 Adding and Customizing a NumericUpDown
Control (Cont.)
- NumericUpDown control
- Increment property
- Increment property default value
- ReadOnly property
- Range Limits
- Maximum property
- Minimum property
18GUI Design Tip
- A NumericUpDown control should follow the same
GUI Design Guidelines as a TextBox. (See
Appendix C.)
1911.5 Adding and Customizing a NumericUpDown
Control (Cont.)
- Figure 11.9 NumericUpDown control added to
Interest Calculator application.
20Good Programming Practice
- Append the UpDown suffix to NumericUpDowncontrol
names.
21GUI Design Tip
- Use a NumericUpDown control to limitthe range of
user input.
2211.5 Adding and Customizing a MultilineTextBox
with a Scrollbar
- Multiline TextBox
- Can use scrollbar
- Enable vertical scrollbar
23GUI Design Tip
- If a TextBox will display multiple lines of
output,set the Multiline property to True and
left align the output by setting the TextAlign
property to Left.
24GUI Design Tip
- If a multiline TextBox will display many lines
ofoutput, limit the TextBox height and use a
vertical scrollbar to allow users to view
additional linesof output.
2511.5 Adding and Customizing a MultilineTextBox
with a Scrollbar (Cont.)
- Figure 11.10 Multiline TextBox with vertical
scrollbar added to the Form.
2611.5 Adding a Click Event Handler
- Declaring variables
- Declaring a counter variable
- Retrieving user input
- Storing user input into variables
2711.5 Adding a Click Event Handler (Cont.)
- Figure 11.11 Application code for retrieving
and storing user input.
2811.5 Adding a Click Event Handler (Cont.)
- Figure 11.12 Application code for displaying a
header in a multiline TextBox.
- Displaying header text in multiline TextBox
- Using ControlChars.Tab
- Using ControlChars.CrLf
2911.5 Calculating Cumulative Interest with
aFor...Next Statement
- Figure 11.13 Application code for For...Next
statement.
30 11.5 Calculating Cumulative Interest with
aFor...Next Statement (Cont.)
- Figure 11.14 Application code for displaying
calculation results.
- Displaying results in a multiline TextBox
31Outline
(1 of 2 )
Declare a variable of type String
Use the Value property of yearUpdDown
32Outline
Construct a header for the TextBox as a String
(2 of 2 )
Loop from 1 to year
Append result of calculation to the String named
output
Display results in resultTextBox