import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.NumberFormatException; import java.util.Vector; /** Created on June 27, 2004, 5:07 PM * Java Programming Project Workshop 3 – * Problem statement – Create a Mortgage Calculator WITHOUT a graphical user interface * Purpose – provide a user-friendly service to potential users for accurately planning mortgage and other types of loans * Scope – The mortgage calculator must display the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. * This program must have hard code amount = 200,000, term= 30 years, and the interest rate = 5.75% in class driver.java * Algorithm – standard loan calculation with fixed number of payments: * Divide the yearly interest rate of 5.75% by 12 to get monthly ineterst rate * Multiply the number of years by 12 to get the number of months * Formula for monthly payment: monthlyPayment = [P((1+r)^n)*r]/[(1+r^n)-1], where r and n are calculated as above. * * Inputs: * Principal = $200,000 * Interest Rate = 0.0575 * Periods = 360 * * Output: * Monthly Payment = * Pay Date: 15JUL05 * @ * NETBEANS IDE 3.6*/ public class Mortgagecalculator{ //4. Public void derive matrix //5. Public void print matrix /** Creates a static mortgagecalculator */ public static int years;//define variables public double loanAmount; public double interestRate; public double monthlyPayment; private static int numberOfMonths, numberOfDays; Vector firstRow = new Vector(); public void setYears(int inYears)//setter { years=inYears; }//end set years public static int getYears()//getter { return years; }//end get years public void setLoanAmount(double inLoanAmount)//setter { loanAmount=inLoanAmount; }//end set loanAmount public double getLoanAmount()//getter { return loanAmount; }//end get loanAmount public void setInterestRate(double inInterestRate)//setter { interestRate=inInterestRate; }//end set interestRate public double getInterestRate()//getter { return interestRate; }//end get interestrate public void CalcMonthlyPayment() { numberOfMonths = years*12; numberOfDays = years*365; monthlyPayment=loanAmount*Math.pow(1 + interestRate/12, numberOfMonths) * (interestRate/12)/(Math.pow(1 + interestRate/12, numberOfMonths) -1); monthlyPayment = Math.round(monthlyPayment*100.00/100.00); }//Divide the yearly interest rate of 5.75% by 12 to get monthly ineterst rate // Multiply the number of years by 12 to get the number of months // Formula for monthly payment: monthlyPayment = [P((1+r)^n)*r]/[(1+r^n)-1], where r and n are calculated as above. public String toString(){//strings are used to store text and variables return "Mortgage Information" + "\n"+ "Loan Amount: $" + loanAmount + "\n" + "Interest Rate: " + interestRate + "%" + "\n" + "Term: " + (years*12) + " months" + "\n" + "Monthly Payment: $" + (Math.round(monthlyPayment*100.00)/100.00);//Using the round to two decimal places } public void deriveMatrix(){ int counter = 1; double Balance, Interest_Payment, Interest_Paid, Principle_Paid; String payDate = "15JUL05"; while(counter <= numberOfMonths){ //Payment# is the month number //Principle is constant at $200,000 //Monthly Payment is constant at $6557.57 //Pay Date is fixed at 15JUL05 //Interest Payment = loanAmount*interestRate/12; //Interest Paid = Interest Payment //Principle Paid = Principle - Balance Balance = Math.round((loanAmount*(1+interestRate/12) - monthlyPayment)*100.00/100.00); //Calculation of Balance Interest_Payment = Math.round((loanAmount*interestRate/12)*100.00/100.00); //Calculation of Interest Interest_Paid = Interest_Payment; Principle_Paid = loanAmount - Balance; Row rows = new Row(counter, loanAmount, monthlyPayment, payDate, Interest_Payment, Interest_Paid, Principle_Paid); firstRow.addElement(rows); counter++; loanAmount = Balance; }//end of while(counter <= numberOfMonths) } public void printMatrix(){//print schedule and scroll System.out.println(); System.out.println("Payment# Principle MonthlyPayment PayDate IntPay IntPaid PrinPaid"); int scrollCounter, counter = 0; while (counter < numberOfMonths){ scrollCounter = 0; while(scrollCounter<30){ System.out.println("" + (counter+1) + "\t" +firstRow.elementAt(counter)); counter++; scrollCounter++; }//end of while(scrollCounter<30) System.out.println("Press Enter to continue.."); BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1); String str = ""; try { str = br.readLine(); } catch (IOException ioe) { System.out.println(ioe);} }//end of while (counter <= numberOfMonths) } }//closing bracket