Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
} catch (NumberFormatException exc) {
showStatus("Invalid Data");
paymentText.setText("");
}
}

// Compute the loan payment.
double compute() {
double numer;
double denom;
double b, e;

numer = intRate * principal / payPerYear;

e = -(payPerYear * numYears);
b = (intRate / payPerYear) + 1.0;

denom = 1.0 - Math.pow(b, e);

return numer / denom;
}
}


The applet produced by this program is shown in Figure 32-1. To use the applet, simply
enter the loan principal, the length of the loan in years, and the interest rate. The payments
are assumed to be monthly. Once the information is entered, press Compute to calculate the
monthly payment.
The following sections examine the code toRegPayin detail. Because all the applets in
this chapter use the same basic framework, much of the explanation presented here also
applies to the other applets.


The RegPay Fields

RegPaybegins by declaring a number of instance variables that hold references to the text
fields into which the user will enter the loan information. Next, it declares thedoItvariable
that will hold a reference to the Compute button.


Chapter 32: Financial Applets and Servlets 935


FIGURE 32-1

TheRegPayapplet

Free download pdf