208 Control Structures: Part 2 Chapter 5 24 (Unable to start debugging on the web server)
208 Control Structures: Part 2 Chapter 5 24 // create JTextArea to display output 25 JTextArea outputTextArea = new JTextArea(); 26 27 // set first line of text in outputTextArea 28 outputTextArea.setText( “YeartAmount on depositn” ); 29 30 // calculate amount on deposit for each of ten years 31 for ( int year = 1; year <= 10; year++ ) { 32 33 // calculate new amount for specified year 34 amount = principal * Math.pow( 1.0 + rate, year ); 35 36 // append one line of text to outputTextArea 37 outputTextArea.append( year + “t” + 38 moneyFormat.format( amount ) + “n” ); 39 40 } // end for structure 41 42 // display results 43 JOptionPane.showMessageDialog( null, outputTextArea, 44 “Compound Interest”, JOptionPane.INFORMATION_MESSAGE ); 45 46 System.exit( 0 ); // terminate the application 47 48 } // end method main 49 50 } // end class Interest Fig. 5.6 Calculating compound interest with the forstructure (part 2 of 2). Line 17 in method main declares three double variables and initializes two of them principal to 1000.0 and rate to .05. Java treats floating-point constants, like 1000.0 and .05 in Fig. 5.6, as type double. Similarly, Java treats whole number constants, like 7 and -22, as type int. Lines 21 22 declare NumberFormat reference moneyFormat and initialize it by calling static method getCurrencyInstance of class NumberFormat. This method returns a NumberFormatobject that can format numeric values as currency (e.g., in the United States, currency values normally are preceded with a dollar sign, $). The argument to the method Locale.US indicates that Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01