Advertisement
MarufA

Untitled

Nov 11th, 2013
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. /**
  2.  * This program calculates the mortgage payment for a loan with a principal
  3.  * value of $200,000, 30 year term, and an annual percentage rate of 5.75%.
  4.  * Author - Tom Rhoads
  5.  */
  6.  
  7. public class MortgageCalculator {
  8.     public static void main(String args[]) {
  9.         /*
  10.          * this line declares the principalvalue of the loan
  11.          */
  12.         double principalValue;
  13.  
  14.         /*
  15.          * this line declares the interestrate of the loan
  16.          */
  17.         double interestRate;
  18.  
  19.         /*
  20.          * this line declares the annualpercentage rate of the loan
  21.          */
  22.         double apr;
  23.  
  24.         /*
  25.          * this line declares the term of theloan in months
  26.          */
  27.         int term;
  28.  
  29.         /*
  30.          * this line declares the monthlypayment of the loan
  31.          */
  32.         double monthlyPayment;
  33.  
  34.         /*
  35.          * $200,000 is the principal value of the loan
  36.          */
  37.         principalValue = 240000;
  38.  
  39.         apr = 6.0; // 5.75% is the annual percentage rate
  40.         interestRate = apr / 100 / 12; // Formula for interest rate by month
  41.         term = 360; // 30 year loan is 360 months
  42.  
  43.         monthlyPayment = calculateMonthlyPayment(principalValue, interestRate,
  44.                 term);
  45.  
  46.         printAmortizationTable(monthlyPayment, term, principalValue,
  47.                 interestRate);
  48.  
  49.     }
  50.  
  51.     /*
  52.      * Formula for calculating monthly payments on a mortgage
  53.      */
  54.     private static double calculateMonthlyPayment(double principalValue,
  55.             double interestRate, int term) {
  56.  
  57.         return (principalValue * interestRate)
  58.                 / (1 - Math.pow(1 + interestRate, -term));
  59.     }
  60.  
  61.     /**
  62.      * Prints the amortization table.
  63.      */
  64.     private static void printAmortizationTable(double payment, int term,
  65.             double principalBalance, double interestRate) {
  66.  
  67.         /** initialize the balances **/
  68.         double loanBalance = term * payment;
  69.         double interestBalance = loanBalance - principalBalance;
  70.         double totalPrincipalPaid = 0;
  71.         double totalInterestPaid = 0;
  72.  
  73.         /**
  74.          * Prints the header of the table.
  75.          */
  76.         System.out.printf("%20s\t%25s\t%20s\n", "Balance",
  77.                 "Interest Paid This Month", "Total Interest Paid");
  78.  
  79.         /** step through all the payment periods */
  80.         for (int paymentNumber = 1; paymentNumber <= term; paymentNumber++) {
  81.             /**
  82.              * The portion of the payment that goes to interest is based on the
  83.              * remaining principal balance.
  84.              **/
  85.             double interestPaidThisMonth = interestRate * principalBalance;
  86.  
  87.             /**
  88.              * The rest of the payment goes to principal.
  89.              **/
  90.             double principalPaidThisMonth = payment - interestPaidThisMonth;
  91.  
  92.             /**
  93.              * Update the remaining balances and total paid values
  94.              **/
  95.             loanBalance = loanBalance - payment;
  96.             principalBalance = principalBalance - principalPaidThisMonth;
  97.             totalPrincipalPaid = totalPrincipalPaid + principalPaidThisMonth;
  98.             interestBalance = interestBalance - interestPaidThisMonth;
  99.             totalInterestPaid = totalInterestPaid + interestPaidThisMonth;
  100.  
  101.             /**
  102.              * Prints a row of the table.
  103.              */
  104.             System.out.printf("%20.2f\t%25.2f\t%20.2f\n", principalBalance,
  105.                     interestPaidThisMonth, totalInterestPaid);
  106.         }
  107.  
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement