I need help calculating my mortgage calculator

Gday31
Gday31 used Ask the Experts™
on
My calcuations are off they should be as follows
                      Term:  7 year(s)  Interest Rate:   5.35%  Monthly Payment: $2859.79
       Term: 15 year(s)  Interest Rate:   5.50%  Monthly Payment: $1634.17
       Term: 30 year(s)  Interest Rate:   5.75%  Monthly Payment: $1167.15
import java.awt.BorderLayout;
 
import java.awt.Color;
 
import java.awt.Container;
 
import java.awt.FlowLayout;
 
import java.awt.event.ActionEvent;
 
import java.awt.event.ActionListener;
 
 
 
import javax.swing.JButton;
 
import javax.swing.JCheckBox;
 
import javax.swing.JFrame;
 
import javax.swing.JPanel;
 
import javax.swing.JTextArea;
 
import javax.swing.JTextField;
 
 
 
public class MortgageCalculatorGUI2 extends JFrame implements ActionListener {
 
 
 
public MortgageCalculatorGUI2()
 
   {
 
	 Container content = this.getContentPane();
 
    content.setBackground(Color.blue);
 
    content.setLayout(new FlowLayout());
 
    content.add(new JButton("Loan Amount"));
 
    content.add(jTextField1=new JTextField("                    "));  /// ** added jTextField1
 
    content.add(new JButton("Interest Rate"));
 
    content.add(jTextField2=new JTextField("                    "));
 
    content.add(new JButton("Term in Years"));
 
    content.add(jTextField3=new JTextField("                    "));
 
    content.add(new JButton("Montly Payment"));
 
    content.add(jTextField4=new JTextField("                    "));
 
    }
 
	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt){                                            
       
 
    }  
 
                                 
 
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)  {                                        
 
        System.exit(0);
 
    }                  
 
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)  {                                        
 
        jTextField1.setText("");
 
        jTextField2.setText("");
 
        jTextField3.setText("");
 
    }                                      
 
 
 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
 
     
 
    float num1, num2, num3, result;
 
/* This is used to convert variables in formulas.
 
  // num1 = Amount of loan
 
  // num2 = Interest of Loan
 
  // num3 = Term in years  */
 
 
 
    num1 = Float.parseFloat(jTextField1.getText());
 
    num2 = Float.parseFloat(jTextField2.getText());
 
    num3 = Float.parseFloat(jTextField3.getText());
 
   
/*  PMT calculation
    PMT=((PV*(IR/12))/(1 - Math.pow(1 + (IR/12), -NP)));
    MT = Result 
 
    nt PV = 200000; //Principle Loan Amount
    inal double IR = 0.0575; //Interest Rate, by Month
    nt NP = 360; //Length of loan in months
    inal double PMT; */
 
/*  Converted variables from the instructors formula, still get the same result.
    result=((num1*num2/12))/(1-(float)Math.pow(1 + (num2/12),-(num3*12)));
    working formula result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12)))); */    
 
//Original Formula   result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));
 
 
/*  System.out.println("ccc " + String.valueOf(result));
 
    jTextField4.setText(String.valueOf(result));*/
 
 
 
    }                                      
 
 
 
    public static void main(String args[]) {
 
     
 
// JFrame f = new JFrame("MortgageCalculatorGUI2 "); Failed line to add title.
 
       
 
             MortgageCalculatorGUI2 f = new MortgageCalculatorGUI2();
 
             f.setTitle("MortgageCalculatorGUI2"); 
				 
 /*puts the title in the frame title*/
                  
                  
                  
                f.setSize(300, 250);
 
                f.setVisible(true);
 
     }
 
       
 // Variables declaration - do not modify                    
 
    private javax.swing.JButton jButton1;
 
    private javax.swing.JButton jButton2;
 
    private javax.swing.JButton jButton3;
 
    private javax.swing.JLabel jLabel1;
 
    private javax.swing.JLabel jLabel2;
 
    private javax.swing.JLabel jLabel3;
 
    private javax.swing.JPanel jPanel1;
 
    private javax.swing.JTextField jTextField1;
 
    private javax.swing.JTextField jTextField2;
 
    private javax.swing.JTextField jTextField3;
 
                      
    private javax.swing.JTextField jTextField4;
 
