S O U R C E C O D E
/ 1999 / 02
/ java
/ java.lst
"Java@Work: Old Places, Old Faces"
by Al Williams
Web Techniques, January 1999
Web Techniques grants permission to use these listings (and code) for
private or commercial use provided that credit to Web Techniques and
the author is maintained within the comments of the source. For
questions, contact editors@web-techniques.com.
[LISTING ONE]
//Title: Mortgage Calculator
//Version:
//Copyright: Copyright (c) 1998
//Author: Al Williams
//Company: AWC
//Description: This is a mortgage calculator applet
//created with JBuilder
package mcalc;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.sun.java.swing.*;
import java.text.NumberFormat;
import java.text.ParseException;
import borland.jbcl.layout.*;
public class Mcalc extends JApplet {
boolean isStandalone = false;
TextField Principal = new TextField();
Label label1 = new Label();
XYLayout xYLayout1 = new XYLayout();
TextField Rate = new TextField();
Label label2 = new Label();
TextField Months = new TextField();
Label label3 = new Label();
Button Calc = new Button();
Label Payment = new Label();
Label Total = new Label();
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Mcalc() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
label1.setText("Principal:");
xYLayout1.setHeight(300);
xYLayout1.setWidth(313);
label2.setText("Interest Rate:");
label3.setText("Months:");
Calc.setLabel("Calculate");
Calc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
Calc_actionPerformed(e);
}
});
this.setSize(400,300);
this.getContentPane().add(Principal,
new XYConstraints(103, 16, 183, -1));
this.getContentPane().add(label1,
new XYConstraints(9, 16, -1, -1));
this.getContentPane().add(Rate,
new XYConstraints(104, 58, 183, -1));
this.getContentPane().add(label2,
new XYConstraints(9, 60, -1, -1));
this.getContentPane().add(Months,
new XYConstraints(106, 102, 183, -1));
this.getContentPane().add(label3,
new XYConstraints(11, 104, -1, -1));
this.getContentPane().add(Calc,
new XYConstraints(121, 158, 80, 34));
this.getContentPane().add(Payment,
new XYConstraints(18, 214, 142, -1));
this.getContentPane().add(Total,
new XYConstraints(19, 240, 142, -1));
}
//Start the applet
public void start() {
}
//Stop the applet
public void stop() {
}
//Destroy the applet
public void destroy() {
}
//Get Applet information
public String getAppletInfo() {
return "Mortgage Calculator by Al Williams";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
Mcalc applet = new Mcalc();
applet.isStandalone = true;
JFrame frame = new JFrame();
frame.setTitle("Mortgage Calculator");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) /
2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
void Calc_actionPerformed(ActionEvent e) {
float intrate, amt;
int period;
float intpermonth;
float pmt,total;
double factor;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
NumberFormat gfmt = NumberFormat.getInstance();
// read inputs
intrate= 0.0f;
try
{
intrate=gfmt.parse(Rate.getText()).floatValue()/100.0f;
}
catch (ParseException ex)
{
Payment.setText("Bad period");
return;
}
period=Integer.parseInt(Months.getText());
amt=0.0f;
try
{
amt=gfmt.parse(Principal.getText()).floatValue();
}
catch (ParseException ex)
{
Payment.setText("Bad amount");
return;
}
// calculate
intpermonth=intrate/12;
factor=Math.exp(-period* Math.log(1.0+intpermonth));
pmt=(float)(amt*intpermonth/(1.0-factor));
total=pmt*period;
// set outputs -- use NumberFormat to format numbers
Payment.setText("Payment = " + fmt.format(pmt));
Total.setText("Total = " + fmt.format(total));
}
}
|
HOME
|
CURRENT ISSUE
|
ARCHIVES
|
SOURCE CODE
|
EVENTS
|
BUYER'S GUIDE
|
CUSTOMER SERVICE
|
ADVERTISING
|
AUTHOR GUIDELINES
|
EDITORIAL CALENDAR
|
SEARCH
|
ABOUT
Entire contents copyright 1996-2000 Miller Freeman, Inc.
Read our privacy policy.