Write an application that calculates the amount of money in an account after 10 years for interest rates of 5%–10%, inclu- sive (Fig. 10.29). For this application, users must provide the initial principal.
) Copying the template to your working directory. Copy the C:Exam- plesTutorial10 ExercisesComparingRates directory to your C:SimplyJava directory.
b) Opening the template file. Open the ComparingRates.java file in your text editor. c) Customizing the JTextArea. You must customize the JTextArea to display the rate
and amount after 10 years. The name of this JTextArea is resultJTextArea. In line
58, insert code to set the bounds property to 20, 85, 260, 120. In line 59, insert code to set resultJTextArea’s editable property to false.
d) Completing a for statement header. In line 98, you will see a for statement header with only two semicolons. Before the first semicolon, declare and initialize variable rate (which will be used as our counter) to 5. Before the second semicolon, enter a loop-continuation condition that will cause the for statement to loop until the counter has reached 10. After the second semicolon, enter the increment of the
counter so that
```
1 //ComparingRates.java
2 // Application that calculates the amount of money the user will have
3 // after one year of saving $100 a month, plus an initial deposit.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.*;
8
9 public class ComparingRates extends JFrame
10 {
11 // JLabel and JTextField for principal amount
12 private JLabel principalJLabel;
13 private JTextField principalJTextField;
14
15 // JLabel and JTextArea for resulting amounts
16 private JLabel resultJLabel;
17 private JTextArea resultJTextArea;
18
19 // JButton calculates resulting amounts for various interest rates
20 private JButton calculateJButton;
21
22 // no-argument constructor
23 public ComparingRates()
24 {
25 createUserInterface();
26 }
27
28 // create and position GUI components; register event handlers
29 private void createUserInterface()
30 {
31 // get content pane for attaching GUI components
32 Container contentPane = getContentPane();
33
34 // enable explicit positioning of GUI components
35 contentPane.setLayout( null );
36
37 // set up principalJLabel
38 principalJLabel = new JLabel();
39 principalJLabel.setBounds( 20, 20, 80, 20 );
40 principalJLabel.setText( "Principal:" );
41 contentPane.add( principalJLabel );
42
43 // set up principalJTextField
44 principalJTextField = new JTextField();
45 principalJTextField.setBounds( 80, 20, 90, 20 );
46 principalJTextField.setText( "0" );
47 principalJTextField.setHorizontalAlignment( JTextField.RIGHT );
48 contentPane.add( principalJTextField );
49
50 // set up resultJLabel
51 resultJLabel = new JLabel();
52 resultJLabel.setBounds( 20, 60, 100, 20 );
53 resultJLabel.setText( "Result:" );
54 contentPane.add( resultJLabel );
55
56 // set up resultJTextArea
57 resultJTextArea = new JTextArea();
58 resultJTextArea.setBounds( 20, 85, 260, 120 );
59 resultJTextArea.setEditable( false );
60 contentPane.add( resultJTextArea );
61
62 // set up calculateJButton
63 calculateJButton = new JButton();
64 calculateJButton.setBounds( 190, 20, 90, 20 );
65 calculateJButton.setText( "Calculate" );
66 contentPane.add( calculateJButton );
67 calculateJButton.addActionListener(
68
69 new ActionListener() // anonymous inner class
70 {
71 // event handler called when calculateJButton is pressed
72 public void actionPerformed( ActionEvent event )
73 {
74 calculateJButtonActionPerformed( event );
75 }
76
77 } // end anonymous inner class
78
79 ); // end call to addActionListener
80
81 // set properties of application’s window
82 setTitle( "Comparing Rates" ); // set title bar text
83 setSize( 310, 255 ); // set window size
84 setVisible( true ); // display window
85
86 } // end method createUserInterface
87
88 // calculate and display amounts
89 private void calculateJButtonActionPerformed( ActionEvent event )
90 {
91 resultJTextArea.setText( "Rate (%)\tAmount after 10 years" );
92 DecimalFormat dollars = new DecimalFormat( "$0.00" );
93
94 int
95
96
97 //
98 for
99 { principal = Integer.parseInt(
principalJTextField.getText() );
for loop to calculate interest
( int rate = 5; rate <= 10; rate++ )
100 double amount = principal *
101 Math.pow( ( ( double ) 1 + rate / ( double ) 100 ), 10 );
102 resultJTextArea.append(
103 "\n" + rate + "\t" + dollars.format( amount ) );
104
105 } // end for
106
107 } // end method calculateJButtonActionPerformed
108
109 // main method
110 public static void main( String[] args )
111 {
112 ComparingRates application = new ComparingRates();
113 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
114
115 } // end method main
116
117 } // end class ComparingRates
```
You might also like to view...
What is the lost income from unoccupied rooms at the Grosvenor Hotel?
What will be an ideal response?
Which of the following statements terminates a program prematurely?
a. terminate( 0 ); b. exit( 1 ); c. end( 0 ); d. eof();
Match the following options with the tabs where they can be accessed:
I. add a picture to a slide II. remove a background from an image III. create a background from a picture IV. crop a video V. fade video in and out A. Picture Tools Format tab B. Design tab C. Video Tools Playback tab D. Video Tools Format tab E. Insert tab
Which service will allow a Windows server to be configured as a router to connect multiple subnets in a network or connect the network to the Internet?
A. RADIUS B. DirectAccess C. Certificate Services D. Routing and Remote Access