1 Replies - 710 Views - Last Post: 26 February 2012 - 07:23 PM Rate Topic: -----

#1 Zilna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 08-October 10

Java mortgage calculator

Posted 26 February 2012 - 06:20 PM

I am being honest, yes this is for homework and I am ready to bang my head around a little. I got my program to work but the problem is that as the amortization schedule loops around it is actually adding about a dollar and change to the next month's payment; the payment should actually be staying the same throughout the entire program.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package changerequest1;

import java.text.DecimalFormat;

/**
 *
 * @author Diane
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       //create mortgage calculator
        int years = 30; //define length of time variable
        double mortgageAmount = 200000; //define total loan amount
        int payments = years * 12; //define total payments over 30 years
        double interestRate = 0.0575/12; //define the interenst rate of the loan

        DecimalFormat decFor = new DecimalFormat("0.00"); //define decimal format

        //loop to amortize loan
        for (double i = payments; i>0;i--){
             double monthlyPayment = (mortgageAmount * interestRate) / (1 - Math.pow(1 + interestRate,-payments));

             double interest = mortgageAmount * interestRate;
             double payment = monthlyPayment - interest;
             double endingBalance = mortgageAmount - payment;




                     //print lines to display mortgage information
            System.out.println ("Month " + (payments) + " Payment: " + decFor.format(monthlyPayment)); //displays month number and payment made
            System.out.println ("Interest Paid: " + decFor.format(interest)); //displays interest paid
            System.out.println ("Remaining Balance: " + decFor.format(endingBalance) + "\n\n"); //displays remaining balance

            //loop for hesitation
            if (payments > 0){
                payments--;
                try{
                    Thread.sleep(1500); //pause 1.5 seconds
                }//end try
                catch (InterruptedException e) {
                    //ignore or print the exception
                }//end catch


        }//end if
        }//end for
   
    }//end args

}//end program




Could someone please point me into the direction as to why the program is increasing the monthly payment please? Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Java mortgage calculator

#2 lbfb   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 23-February 12

Re: Java mortgage calculator

Posted 26 February 2012 - 07:23 PM

The reason the monthly payment changes is because you have changed the formula each time you run through the loop.

(1 - Math.pow(1 + interestRate, payments) - but at the end of the for loop you have reduced the 'payments' figure each time. I am only learning Java myself so I don't know why you have that 'if' loop at the end. Is it for the delay? If it is, could you not use the for loop index 'i' instead as this will be reducing anyway as part of the for loop? And then 'payments' would remain constant and the monthly figure would be the same. (I can't comment on the formula itself as it means nothing to me).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1