A bank wants to show its customers how much they would need to invest to achieve a specified financial goal (future value) in 5, 10, 15, 20, 25 or 30 years. Users must provide their financial goal (the amount of money desired after the specified number of years has elapsed), an interest rate and the length of the invest- ment in years. Create an application that calculates and displays the principal (initial amount to invest) needed to achieve the user’s financial goal. Your application should allow the user to invest money for 5, 10, 15, 20, 25 or 30 years. 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. 10.28.
a) Copying the template to your working directory. Copy the C:\Examples\Tutorial10\Exercises\PresentValue directory to your C:\SimplyJava directory.
b) Opening the template file. Open the PresentValue.java file in your text editor.
c) Customizing the JSpinner. You must customize the JSpinner to display the number of years. The name of this JSpinner is yearsJSpinner. This JSpinner should dis- play every multiple of 5 from 0 to 30, inclusive. Modify line 83 so that the initial value in the JSpinner is 0, the minimum value is 0, the maximum value is 30 and the step size is 5. In line 84, insert code to set the bounds property to 130, 95, 100, 20.
d) Customizing the JTextArea. You must customize the JTextArea that will display the various amounts needed on deposit. The name of this JTextArea is amountNeededJ- TextArea. Insert a blank line at line 94. On line 95, insert code to set the bounds property to 20, 155, 320, 115. On the next line, insert code to set amountNeeded- JTextArea’s editable property to false.
e) Retrieving input from a JSpinner. In line 133, access the JSpinner’s value property and use the ( Integer ) cast to convert the result to an object of type Integer. Store the result in Integer object integerObject. Insert a blank line at line 133. On line 134, retrieve the int data in integerObject, and store this data in int variable
years.
f) Completing a for statement header. In line 140 you will see a for statement header with only two semicolons. Before the first semicolon, declare and initialize variable counter to 5. Before the second semicolon, enter a loop-continuation condition that will cause the for statement to loop until counter has reached the number of years specified by the user. After the second semicolon, enter the increment of counter so that the for statement executes for every fifth year.
g) Calculating present values. You will now calculate the amount needed to achieve the future value for each five-year interval. To do this, you will need to implement the following formula within the for statement:
p = a / (1 + r) n
where
p is the amount needed to achieve the future value
r is the annual interest rate
n is the number of years
a is the future value amount (the amount the user would like to have after n years)
In lines 142–143, use the Math.pow method (as well as the variables defined for you in lines 129 and 131) to calculate the present value needed for the current number of years. Use two lines for clarity. In lines 144–145, use the append method to output the present value calculated in the application’s JTextArea. Use the DecimalFormat object (dollars) created for you in line 137. Use two lines for clarity.
h) Saving the application. Save your modified source code file.
i) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:\SimplyJava\PresentValue.
j) Compiling the application. Compile your application by typing javac PresentValue.java.
k) Running the completed application. When your application compiles correctly, run it by typing java PresentValue. Input a future value, interest rate and a number of years, then click the Calculate JButton. View the results to ensure that the correct number of years is displayed, and that the initial deposit results are correct.
l) Closing the application. Close your running application by clicking its close button. m) Closing the Command Prompt window. Close the Command Prompt window by clicking its close button.
```
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 javax.swing.*;
7 import java.text.*;
8
9 public class PresentValue extends JFrame
10 {
11 // JLabel and JTextField for future value
12 private JLabel futureValueJLabel;
13 private JTextField futureValueJTextField;
14
15 // JLabel and JTextField for interest rate
16 private JLabel interestRateJLabel;
17 private JTextField interestRateJTextField;
18
19 // JLabel and JSpinner for number of years
20 private JLabel yearsJLabel;
21 private JSpinner yearsJSpinner;
22
23 // JLabel and JTextArea for initial amounts
24 // needed to reach future value
25 private JLabel amountNeededJLabel;
26 private JTextArea amountNeededJTextArea;
27
28 // JButton calculates initial amounts needed
29 // to reach future value
30 private JButton calculateJButton;
31
32 // no-argument constructor
33 public PresentValue()
34 {
35 createUserInterface();
36 }
37
38 // create and position GUI components; register event handlers
39 private void createUserInterface()
40 {
41 // get content pane for attaching GUI components
42 Container contentPane = getContentPane();
43
44 // enable explicit positioning of GUI components
45 contentPane.setLayout( null );
46
47 // set up futureValueJLabel
48 futureValueJLabel = new JLabel();
49 futureValueJLabel.setBounds( 20, 25, 90, 20 );
50 futureValueJLabel.setText( "Future value:" );
51 contentPane.add( futureValueJLabel );
52
53 // set up futureValueJTextField
54 futureValueJTextField = new JTextField();
55 futureValueJTextField.setBounds( 130, 25, 100, 20 );
56 futureValueJTextField.setText( "0" );
57 futureValueJTextField.setHorizontalAlignment(
58 JTextField.RIGHT );
59 contentPane.add( futureValueJTextField );
60
61 // set up interestRateJLabel
62 interestRateJLabel = new JLabel();
63 interestRateJLabel.setBounds( 20, 60, 90, 20 );
64 interestRateJLabel.setText( "Interest rate:" );
65 contentPane.add( interestRateJLabel );
66
67 // set up interestRateJTextField
68 interestRateJTextField = new JTextField();
69 interestRateJTextField.setBounds( 130, 60, 100, 20 );
70 interestRateJTextField.setText( "0" );
71 interestRateJTextField.setHorizontalAlignment(
72 JTextField.RIGHT );
73 contentPane.add( interestRateJTextField );
74
75 // set up yearsJLabel
76 yearsJLabel = new JLabel();
77 yearsJLabel.setBounds( 20, 95, 80, 20 );
78 yearsJLabel.setText( "Years:" );
79 contentPane.add( yearsJLabel );
80
81 // set up yearsJSpinner
82 yearsJSpinner = new JSpinner(
83 new SpinnerNumberModel( 0, 0, 30, 5 ) );
84 yearsJSpinner.setBounds( 130, 95, 100, 20 );
85 contentPane.add( yearsJSpinner );
86
87 // set up amountNeededJLabel
88 amountNeededJLabel = new JLabel();
89 amountNeededJLabel.setBounds( 20, 130, 150, 20 );
90 amountNeededJLabel.setText( "Annual amount needed:" );
91 contentPane.add( amountNeededJLabel );
92
93 // set up amountNeededJTextArea
94 amountNeededJTextArea = new JTextArea();
95 amountNeededJTextArea.setBounds( 20, 155, 320, 115 );
96 amountNeededJTextArea.setEditable( false );
97 contentPane.add( amountNeededJTextArea );
98
99 // set up calculateJButton
100 calculateJButton = new JButton();
101 calculateJButton.setBounds( 250, 25, 90, 20 );
102 calculateJButton.setText( "Calculate" );
103 contentPane.add( calculateJButton );
104 calculateJButton.addActionListener(
105
106 new ActionListener() // anonymous inner class
107 {
108 // event handler called when calculateJButton is pressed
109 public void actionPerformed( ActionEvent event )
110 {
111 calculateJButtonActionPerformed( event );
112 }
113
114 } // end anonymous inner class
115
116 ); // end call to addActionListener
117
118 // set properties of application’s window
119 setTitle( "Present Value Calculator" ); // set title bar text
120 setSize( 370, 320 ); // set window size
121 setVisible( true ); // display window
122
123 } // end method createUserInterface
124
125 // calculate and display amounts
126 private void calculateJButtonActionPerformed( ActionEvent event )
127 {
128 int futureValue = Integer.parseInt(
129 futureValueJTextField.getText() );
130 double rate = Double.parseDouble(
131 interestRateJTextField.getText() );
132
133
134
135
Integer integerObject = ( Integer ) yearsJSpinner.getValue();
int years = integerObject.intValue();
136 amountNeededJTextArea.setText( "Year\tInitial Deposit" );
137 DecimalFormat dollars = new DecimalFormat( "$0.00" );
138
139 // for loop calculates total amount
140 for ( int counter = 5; counter <= years; counter += 5
141 {
142 double amount =
143 futureValue / Math.pow( ( 1 + rate / 100 ), counter );
144 amountNeededJTextArea.append(
145 "\n" + counter + "\t" + dollars.format( amount ) );
146
147 } // end for
148
149 } // end method calculateJButtonActionPerformed
150
151 // main method
152 public static void main( String[] args )
153 {
154 PresentValue application = new PresentValue();
155 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
156
157 } // end method main
158
159 } // end class PresentValue
```
You might also like to view...
In addition to the speed of the core processor, what is also critical to the performace of a CPU?
a. Cache speed b. Front side bus speed c. Prefetch capacity d. Number of cores
VGA mode includes what two resolutions for troubleshooting monitors?
A. 640 x 480 B. 800 x 600 C. 1280 x 720 D. 1920 x 1080