I need to explain this java code you can use comment to explain every point and
ID: 3634954 • Letter: I
Question
I need to explain this java code
you can use comment to explain every point and how the program process :
1 // Fig. 5.6: Interest.java
2 // Calculating compound interest.
3 import java.text.NumberFormat; // class for numeric formatting
4 import java.util.Locale; // class for country-specific information
5
6 import javax.swing.JOptionPane;
7 import javax.swing.JTextArea;
8
9 public class Interest {
10
11 public static void main( String args[] )
12 {
13 double amount; // amount on deposit at end of each year
14 double principal = 1000.0; // initial amount before interest
15 double rate = 0.05; // interest rate
16
17 // create NumberFormat for currency in US dollar format
18 NumberFormat moneyFormat =
19 NumberFormat.getCurrencyInstance( Locale.US );
20
21 // create JTextArea to display output
22 JTextArea outputTextArea = new JTextArea();
23
24 // set first line of text in outputTextArea
25 outputTextArea.setText( "Year Amount on deposit " );
26
27 // calculate amount on deposit for each of ten years
28 for ( int year = 1; year <= 10; year++ ) {
29
30 // calculate new amount for specified year
31 amount = principal * Math.pow( 1.0 + rate, year );
32
33 // append one line of text to outputTextArea
34 outputTextArea.append( year + " " +
35 moneyFormat.format( amount ) + " " );
36
37 } // end for
38
39 // display results
40 JOptionPane.showMessageDialog( null, outputTextArea,
41 "Compound Interest", JOptionPane.INFORMATION_MESSAGE );
42
43 System.exit( 0 ); // terminate the application
44
45 } // end main
46
47 } // end class Interest
Explanation / Answer
Please Rate:Thanks
1 // Fig. 5.6: Interest.java
2 // Calculating compound interest.
3 import java.text.NumberFormat; // class for numeric formatting
4 import java.util.Locale; // class for country-specific information
5
6 import javax.swing.JOptionPane; //it uses for input and output
7 import javax.swing.JTextArea; //it needed for use default font and color
8 //create class name Interest
9 public class Interest {
10 //Main method
11 public static void main( String args[] )
12 {
13 double amount; // amount on deposit at end of each year
14 double principal = 1000.0; // initial amount before interest
15 double rate = 0.05; // interest rate
16
17 // create NumberFormat for currency in US dollar format
18 NumberFormat moneyFormat =
19 NumberFormat.getCurrencyInstance( Locale.US );
20
21 // create JTextArea to display output
22 JTextArea outputTextArea = new JTextArea();
23
24 // set first line of text in outputTextArea
25 outputTextArea.setText( "Year Amount on deposit " );
26
27 // calculate amount on deposit for each of ten years
28 for ( int year = 1; year <= 10; year++ ) {
29
30 // calculate new amount for specified year
31 amount = principal * Math.pow( 1.0 + rate, year );
32
33 // append one line of text to outputTextArea
34 outputTextArea.append( year + " " +
35 moneyFormat.format( amount ) + " " );
36
37 } // end for
38
39 // display results
40 JOptionPane.showMessageDialog( null, outputTextArea,
41 "Compound Interest", JOptionPane.INFORMATION_MESSAGE );
42
43 System.exit( 0 ); // terminate the application
44
45 } // end main
46
47 } // end class Interest