Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

952 Part IV: Applying Java


$500,000 in a retirement account, how much can you take out each month for 20 years, assuming
a 6 percent rate of return? The formula that computes the maximum withdrawal is shown here:
Maximum Withdrawal =principal* ( ( (rateOfRet/wdPerYear) /
(–1 + ((rateOfRet/wdPerYear) + 1)wdPerYear*numYears) ) +
(rateOfRet/wdPerYear) )

whererateOfRetspecifies the rate of return,principalcontains the value of the initial
investment,wdPerYearspecifies the number of withdrawals per year, andnumYears
specifies the length of the annuity in years.
TheMaxWDapplet shown next computes the maximum periodic withdrawals that can
be made over a specified length of time for an assumed rate of return. The applet produced
by this program is shown in Figure 32-5.

/* Compute the maximum annuity that can
be withdrawn from an investment over
a period of time. */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
/*
<applet code="MaxWD" width=340 height=260>
</applet>
*/

public class MaxWD extends JApplet
implements ActionListener {

JTextField maxWDText, orgPText, periodText,
rateText, numWDText;
JButton doIt;

double principal; // initial principal
double rateOfRet; // annual rate of return
double numYears; // length of time in years
int numPerYear; // number of withdrawals per year

NumberFormat nf;

FIGURE 32-5

TheMaxWD
applet
Free download pdf