import java.awt.event.*;
import java.applet.*;
import java.awt.*;
public class MortgageApp extends Applet implements
ActionListener
{ TextField balField;
TextField intField;
TextField nyrField;
Button OK;
TextArea msgArea;
// Convert double to dollars and cents
static String format(double dollars)
{
String numString = Double.toString(dollars);
int dotpos = numString.indexOf('.');
if (dotpos < 0) // Check if whole number
return numString;
// Check for excess fraction digits
else if (dotpos < numString.length() - 2)
return numString.substring(0, dotpos + 3); //
'.'+ 2 digits
else return numString + "0"; // Assume only 1
fraction digit
}
public void init()
{ balField = new TextField("", 15);
intField = new TextField("", 5);
nyrField = new TextField("", 5);
OK = new Button("Compute");
msgArea = new TextArea("", 15, 60);
msgArea.setEditable(false);
add(new Label("Enter principal"));
add(balField);
add(new Label("Enter annual interest rate"));
add(intField);
add(new Label("Enter number of years"));
add(nyrField);
add(OK);
OK.addActionListener(this);
add(msgArea);
}
public void actionPerformed(ActionEvent evt)
{
this.update();
}
void update()
{ String balString = balField.getText();
String intString = intField.getText();
String nyrString = nyrField.getText();
if (balString.trim().length() == 0)
msgArea.setText("Principal amount
missing");
else if (intString.trim().length() == 0)
msgArea.setText("Interest rate missing");
else if (nyrString.trim().length() == 0)
msgArea.setText("Number of years
missing");
else {
double bal = new
Double(balString).doubleValue();
double intyr = new
Double(intString).doubleValue() / 100.;
short nyears = (short) new
Integer(nyrString).intValue();
StringBuffer msg = new StringBuffer();
msg.append("\nprincipal=" + bal + "
interest=" + intyr
+ " years=" + nyears + "\n");
double intmo = intyr / 12.;
int npmts = nyears * 12;
double pmt = bal * (intmo / (1.-
Math.pow(1.+ intmo,-npmts)));
msg.append("payment\ttotal\tinterest\tprincipal\tbalance\n");
msg.append("number\tpayment\tpayment\tpayment\n");
msg.append("\t\t\t\t" + bal + "\n");
for (short mo = 1; mo <= npmts; ++mo) {
double prinpmt, intpmt = bal * intmo;
if (mo < npmts)
prinpmt = pmt - intpmt;
else prinpmt = bal;
bal -= prinpmt;
msg.append(mo + "\t" + format(intpmt +
prinpmt)
+ "\t" + format(intpmt)
+ "\t" + format(prinpmt)
+ "\t" + format(bal) + "\n");
}
msgArea.setText(msg.toString());
}
}
}
__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site
http://webhosting.yahoo.com
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________