Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program calculates the mortgage payment for a loan with a principal
- * value of $200,000, 30 year term, and an annual percentage rate of 5.75%.
- * Author - Tom Rhoads
- */
- public class MortgageCalculator {
- public static void main(String args[]) {
- /*
- * this line declares the principalvalue of the loan
- */
- double principalValue;
- /*
- * this line declares the interestrate of the loan
- */
- double interestRate;
- /*
- * this line declares the annualpercentage rate of the loan
- */
- double apr;
- /*
- * this line declares the term of theloan in months
- */
- int term;
- /*
- * this line declares the monthlypayment of the loan
- */
- double monthlyPayment;
- /*
- * $200,000 is the principal value of the loan
- */
- principalValue = 240000;
- apr = 6.0; // 5.75% is the annual percentage rate
- interestRate = apr / 100 / 12; // Formula for interest rate by month
- term = 360; // 30 year loan is 360 months
- monthlyPayment = calculateMonthlyPayment(principalValue, interestRate,
- term);
- printAmortizationTable(monthlyPayment, term, principalValue,
- interestRate);
- }
- /*
- * Formula for calculating monthly payments on a mortgage
- */
- private static double calculateMonthlyPayment(double principalValue,
- double interestRate, int term) {
- return (principalValue * interestRate)
- / (1 - Math.pow(1 + interestRate, -term));
- }
- /**
- * Prints the amortization table.
- */
- private static void printAmortizationTable(double payment, int term,
- double principalBalance, double interestRate) {
- /** initialize the balances **/
- double loanBalance = term * payment;
- double interestBalance = loanBalance - principalBalance;
- double totalPrincipalPaid = 0;
- double totalInterestPaid = 0;
- /**
- * Prints the header of the table.
- */
- System.out.printf("%20s\t%25s\t%20s\n", "Balance",
- "Interest Paid This Month", "Total Interest Paid");
- /** step through all the payment periods */
- for (int paymentNumber = 1; paymentNumber <= term; paymentNumber++) {
- /**
- * The portion of the payment that goes to interest is based on the
- * remaining principal balance.
- **/
- double interestPaidThisMonth = interestRate * principalBalance;
- /**
- * The rest of the payment goes to principal.
- **/
- double principalPaidThisMonth = payment - interestPaidThisMonth;
- /**
- * Update the remaining balances and total paid values
- **/
- loanBalance = loanBalance - payment;
- principalBalance = principalBalance - principalPaidThisMonth;
- totalPrincipalPaid = totalPrincipalPaid + principalPaidThisMonth;
- interestBalance = interestBalance - interestPaidThisMonth;
- totalInterestPaid = totalInterestPaid + interestPaidThisMonth;
- /**
- * Prints a row of the table.
- */
- System.out.printf("%20.2f\t%25.2f\t%20.2f\n", principalBalance,
- interestPaidThisMonth, totalInterestPaid);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement