Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2012, 09:38 PM   PM User | #1
KenanCross
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
KenanCross is an unknown quantity at this point
Question Need help with Math.pow Calculations

Hello again! Thanks for the help in the previous post I made last week. I'm in need of a little bit of assistance again.

I'm trying to get Math.pow to calculate and produce a result for my Mortgage Calculator project. I need to show a decreasing amount in the remaining balance of a mortgage loan. However the results I get seem to always stay the same. What am I doing wrong?

Code:
import java.text.DecimalFormat;
// This calculator will accurately calculate Mortgage Payments, while allowing for flexible variable adjustments 
//(i.e. Length of Payments, Principal, Interest.) Based on Service Request from McBride Financial

// Author: Kenan Cross //

public class MortgageCalculator {
    int i; // counter
    int termLimit = 30;
    int months;
    int payments; /* No. of monthly payments in mortgage*/
    int monthsPd = 12 * i; //Calculations of months paid so far
    double balPd; //Part of Balance Paid Formula
    double balPd2; //Part of Balance Paid Formula
    double balPd3; //Part of Balance Paid formula
    double balPdCalc; //Calculated result of balPd and balPd2
    double loanCalc;   //Power of Calculation for compound interest
    double powCalc; //Power of calculation variable
    double rate;
    double loanBalance;
    double loanInterest; //Calculated Loan with Interest
    double newLoanBalance; //Calculated Remaining Loan Balanace
    double annualRate;  //Computed Annual Rate
    double interestPlus; // rate + 1
    double interestPd;  // contains interest paid so far
    double interestAdj;
    double interestExp;
    int length; /* length of mortgage */
    double principle; /* Principle loan amount */
    double monthlyPayment; /* Calculated Monthly Payment */
    DecimalFormat dec;
    DecimalFormat percentage;


public static void main (String args[]){
	MortgageCalculator calc = new MortgageCalculator();
	calc.calculatePaymentAmount();
	calc.printPaymentAmount();
}

public MortgageCalculator(){
	months = 12;
	rate = 0.0575;
        principle = 200000;
	length = 30;
	dec = new DecimalFormat ("$0.00");
        percentage = new DecimalFormat("0.00%");
}

public void calculatePaymentAmount(){
	annualRate = ((rate)/(12));
        payments = (months) * (length);
	interestAdj = (annualRate)+(1);
        interestPlus = (rate + 1);
	interestExp = Math.pow(interestAdj, payments);
	monthlyPayment = principle * (annualRate * interestExp)/(interestExp - 1);
        loanCalc = principle * (Math.pow(interestAdj,(termLimit)));
}
//Above formula is based on M=P[i(1+i)^n]/[(1+i)^n-1] found on ifitbreaks.com/interest.htm//
public void printPaymentAmount (){
     System.out.println("Your Loan Amount is "+ dec.format(principle));
     System.out.println("Your Annual Rate is "+ percentage.format(rate));
     System.out.println("Your monthly payment amount is " + dec.format (monthlyPayment));
     System.out.println("Your principal balance with interest is " + dec.format (loanCalc));
    for (i=1; i <= termLimit; i++){
     //Loop set for the limit of Mortgage term
     powCalc = principle * (Math.pow(interestAdj, i))-1;
     balPd = monthlyPayment/annualRate;
     balPd2 = balPd - principle;
     balPd3 = 1 - Math.pow(annualRate, monthsPd);
     balPdCalc = balPd - (balPd2 * balPd3); 
     //formula is based on Amt Paid = (L/R - P)[(1+R)^X - 1] found
     // at http://www.infobarrel.com/Financial_Math:__Calculating_the_Mortgage_Balance_after_X_Number_of_Years
     interestPd = powCalc - principle;
     System.out.println("\tIn Year " + i + " you will have paid " + dec.format(interestPd) + " in interest.");
     System.out.println("\t Your remaining loan balance is " + dec.format(balPdCalc));
        try {
            Thread.sleep(5000);
            } 
        catch (InterruptedException e) {
            }
    }
 }
}
KenanCross is offline   Reply With Quote
Old 02-15-2012, 06:18 PM   PM User | #2
clinto
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
clinto is an unknown quantity at this point
One thing that is happening is your monthsPd variable is being initialized at 12 * i; and never changes through each iteration. You want to move it into your for loop.

Code:
     for (i=1; i <= termLimit; i++){
     //Loop set for the limit of Mortgage term
     monthsPd = 12 * i;
Based on the formulas I see on http://www.infobarrel.com/Financial_...umber_of_Years I came up with this code:

Code:
    double amtPaid = (monthlyPayment/annualRate - principle) * (Math.pow(1 + annualRate, monthsPd) - 1);
    double mortgageBalance = loanCalc - amtPaid;
You would then print mortgageBalance.
Code:
System.out.println("\t Your remaining loan balance is " + dec.format(mortgageBalance));
clinto is offline   Reply With Quote
Reply

Bookmarks

Tags
java, math.pow

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:42 AM.


Advertisement
Log in to turn off these ads.