This program will return the monthly amortized, compounded monthly mortgage payment given Amount, Term, and Interest Rate. For this program, these items are hard coded.
//Import statements import java.math.*; import java.text.DecimalFormat; import java.io.*; //Class declaration public class MortgageWeek4f {// open class variables // Class Variables // declaring and initializing the arrays double[] interestRate = { 5.35, 5.5, 5.75 }; // array for the interest rate double[] monthlyPayment = new double[3]; // array for the monthly payment // amount. computed below int[] loanLength = { 84, 180, 360 }; // array for the loan length // variable declarations and initializations double loanAmount = 200000; double discount = 0; // decimal format used to format the number output DecimalFormat decimalPlaces = new DecimalFormat("$#,##0.00"); {// Program opening brace // *******************formula for the monthlyPayment amount // variable******************************** for (int i = 0; i < 3; i++) // this formula will increment the arrays // and use each one sequentially { // open for loop discount = Math.pow((1 + ((interestRate[i] / 100) / 12)), loanLength[i]) / (Math.pow((1 + ((interestRate[i] / 100) / 12)), loanLength[i]) - 1); monthlyPayment[i] = loanAmount * ((interestRate[i] / 100) / 12) * discount; } // close for loop // initial output statements System.out.println("\t Mortgage Calculator"); System.out.println("\t by Larry Marcus"); System.out.print("\n\nOriginal Amount \t=" + decimalPlaces.format(loanAmount) + "\n\n\n"); // Prints out a nice formatting line System.out .print("********************************************************************************"); // output statements for results of the formula above System.out.println("Interest Rate (APR)\t=\t" + interestRate[0] + "%\t\t" + interestRate[1] + "%\t\t" + interestRate[2] + "%"); System.out.println("Term of loan \t=\t" + loanLength[0] / 12 + " years\t\t" + loanLength[1] / 12 + " years\t" + loanLength[2] / 12 + " years"); System.out.print("Monthly Payment \t=\t" + decimalPlaces.format(monthlyPayment[0]) + "\t" + decimalPlaces.format(monthlyPayment[1]) + "\t" + decimalPlaces.format(monthlyPayment[2]) + "\n"); // Prints out a nice formatting line System.out .print("********************************************************************************"); System.out.println("\n\n\n"); }// program closing brace }