Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

944 Part IV: Applying Java


The formula to compute an initial investment is shown here:

Initial Investment =targetValue/ (((rateOfRet/compPerYear) + 1)compPerYear*numYears)

whererateOfRetspecifies the rate of return,targetValuecontains the starting balance,compPerYear
specifies the number of compounding periods per year, andnumYearsspecifies 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 calledInitInvuses the preceding formula to compute the initial
investment required to reach a desired future value. The applet produced by this program
is shown in Figure 32-3.

/* Compute the initial investment necessary for
a specified future value. */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
/*
<applet code="InitInv" width=340 height=240>
</applet>
*/

public class InitInv extends JApplet
implements ActionListener {

JTextField targetText, initialText, periodText,
rateText, compText;
JButton doIt;

double targetValue; // original targetValue
double rateOfRet; // rate of return
double numYears; // length of loan in years
int compPerYear; // number of compoundings per year

NumberFormat nf;

FIGURE 32-3

TheInitInvapplet
Free download pdf