940 Part IV: Applying Java
Finding the Future Value of an Investment
Another popular financial calculation finds the future value of an investment given the
initial investment, the rate of return, the number of compounding periods per year, and the
number of years the investment is held. For example, you might want to know what your
retirement account will be worth in 12 years if it currently contains $98,000 and has an
average annual rate of return of 6 percent. TheFutValapplet developed here will supply
the answer.
To compute the future value, use the following formula:
Future Value =principal* ((rateOfRet/compPerYear) + 1)compPerYear*numYears
whererateOfRetspecifies the rate of return,principalcontains the initial value of the
investment,compPerYearspecifies the number of compounding periods per year, and
numYearsspecifies the length of the investment in years. If you use an annualized rate
of return forrateOfRet,then the number of compounding periods is 1.
The following applet calledFutValuses the preceding formula to compute the future
value of an investment. The applet produced by this program is shown in Figure 32-2.
Aside from the computational differences within thecompute( )method, the applet is
similar in operation to theRegPayapplet described in the preceding section.
// Compute the future value of an investment.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
/*
<applet code="FutVal" width=380 height=240>
</applet>
*/
public class FutVal extends JApplet
implements ActionListener {
JTextField amountText, futvalText, periodText,
rateText, compText;
JButton doIt;
FIGURE 32-2
TheFutValapplet