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...
?The default browser style for header cells is to display the text of the header in bold font and _____ within the cell.
A. ?left-aligned B. ?right-aligned C. ?centered horizontally D. ?positioned vertically
Case G-2Flippy and Hammy, two local party clowns, have been taking lots of digital photos during their recent gigs. They want to use the photos for promotion, both by posting them to their Web site and by creating some eye-catching ads and hand-outs. Most of their pictures have been taken against a solid black background. While he sharpens the photos, Flippy is extremely happy that they use a solid black curtain as a backdrop at their shows. Why?
A. The black background makes for much stronger edges, as edges are the points in an image where pixels that are close in color abut one another. B. It is much easier for him to use the Lasso tools to select portions of the image. C. With the black contrasts against the colors of their costumes, the edges are already very sharp. D. The black background can easily be replaced with colors that are more printer-friendly.