// End of variables declaration
 
   JButton myButton = new JButton("Calculate");
 
   
 
   JCheckBox myCheckBox = new JCheckBox("Reset Fields");
 
   
 
 // Clear all fields in Jbuttons
 
 
 
   JTextArea myText = new JTextArea("Please enter the fields and click calculate");
 
 
 
   JPanel Panel = new JPanel();
 
 
 
   JPanel holdAll = new JPanel();
 
  {
 
    Panel.setLayout(new FlowLayout());
 
    Panel.add(myCheckBox);
 
    Panel.add(myButton);
 
 
 
    holdAll.setLayout(new BorderLayout());
 
    holdAll.add(Panel, BorderLayout.NORTH);
 
    holdAll.add(myText, BorderLayout.CENTER);
 
 
 
    getContentPane().add(holdAll, BorderLayout.CENTER);
 
 
 
    myButton.addActionListener(this);
 
    myCheckBox.addActionListener(this);
 
 
 
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 
  }
 
 
 
  public void actionPerformed(ActionEvent e)
 
  {
    System.out.println("actionPerformed");
     float num1, num2, num3, result;
	  
           if(e.getActionCommand().equals("Calculate"))//begin calculations when Calculate pressed
            {
             String enteredconvertedAmount = fieldconvertedAmount.getText();
             int[] convertedTerm; 
             convertedTerm = new int[3];
             convertedTerm[0] = 7;
             convertedTerm[1] = 15;
             convertedTerm[2] = 30;
             double[] convertedRate;
             convertedRate = new double[3];
             convertedRate[0] = 5.35;
             convertedRate[1] = 5.50;
             convertedRate[2] = 5.75;
            
  
 
 
  // num1 = Amount of loan
 
  // num2 = Interest of Loan
 
  // num3 = Term in years
 
 
 
    num1 = Float.parseFloat(jTextField1.getText());
 
    num2 = Float.parseFloat(jTextField2.getText());
 
    num3 = Float.parseFloat(jTextField3.getText());
 
   
 
 
    result=(num1*((num2/12)/1-(float)Math.pow((1+(num1/12)),-(num3*12))));
 
 
//  result = (num1 * num2)/(1 - (float) Math.pow(1/ (1 + num2), num3 * 12));
 
   System.out.println("ccc " + String.valueOf(result));
 
    jTextField4.setText(String.valueOf(result));
 
   if (e.getSource() == myButton)
 
      myText.setText("Thank you");
 
 
 
    else if (e.getSource() == myCheckBox)
 
      myText.setText("Fields Cleared" +
 
                     myCheckBox.isSelected());
 
    else
 
      myText.setText("E ...?");
 
  }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Officer
Most Valuable Expert 2011
Commented:
Gday31,

The formula (which appears to be what you are using) is as follows:
M = P * ( J / (1 - (1 + J) ** -N))
(http://www.hughchou.org/calc/formula.html)

Think where you are off is that the N should be expressed in months.  Which if you get the loan term is years is T * 12; however, since you already have in months, I would suspect multiplying by 12 is your issue here.

I would expect the formula to be translated as follows in Java.

Regards,
mwvisa1
double P = 200000.00; //Principle Loan Amount
double J = 0.0575/12; //Interest Rate, by Month
int N = 360; //Length of loan in months
// if J is annual interest, then divide by 12
//   replace J with (J/12)
// if N is number of years, then multiply by 12
//   replace N with (N*12)
double M = P * (J / (1 - Math.pow((1+J), -N)));
System.out.println(M);

Open in new window

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
It is past the deadline now, but the code I provided works for calculating the payments exactly as needed per question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial