PDA

View Full Version : Java help - I don't think this is right...


MacroGuru
08-07-2005, 10:01 PM
Alright, I am in a Java class right now, and for homework, I have to build a mortgage application, and I have the Principal, Annual Rate, and Length hard coded in (part of the assignment)

Then, based off of that, I am to figure out the monthly payment.


/*POS/406
*Mortgage Application
*By Dennis Thomas
*Week Two Assignment**/

public class Mortgage{

public static void main(String[] arguments){


double dRate = 5.75; //Yearly Interest
int iAmount = 200000; //Loan Amount
int iTerm = 30; //Length of Loan in Years
double dMonthRate; //Monthly Interest Rate
int iMonthsTotal; //Total Months of the Loan
double dPayment; //Monthly Loan Payment

//Display prefigured information to the end user
System.out.println("\t\n Your Mortgage Amount is " + iAmount);
System.out.println("\t\n Your Yearly Interest Rate is " + dRate);
System.out.println("\t\n Your Term years are " + iTerm);

dMonthRate = dRate/(12 * 100);//figure out the Monthly Rate

System.out.println("\t\n Your Monthly Rate is " + dMonthRate);

iMonthsTotal = iTerm*12;//figure out the total months of the loan

System.out.println("\t\n Your total months on the loan are " + iMonthsTotal);

dPayment = iAmount*(dMonthRate/(1-(1+dMonthRate)*-iMonthsTotal));//figure out the monthly payment

System.out.println("\t\n Your Monthly Payment is " + dPayment);



}
}


running the formula of

Monthly Payment = Principal * (Monthly Interest / (1 - (1 + Monthly Interest) * -Number of Months)

The number does not a appear to be correct to me...

However, I can not see what I am doing wrong, can anyone look at it and see if they can point out what I am doing wrong...

Easy Mac
08-07-2005, 11:16 PM
This is probably horribly wrong, but try this:
dpayment = (iAmount+(iAmount*dMonthRate)/iMonthsTotal))

Doubt this is right, but it gives a number that looks reasonably normal.

MacroGuru
08-08-2005, 12:39 AM
I tried that, but the monthly payment was like 20K it appeared to me.

Raven
08-08-2005, 01:13 AM
payment = (initAmount / (1- ((1 + monIntRate)^-nMonths)))

is that right?

MacroGuru
08-08-2005, 09:22 AM
payment = (initAmount / (1- ((1 + monIntRate)^-nMonths)))

is that right?
This has provided the closest result so far...I guess the instructor might have to let me know if I am wrong or right.

Radii
08-08-2005, 10:18 AM
http://www.mtgprofessor.com/formulas.htm

The following formula is used to calculate the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of c. [If the quoted rate is 6%, for example, c is .06/12 or .005].

P = L[c(1 + c)^n]/[(1 + c)^n - 1]

Radii
08-08-2005, 10:29 AM
so I think: P = L[c(1 + c)^n]/[(1 + c)^n - 1]

is:

dPayment = iAmount * ( (dMonthRate * Math.pow((1+dMonthRate),iMonthsTotal)) / (Math.pow((1+dMonthRate),iMonthsTotal) - 1) );

... I think, I didn't actually try it out. Parentheses may need adjusting and there may be some casting required.

MacroGuru
08-08-2005, 03:06 PM
so I think: P = L[c(1 + c)^n]/[(1 + c)^n - 1]

is:

dPayment = iAmount * ( (dMonthRate * Math.pow((1+dMonthRate),iMonthsTotal)) / (Math.pow((1+dMonthRate),iMonthsTotal) - 1) );

... I think, I didn't actually try it out. Parentheses may need adjusting and there may be some casting required.
This works, a lot better than what I had...Thank you so much for your assistance