Modify the application you developed in Exercise 10.11. to calculate the present value for a future value. Recall that to calculate the present investment you should use the formula:
where
p is the amount needed to achieve the future value
r is the annual interest rate (for example, 5 is equivalent to 5%)
n is the number of years
a is the future-value amount.
For example, if a customer wants to reach the financial goal of $15,000 over a period of 5 years when the interest rate is 6.6%, the customer would need to invest $10,896.96 as shown in Fig. 13.23. The years are to be entered with a JSpinner in the range from 0 to 30 in incre- ments of 5. Your application should clear the JTextArea if the values in any of the input components are modified. Your application should also provide the user with a Clear JBut- ton which will clear the JTextFields and JTextArea and reset the value in the JSpinner to the initial value (0).
) Copying the template to your work
```
1 // PresentValue.java
2 // Application that calculates how much money should be invested in
3 // order to achieve a certain financial goal.
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.text.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 public class PresentValue extends JFrame
11 {
12 // JLabel and JTextField for future value input
13 private JLabel futureValueJLabel;
14 private JTextField futureValueJTextField;
15
16 // JLabel and JTextField for interest rate input
17 private JLabel interestRateJLabel;
18 private JTextField interestRateJTextField;
19
20 // JLabel and JSpinner for number of years input
21 private JLabel yearsJLabel;
22 private JSpinner yearsJSpinner;
23
24 // JLabel and JTextArea for initial amounts
25 // needed to reach future value
26 private JLabel amountNeededJLabel;
27 private JTextArea amountNeededJTextArea;
28
29 // JButton calculates initial amounts needed
30 // to reach future value
31 private JButton calculateJButton;
32
33 // JButton to clear values
34 private JButton clearJButton;
35
36 // no-argument constructor
37 public PresentValue()
38 {
39 createUserInterface();
40 }
41
42 // create and position GUI components; register event handlers
43 private void createUserInterface()
44 {
45 // get content pane for attaching GUI components
46 Container contentPane = getContentPane();
47
48 // enable explicit positioning of GUI components
49 contentPane.setLayout( null );
50
51 // set up futureValueJLabel
52 futureValueJLabel = new JLabel();
53 futureValueJLabel.setBounds( 20, 25, 90, 20 );
54 futureValueJLabel.setText( "Future value:" );
55 contentPane.add( futureValueJLabel );
56
57 // set up futureValueJTextField
58 futureValueJTextField = new JTextField();
59 futureValueJTextField.setBounds( 130, 25, 100, 20 );
60 futureValueJTextField.setHorizontalAlignment(
61 JTextField.RIGHT );
62 contentPane.add( futureValueJTextField );
63 futureValueJTextField.addKeyListener(
64
65 new KeyAdapter() // anonymous inner class
66 {
67 // event handler called when
68 // futureValueJTextField is edited
69 public void keyPressed( KeyEvent event )
70 {
71 futureValueJTextFieldKeyPressed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end call to addKeyListener
77
78 // set up interestRateJLabel
79 interestRateJLabel = new JLabel();
80 interestRateJLabel.setBounds( 20, 60, 90, 20 );
81 interestRateJLabel.setText( "Interest rate:" );
82 contentPane.add( interestRateJLabel );
83
84 // set up interestRateJTextField
85 interestRateJTextField = new JTextField();
86 interestRateJTextField.setBounds( 130, 60, 100, 20 );
87 interestRateJTextField.setHorizontalAlignment(
88 JTextField.RIGHT );
89 contentPane.add( interestRateJTextField );
90 interestRateJTextField.addKeyListener(
91
92 new KeyAdapter() // anonymous inner class
93 {
94 // event handler called when
95 // interestRateJTextField is edited
96 public void keyPressed( KeyEvent event )
97 {
98 interestRateJTextFieldKeyPressed( event );
99 }
100
101 } // end anonymous inner class
102
103 ); // end call to addKeyListener
104
105 // set up yearsJLabel
106 yearsJLabel = new JLabel();
107 yearsJLabel.setBounds( 20, 95, 80, 20 );
108 yearsJLabel.setText( "Years:" );
109 contentPane.add( yearsJLabel );
110
111 // set up yearsJSpinner
112 yearsJSpinner = new JSpinner(
113 new SpinnerNumberModel( 0, 0, 30, 5 ) );
114 yearsJSpinner.setBounds( 130, 95, 100, 20 );
115 contentPane.add( yearsJSpinner );
116 yearsJSpinner.addChangeListener(
117
118 new ChangeListener() // anonymous inner class
119 {
120 // event handler called when yearsJSpinner is changed
121 public void stateChanged( ChangeEvent event )
122 {
123
124 }
125 yearsJSpinnerStateChanged( event );
126 } // end anonymous inner class
127
128 ); // end call to addChangeListener
129
130 // set up resultJLabel
131 amountNeededJLabel = new JLabel();
132 amountNeededJLabel.setBounds( 20, 130, 150, 20 );
133 amountNeededJLabel.setText( "Annual amount needed:" );
134 contentPane.add( amountNeededJLabel );
135
136 // set up amountNeededJTextArea
137 amountNeededJTextArea = new JTextArea();
138 amountNeededJTextArea.setBounds( 20, 155, 320, 115 );
139 amountNeededJTextArea.setEditable( false );
140 contentPane.add( amountNeededJTextArea );
141
142 // set up calculateJButton
143 calculateJButton = new JButton();
144 calculateJButton.setBounds( 250, 25, 90, 20 );
145 calculateJButton.setText( "Calculate" );
146 contentPane.add( calculateJButton );
147 calculateJButton.addActionListener(
148
149 new ActionListener() // anonymous inner class
150 {
151 // event handler called when calculateJButton is pressed
152 public void actionPerformed( ActionEvent event )
153 {
154 calculateJButtonActionPerformed( event );
155 }
156
157 } // end anonymous inner class
158
159 ); // end call to addActionListener
160
161 clearJButton = new JButton();
162 clearJButton.setBounds( 250, 60, 90, 20 );
163 clearJButton.setText( "Clear" );
164 contentPane.add( clearJButton );
165 clearJButton.addActionListener(
166
167 new ActionListener() // anonymous inner class
168 {
169 // event handler called when clearJButton is pressed
170 public void actionPerformed( ActionEvent event )
171 {
172 clearJButtonActionPerformed( event );
173 }
174
175 } // end anonymous inner class
176
177
178
179
180
181
182
183
184 } // end method createUserInterface
185
186 // calculate and display amounts
187 private void calculateJButtonActionPerformed( ActionEvent event )
188 {
189 String futureValueText = futureValueJTextField.getText();
190 int futureValue = Integer.parseInt( futureValueText );
191 String rateText = interestRateJTextField.getText();
192 double rate = Double.parseDouble( rateText );
193
194 Integer integerObject = ( Integer ) yearsJSpinner.getValue();
195 int years = integerObject.intValue();
196
197 amountNeededJTextArea.setText( "Year\tInitial Deposit" );
198 DecimalFormat dollars = new DecimalFormat( "$0.00" );
199
200 // for loop calculates total amount
201 for ( int counter = 5; counter <= years; counter += 5 )
202 {
203 double amount = futureValue / Math.pow(
204 ( 1 + rate / 100 ), counter );
205 amountNeededJTextArea.append(
206 "\n" + counter + "\t" + dollars.format( amount ) );
207
208 } // end for
209
210 } // end method calculateJButtonActionPerformed
211
212 // clear the JTextArea
213 private void futureValueJTextFieldKeyPressed( KeyEvent event )
214 {
215 amountNeededJTextArea.setText( "" );
216
217 } // end method futureValueJTextFieldKeyPressed
218
219 // clear the JTextArea
220 private void interestRateJTextFieldKeyPressed( KeyEvent event )
221 {
222 amountNeededJTextArea.setText( "" );
223
224 } // end method interestRateJTextFieldKeyPressed
225
226 // clear the JTextArea
227 private void yearsJSpinnerStateChanged( ChangeEvent event )
228 {
229 amountNeededJTextArea.setText( "" );
230
231 } // end method yearsJSpinnerStateChanged
232
233 // clear components and display
234 private void clearJButtonActionPerformed( ActionEvent event )
235 {
236 // clear the JTextFields
237 futureValueJTextField.setText( "" );
238 interestRateJTextField.setText( "" );
239
240 // reset the value in the JSpinner
241 yearsJSpinner.setValue( new Integer(
242
243 // clear the JTextArea
244 amountNeededJTextArea.setText( "" );
245
246 } // end method clearJButtonActionPerformed
247
248 //
249 public static void main( String args[] )
250 {
251 PresentValue application = new PresentValue();
252 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
253
254 } // end method main
255
256 } // end class PresentValue
```
You might also like to view...
Present value (Pv) represents the amount of the loan before any payments are made
Indicate whether the statement is true or false
An extended partition holds _______ drives
Fill in the blank(s) with correct word