Chapter 32: Financial Applets and Servlets 951
fields are completed. */
public void actionPerformed(ActionEvent ae) {
double result = 0.0;String regWDStr = regWDText.getText();
String periodStr = periodText.getText();
String rateStr = rateText.getText();
String numWDStr = numWDText.getText();try {
if(regWDStr.length() != 0 &&
periodStr.length() != 0 &&
rateStr.length() != 0 &&
numWDStr.length() != 0) {regWDAmount = Double.parseDouble(regWDStr);
numYears = Double.parseDouble(periodStr);
rateOfRet = Double.parseDouble(rateStr) / 100;
numPerYear = Integer.parseInt(numWDStr);result = compute();initialText.setText(nf.format(result));
}showStatus(""); // erase any previous error message
} catch (NumberFormatException exc) {
showStatus("Invalid Data");
initialText.setText("");
}
}// Compute the required initial investment.
double compute() {
double b, e;
double t1, t2;t1 = (regWDAmount * numPerYear) / rateOfRet;b = (1 + rateOfRet/numPerYear);
e = numPerYear * numYears;t2 = 1 - (1 / Math.pow(b, e));return t1 * t2;
}
}Finding the Maximum Annuity for a Given Investment
Another annuity calculation computes the maximum annuity (in terms of a regular withdrawal)
available from a given investment over a specified period of time. For example, if you have