948 Part IV: Applying Java
whererateOfRetspecifies the rate of return,regWDcontains the desired regular withdrawal,
wdPerYearspecifies the number of withdrawals per year, andnumYearsspecifies the length
of the annuity in years.
TheAnnuityapplet shown here computes the initial investment required to produce
the desired annuity. The applet produced by this program is shown in Figure 32-4.
/* Compute the initial investment necessary for
a desired annuity. In other words, it finds
the initial amount needed to allow the regular
withdrawals of a desired amount over a period
of time. */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
/*
<applet code="Annuity" width=340 height=260>
</applet>
*/
public class Annuity extends JApplet
implements ActionListener {
JTextField regWDText, initialText, periodText,
rateText, numWDText;
JButton doIt;
double regWDAmount; // amount of each withdrawal
double rateOfRet; // rate of return
double numYears; // length of time in years
int numPerYear; // number of withdrawals per year
NumberFormat nf;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
FIGURE 32-4
TheAnnuityapplet