Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 32: Financial Applets and Servlets 955


String orgPStr = orgPText.getText();
String periodStr = periodText.getText();
String rateStr = rateText.getText();
String numWDStr = numWDText.getText();

try {
if(orgPStr.length() != 0 &&
periodStr.length() != 0 &&
rateStr.length() != 0 &&
numWDStr.length() != 0) {

principal = Double.parseDouble(orgPStr);
numYears = Double.parseDouble(periodStr);
rateOfRet = Double.parseDouble(rateStr) / 100;
numPerYear = Integer.parseInt(numWDStr);

result = compute();

maxWDText.setText(nf.format(result));
}

showStatus(""); // erase any previous error message
} catch (NumberFormatException exc) {
showStatus("Invalid Data");
maxWDText.setText("");
}
}

// Compute the maximum regular withdrawals.
double compute() {
double b, e;
double t1, t2;

t1 = rateOfRet / numPerYear;

b = (1 + t1);
e = numPerYear * numYears;

t2 = Math.pow(b, e) - 1;

return principal * (t1/t2 + t1);
}
}

Finding the Remaining Balance on a Loan


Often, you will want to know the remaining balance on a loan. This is easily calculated if you
know the original principal, the interest rate, the loan length, and the number of payments
made. To find the remaining balance, you must sum the payments, subtracting from each
payment the amount allocated to interest, and then subtract that result from the principal.
TheRemBalapplet, shown next, finds the remaining balance of a loan. The applet
produced by this program is shown in Figure 32-6.
Free download pdf