Learning Java
Custom Search

Previous Next

Part B - Mortgage Calculator

Now that we have the graphical interface of our mortgage calculator, we will add what is necessary to calculate our mortgage.

We will add code to make our calculate button do something. Our class has to implement the ActionListener. Then we have to add the ActionListener to our button before we add the button to the JPanel. Then we have to implement the method actonPerformed where we will tell our application how to calculate the mortgage. Let's not forget to import the package java.awt.event.* that contains classes for handling events. The structure of this additional code is:

...
import java.awt.event.*;

public class MortgCalc2 extends JFrame implements ActionListener {

    public MortgCalc2() {
	....
        btnCalc.addActionListener(this);
        pane.add(btnCalc);
	...
    }

    public static void main(String[] arguments) {
	MortgCalc2 runCalc = new MortgCalc2();
    }

    public void actionPerformed(ActionEvent evt) {
	...
    }
}

We changed the name of our class. If you do the same, be sure to change all occurrences or you may not get the expected results. I've hi-lighted in red the occurrences.

Now let's add our formula which we will put in a comment between /** and */ at the top for anyone who wants to understand what's in our actionPerformed. To use mathematical formulas, we'll need to import java.lang.Math.*. We need to import java.text.* in order to use the class DecimalFormat for formatting out payment amount.

Now let's code the method actionPerformed. Our numbers have decimal points, so we will use the double data type for our numbers. We use the class DecimalFormat to format the payment amount. The format #,##0.00 tells java that we want 2 decimal places after the decimal point and we want it comma delimited.

/**
Mortgage Payments
 
p  is the principal 
a  is the payment per month 
r  is the interest rate per month 
n  is the number of months 
ln  is the natural log function 
*  is multiply 

Repayment amount for a set principal over a set number of payments
 
a = ( p * r * ( 1 + r )**n ) / ( ( 1 + r)**n - 1 )
 or
a =   p * r / ( 1 - ( 1 / (( 1 + r)**n) ) )
*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.text.*;
import java.util.*;

public class MortgCalc2 extends JFrame implements ActionListener {
    JLabel lblPrinc = new JLabel("Mortgage Principal...");
    JTextField txtPrinc = new JTextField("255000", 7);
    JLabel lblTerm = new JLabel("Years..........................");
    JTextField txtTerm = new JTextField("30", 7);
    JLabel lblRate = new JLabel("Interest Rate.............");
    JTextField txtRate = new JTextField("6.00", 7);
    JLabel lblPayment = new JLabel("Payment....................");
    JTextField txtPayAns = new JTextField("0.00", 7);
    JButton btnCalc = new JButton("Calculate");

    public MortgCalc2() {
	super("Mortgage Calculator");
	setSize(225, 180);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        pane.add(lblPrinc);
        pane.add(txtPrinc);
        pane.add(lblTerm);
        pane.add(txtTerm);
        pane.add(lblRate);
        pane.add(txtRate);
        pane.add(lblPayment);
        pane.add(txtPayAns);
        btnCalc.addActionListener(this);
        pane.add(btnCalc);
        setContentPane(pane);
	setVisible(true);
    }

    public static void main(String[] arguments) {
	MortgCalc2 runCalc = new MortgCalc2();
    }

    public void actionPerformed(ActionEvent evt) {
	Double x, y, Payment;
	Double Princ = Double.parseDouble(txtPrinc.getText());
	Double Term = Double.parseDouble(txtTerm.getText());
	Double Rate = Double.parseDouble(txtRate.getText());
	Rate = Rate / 1200d;
	y = 1.0d + Rate;
	Term = Term * 12;
	x = Math.pow(y, Term);
	Payment = Princ * (Rate / (1.0d - (1.0d / x)));

	DecimalFormat numFormatter = new DecimalFormat("#,##0.00");
	String PayFmt = numFormatter.format(Payment);

	txtPayAns.setText(PayFmt);
    }
}

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved