Solved

# Java Mortgage Amortization

Posted on 2006-07-17

I have completed this much of it but need the program somehow to only display the payments based upon a 5.75% rate but when I change it to only reflect a 30 year loan at 5.75%, it does not compile. This actually compiles but the payments are all jacked and I can't figure it out. I am trying to show the $200,000 balance and the first payment as well as interest paid and continue displaying this until the balance is zero. Please help ! Here's what I have now:

//import the java classes

import java.text.NumberFormat;

import java.util.Locale;

public class MortgageMulti {

//MortgageMulti is the existing function

public static void main(String[] arguments) {

//hard code the value of the mortgage

double mortgage = 200000.00;

//hard code in the yearly interst rate

double[][] yearsYearlyRate = {

{ 7, 15, 30 },

{ 5.35, 5.5, 5.75 }

};

//then code in the value for the number of times a year mulitplied by

// the amoutn paid each time

double[][] monthsMonthlyRt = {

{ yearsYearlyRate[0][0] * 12, yearsYearlyRate[0][1] * 12,

yearsYearlyRate[0][2] * 12

},

{ yearsYearlyRate[1][0] / 1200, yearsYearlyRate[1][1] / 1200,

yearsYearlyRate[1][2] / 1200

}

};

for (int i = 0; i < 3; i++) {

//Call the method

double payment = calPayment(monthsMonthlyRt[1][i], monthsMonthlyRt[0][i], mortgage);

// Display the monthly payment

System.out.println("\nMortgage Amount \tTerm \t\tRate \t\tMonthly Payment");

System.out.println("$200,000 \t\t" + yearsYearlyRate[0][i] + " years\t"

+ yearsYearlyRate[1][i] + "%\t\t" + payment);

System.out.println("\nMonth # \tInterest for the Month \t\tLoan Balance");

// Call the method

interestBalanceList(mortgage, monthsMonthlyRt[1][i], monthsMonthlyRt[0][i], payment);

}

}

// Utility method to calculate the monthly mortgage payment

// (1st error spotted)public static double calPayment(double param1, double

// param2, double mortgage);

public static double calPayment(double param1, double param2, double mortgage) {

// Calculate the monthlyPayment of the mortgage

double monthlyPayment = (param1 + param2 / (Math.pow((1 + param1), param2) - 1)) * mortgage;

return monthlyPayment;

}

// Utility method to list the interest paid and loan balance

public static void interestBalanceList(double mortgage, double param1, double param2,

double payment) {

// Use the for loop to display the list

for (int i = 1; i < (param2 + 1); i++) {

// Pause the list and go to the next month and next amount

System.out.println("Hit the 'ENTER' key to exit.");

try {

System.in.read();

} catch (Exception e) {

}

// Calculate the monthlyInterest

double monthlyInterest = mortgage * param1;

// Calculate the loanBlance

double loanRemaining = mortgage - (payment - monthlyInterest);

// Set the value of mortgageAmount to the value of loanRemaining

mortgage = loanRemaining;

// Formate the monthlyInterest into U.S. currency

NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);

String str = n.format(monthlyInterest);

// Formate the loanRemaining into U.S. currency

NumberFormat num = NumberFormat.getCurrencyInstance(Locale.US);

String string = num.format(loanRemaining);

// Display the month #, interest paid and loan balance

System.out.println("\n " + i + "\t\t " + str + "\t\t\t" + string);

}

}

}