Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator Application 10.2 Essentials of Counter Controlled Repetition 10.3 Introducing the for Repetition Statement 10.4 Examples Using the for Statement 10.5Constructing the Interest Calculator Application 10.6Wrap-Up Tutorial 10 – Interest Calculator Application Introducing the for Repetition Statement

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Execute statements repeatedly with the for repetition statement. –Use the JSpinner component to obtain input within a specified range of values. –Add scrollbars to a JTextArea by placing the component inside a JScrollPane.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 10.1Test Driving the Interest Calculator Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.1 Running the completed Interest Calculator application. Click to decrease the number of years JSpinner componentClick to increase the number of years

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 10.1Test Driving the Interest Calculator Application (Cont.) Entering values into the application –Enter 1000 in the Principal: JTextField –Enter 5 in the Interest rate: JTextField –Click the up arrow in the Years: JSpinner until the value reads 10 –Click the Calculate JButton

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.2 Completed Interest Calculator application (initial output). JTextArea displays the application results Scrollbar enables users to view all information in the JTextArea

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.3 Completed Interest Calculator application (output after scrolling up). Output not displayed previously Using the mouse to draw the scrollbar

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.4 Output from valid input. Valid input directly entered by typing in the JSpinner Arrows do not allow the user to select a value higher than 10 or lower than 1

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.5 Entering out-of-range input into a JSpinner. Invalid input directly entered by typing in the JSpinner

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 10.1Test Driving the Interest Calculator Application (Cont.) Figure 10.6 Entering input of the wrong type into a JSpinner. Invalid input directly entered by typing in the JSpinner Clicking the Calculate JButton causes the JSpinner to display the most recent valid value

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 10.2Essentials of Counter-Controlled Repetition Four essentials of counter-controlled repetition –the name of the control variable (or loop counter) –the initial value of the control variable –the increment (or decrement) –the condition that tests for the final value of the control variable

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 10.2Essentials of Counter-Controlled Repetition (Cont.) int counter = 2; // repetition counter while ( counter <= 5 ) { months = 12 * counter; // calculate payment period // get monthly payment monthlyPayment = calculateMonthlyPayment( monthlyInterest, months, loanAmount ); // insert result into JTextArea paymentsJTextArea.append( "\n" + months + "\t" + currency.format( monthlyPayment ) ); counter++; // increment counter } // end while Figure 10.7 Counter-controlled repetition example.

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 10.2Essentials of Counter-Controlled Repetition (Cont.) Four elements of counter-controlled repetition –Name: counter –Initial value: 2 –Increment: 1 –Final value: 5

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 10.3Introducing the for Repetition Statement Figure 10.8 Code segment for the Car Payment Calculator application that demonstrates the for loop. for ( int counter = 2; counter <= 5; counter+= 1 ) { months = 12 * counter; // calculate payment period // get monthly payment monthlyPayment = calculateMonthlyPayment( monthlyInterest, months, loanAmount ); // insert result into JTextArea paymentsJTextArea.append( "\n" + months + "\t" + currency.format( monthlyPayment ) ); } // end for

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 10.3Introducing the for Repetition Statement for statement header ( for header) –Specifies all four elements for counter-controlled repetition First expression: initial value, name Second expression: loop-continuation condition Third expression: increment Figure 10.9 for header components.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 10.3Introducing the for Repetition Statement (Cont.) Figure 10.10 for repetition statement UML activity diagram.

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 10.4Examples Using the for Statement for ( int i = 1; i <= 100; i++ ) for ( int i = 100; i >= 1; i-- ) for ( int i = 7; i <= 77; i += 7 ) for ( int i = 20; i >= 2; i -= 2 ) for ( int i = 2; i <= 20; i += 3 ) for ( int i = 99; i >= 0; i -= 11 ) 1, 2, 3, …, 100 100, 99, 98, …, 1 7, 14, 21, …, 77 20, 18, 16, …, 2 2, 5, 8, …, 20 99, 88, 77, …, 0

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 10.5Constructing the Interest Calculator Application Calculate the amount on deposit when the user clicks the Calculate JButton: Get the values for the principal, interest rate and years entered by the user Display a header in the JTextArea to label the output For each year, starting at 1 and ending with the number of years entered, Calculate and display the year Calculate and display the current value of the investment

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 10.5Constructing the Interest Calculator Application (Cont.)

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 10.5Constructing the Interest Calculator Application (Cont.) SpinnerNumberModel –Specifies that a JSpinner will contain a range of numerical values rather than other types of data, such as dates –Arguments First: initial values displayed in JSpinner Second: minimum value Third: maximum value Fourth: step size (increment)

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 10.5Constructing the Interest Calculator Application (Cont.) Figure 10.12 Specifying yearsJSpinner ’s initial value to be 1, its range of values to be 1 through 10 and its step size to be 1. Figure 10.13 Setting the bounds property of a JSpinner. Setting the yearsJSpinner ’s bounds property Creating a number JSpinner and setting its range

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 10.5Creating the Interest Calculator Application (Cont.) Figure 10.14 JSpinner added to Interest Calculator application. JSpinner component

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 10.5Creating the Interest Calculator Application (Cont.) JScrollPane –Container Contains other components –Causes scrollbars to appear

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 10.5Creating the Interest Calculator Application (Cont.) Figure 10.15 Setting the JTextArea ’s editable property to false. Setting yearlyBalanceJTextArea ’s editable property Figure 10.16 Adding yearlyBalanceJTextArea to y earlyBalanceJScrollPane. Adding scrollbars to yearlyBalanceJTextArea

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 10.5Creating the Interest Calculator Application (Cont.) Figure 10.17 Adding a JScrollPane that will hold a JTextArea. Setting yearlyBalanceJTextArea ’s bounds property

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 10.5Creating the Interest Calculator Application (Cont.) Figure 10.18 JTextArea with a JScrollPane added to the application. JTextArea displays the application results Vertical scrollbar (not visible)

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 10.5Creating the Interest Calculator Application (Cont.) Figure 10.19 Application code for retrieving and storing user input. Retrieve user input The getValue method returns the value displayed in the JSpinner as an Object Cast that Object to an Integer Convert that Integer to an int using the intValue method

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 10.5Creating the Interest Calculator Application (Cont.) Figure 10.20 Application code for displaying a header in a JTextArea. Placing header in the JTextArea and creating a DecimalFormat to format dollar amounts

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 10.5Creating the Interest Calculator Application (Cont.) Figure 10.21 Header output in the JTextArea.

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 10.5Creating the Interest Calculator Application (Cont.) Figure 10.22 Creating the for statement. Empty for statement The for statement loops once for each year up to the value selected by the user

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 10.5Creating the Interest Calculator Application (Cont.) Calculating the interest –Use the formula: a = p (1 + r) n –a : amount –p : principal –r : rate –n : number of years –To perform an exponential operation Use the Math.pow method

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 10.5Constructing the Interest Calculator Application (Cont.) Figure 10.23 Application code for calculating interest amount. Using Math method pow to calculate the amount on deposit after the specified number of years

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 10.5Constructing the Interest Calculator Application (Cont.) Figure 10.24 Appending output to yearlyBalanceJTextArea. Appending one line of text to the JTextArea. (Note the use of the escape sequence \n to create a new line and the escape sequence \t to tab to the next column.)

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 10.5Creating the Interest Calculator Application (Cont.) Figure 10.25 Completed application (with initial output).

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 10.5Creating the Interest Calculator Application (Cont.) Figure 10.26 Completed application (with remainder of output).

36  2004 Prentice Hall, Inc. All rights reserved. Outline 36 InterestCalculator.ja va (1 of 7) 1 // Tutorial 10: InterestCalculator.java 2 // Calculate the total value of an investment. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.text.*; 7 8 public class InterestCalculator extends JFrame 9 { 10 // JLabel and JTextField for principal invested 11 private JLabel principalJLabel; 12 private JTextField principalJTextField; 13 14 // JLabel and JTextField for interest rate 15 private JLabel interestRateJLabel; 16 private JTextField interestRateJTextField; 17 18 // JLabel and JTextField for the number of years 19 private JLabel yearsJLabel; 20 private JSpinner yearsJSpinner; 21 22 // JLabel and JTextArea display amount on deposit at 23 // the end of each year up to number of years entered 24 private JLabel yearlyBalanceJLabel; 25 private JTextArea yearlyBalanceJTextArea;

37  2004 Prentice Hall, Inc. All rights reserved. Outline 37 InterestCalculator.ja va (2 of 7) 26 27 // JScrollPane adds scrollbars to JTextArea for lengthy output 28 private JScrollPane yearlyBalanceJScrollPane; 29 30 // JButton calculates amount on deposit at the 31 // end of each year up to number of years entered 32 private JButton calculateJButton; 33 34 // no-argument constructor 35 public InterestCalculator() 36 { 37 createUserInterface(); 38 } 39 40 // create and position GUI components; register event handlers 41 private void createUserInterface() 42 { 43 // get content pane for attaching GUI components 44 Container contentPane = getContentPane(); 45 46 // enable explicit positioning of GUI components 47 contentPane.setLayout( null ); 48

38  2004 Prentice Hall, Inc. All rights reserved. Outline 38 InterestCalculator.ja va (3 of 7) 49 // set up principalJLabel 50 principalJLabel = new JLabel(); 51 principalJLabel.setBounds( 16, 16, 56, 24 ); 52 principalJLabel.setText( "Principal:" ); 53 contentPane.add( principalJLabel ); 54 55 // set up principalJTextField 56 principalJTextField = new JTextField(); 57 principalJTextField.setBounds( 100, 16, 100, 24 ); 58 principalJTextField.setHorizontalAlignment( 59 JTextField.RIGHT ); 60 contentPane.add( principalJTextField ); 61 62 // set up interestRateJLabel 63 interestRateJLabel = new JLabel(); 64 interestRateJLabel.setBounds( 16, 56, 80, 24 ); 65 interestRateJLabel.setText( "Interest rate:" ); 66 contentPane.add( interestRateJLabel ); 67 68 // set up interestRateJTextField 69 interestRateJTextField = new JTextField(); 70 interestRateJTextField.setBounds( 100, 56, 100, 24 ); 71 interestRateJTextField.setHorizontalAlignment( 72 JTextField.RIGHT ); 73 contentPane.add( interestRateJTextField );

39  2004 Prentice Hall, Inc. All rights reserved. Outline 39 InterestCalculator.ja va (4 of 7) 74 75 // set up yearsJLabel 76 yearsJLabel = new JLabel(); 77 yearsJLabel.setBounds( 16, 96, 48, 24 ); 78 yearsJLabel.setText( "Years:" ); 79 contentPane.add( yearsJLabel ); 80 81 // set up yearsJSpinner 82 yearsJSpinner = new JSpinner( 83 new SpinnerNumberModel( 1, 1, 10, 1 ) ); 84 yearsJSpinner.setBounds( 100, 96, 100, 24 ); 85 contentPane.add( yearsJSpinner ); 86 87 // set up yearlyBalanceJLabel 88 yearlyBalanceJLabel = new JLabel(); 89 yearlyBalanceJLabel.setBounds( 16, 136, 150, 24 ); 90 yearlyBalanceJLabel.setText( "Yearly account balance:" ); 91 contentPane.add( yearlyBalanceJLabel ); 92 93 // set up yearlyBalanceJTextArea 94 yearlyBalanceJTextArea = new JTextArea(); 95 yearlyBalanceJTextArea.setEditable( false ); 96 Setting the range of your JSpinner Customizing the bounds property of yearsJSpinner Setting yearlyBalanceJTextArea ’s editable property to false

40  2004 Prentice Hall, Inc. All rights reserved. Outline 40 InterestCalculator.ja va (5 of 7) 97 // set up yearlyBalanceJScrollPane 98 yearlyBalanceJScrollPane = new JScrollPane( 99 yearlyBalanceJTextArea ); 100 yearlyBalanceJScrollPane.setBounds( 16, 160, 300, 92 ); 101 contentPane.add( yearlyBalanceJScrollPane ); 102 103 // set up calculateJButton 104 calculateJButton = new JButton(); 105 calculateJButton.setBounds( 216, 16, 100, 24 ); 106 calculateJButton.setText( "Calculate" ); 107 contentPane.add( calculateJButton ); 108 calculateJButton.addActionListener( 109 110 new ActionListener() // anonymous inner class 111 { 112 // event handler called when calculateJButton is clicked 113 public void actionPerformed( ActionEvent event ) 114 { 115 calculateJButtonActionPerformed( event ); 116 } 117 118 } // end anonymous inner class 119 120 ); // end call to addActionListener 121 Adding a JTextArea to a JScrollPane and setting the bounds of the JScrollPane

41  2004 Prentice Hall, Inc. All rights reserved. Outline 41 InterestCalculator.ja va (6 of 7) 122 123 // set properties of window 124 setTitle( "Interest Calculator" ); // set window title 125 setSize( 340, 296 ); // set window size 126 setVisible( true ); // display window 127 128 } // end method createUserInterface 129 130 // calculate and display amounts 131 private void calculateJButtonActionPerformed( ActionEvent event ) 132 { 133 // declare variables to store user input 134 String principalText = principalJTextField.getText(); 135 double principal = Double.parseDouble( principalText ); 136 String rateText = interestRateJTextField.getText(); 137 double rate = Double.parseDouble( rateText ); 138 139 Integer integerObject = ( Integer ) yearsJSpinner.getValue(); 140 int year = integerObject.intValue(); 141 142 yearlyBalanceJTextArea.setText( "Year\tAmount on Deposit" ); 143 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 144 Retrieving user input Displaying the header for output and creating an object to format the remainder of the output

42  2004 Prentice Hall, Inc. All rights reserved. Outline 42 InterestCalculator.ja va (7 of 7) 145 // calculate the total value for each year 146 for ( int count = 1; count <= year; count++ ) 147 { 148 double amount = 149 principal * Math.pow( ( 1 + rate / 100 ), count ); 150 yearlyBalanceJTextArea.append( "\n" + count + "\t" + 151 dollars.format( amount ) ); 152 153 } // end for 154 155 } // end method calculateJButtonActionPerformed 156 157 // main method 158 public static void main( String args[] ) 159 { 160 InterestCalculator application = new InterestCalculator(); 161 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 162 163 } // end method main 164 165 } // end class InterestCalculator Using a for statement to calculate the amount on deposit and append this data to our JTextArea


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator."

Similar presentations


Ads by Google