java Calculator- wrong calculation

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 5
Reputation: skol10 is an unknown quantity at this point 
Solved Threads: 0
skol10 skol10 is offline Offline
Newbie Poster

java Calculator- wrong calculation

 
0
  #1
Jan 26th, 2007
I have already turned this so my grade will off anyway but I just need to know. I cannot get the right figure for this calculation, here is what I have and it gives me the wrong calculation of 7178. when its supossed to be 1167. Any help would greatly appreciated. The bolded text is what is wrong.

class MortgageCalculatorx
{
//Program Variables
public static void main(String[] args)
{
double Loan = 200000; // *$200,000 Loan
int Term = 360; //*360 Months
double InterestRate = .0575; //*5.75% interest rate
double Interest = Loan * InterestRate; // calculate interest
double Payment = Loan * InterestRate / 1 - (InterestRate + 1) - 12 * Term; // monthly payment
//Output
System.out.println ();
System.out.println (" This is my mortgage calculator" );
System.out.println ();
System.out.println (" Your loan amount is $"+ Loan); //*prints loan amount
System.out.println ();
System.out.println (" Your interest rate is $"+ InterestRate); //*prints interest rate
System.out.println ();
System.out.println (" Your payment time is " + Term/12 + " years"); //*prints loan term
System.out.println ();
System.out.println (" Your monthly payment is $"+ Payment); //*prints monthly payment
System.out.println ();
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java Calculator- wrong calculation

 
0
  #2
Jan 26th, 2007
I, personally, don't know the right formula for mortgage rate calculations, so please provide the formula you were to use as presented in your assignment instructions.

In any case, when you get the wrong number, then the problem is usually that you have entered the formula wrong, which is the reason I want to see how it was presented to you in your assignment.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,240
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 490
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: java Calculator- wrong calculation

 
0
  #3
Jan 26th, 2007
Bracklets is what you missing in your calculation in first place as your line of code
double Payment = Loan * InterestRate / 1 - (InterestRate + 1) - 12 * Term;
Java's rules of operator precedence are
  1. Multiplication, division and reminder operations are applied first. If an expression contains several such operations, the operators are applied from left to right. Miltiplication, division and remainder have the same level of precedence
  2. Addition and subtraction operations are applied next. If an expresion contains several such operations, the operators are applied from left to right. Addition and subtraction operations have the same level of precedence
  3. If there are any bracklets, calculate first content of braclets using the first two rules and then run calculation on reminding expresion be following first two rules

so you expresion is
Payment = Loan * InterestRate / 1 - (InterestRate + 1) - 12 * Term
replace variables with values
Payment = 200000 * 0.0575 / 1 - ( 0.0575 + 1 ) - 12 * 360
Braclet first
Payment = 200000 * 0.0575 / 1 - 1.0575 - 12 * 360
Multiplications and division
Payment = 11500 / 1 - 1.0575 - 12 * 360
Payment = 11500 - 1.0575 - 12 * 360
Payment = 11500 - 1.0575 - 4320
Payment = 7178 (plus some reminder)

Your expresion is also not realy clear. Why 1 - (InterestRate + 1) ?
Thats gives only negative InterestRate. You could do it with simple -InterestRate
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: skol10 is an unknown quantity at this point 
Solved Threads: 0
skol10 skol10 is offline Offline
Newbie Poster

Re: java Calculator- wrong calculation

 
0
  #4
Jan 26th, 2007
Originally Posted by masijade View Post
I, personally, don't know the right formula for mortgage rate calculations, so please provide the formula you were to use as presented in your assignment instructions.

In any case, when you get the wrong number, then the problem is usually that you have entered the formula wrong, which is the reason I want to see how it was presented to you in your assignment.
Ops so sorry, here it is- Write a program that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. In the first iteration of the program, hard code the principal = $200,000, the term = 30 years, and the annual interest rate = 5.75%-
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java Calculator- wrong calculation

 
0
  #5
Jan 26th, 2007
That, however, is not the formula. I realise now, that the assignment probably did not have the formula in it, and it assumed you knew the formula. Unfortunately, I do not know the formula, can you post that know please. (i.e. Principal * Interest * Term / ......)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: skol10 is an unknown quantity at this point 
Solved Threads: 0
skol10 skol10 is offline Offline
Newbie Poster

Re: java Calculator- wrong calculation

 
0
  #6
Jan 27th, 2007
Thanks there is some good help, The problem I have is that I am not sure what the formula is supposed to be, I have not have any exposure to this and this class is pretty fast pace. Here is what it is based on

payment = [$200,000(1 + .00575)360 x .00575] / [(1 + .00575)360 - 1] = $1167

But I dont know hwo to put that into code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,240
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 490
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: java Calculator- wrong calculation

 
0
  #7
Jan 27th, 2007
Originally Posted by skol10 View Post
Thanks there is some good help, The problem I have is that I am not sure what the formula is supposed to be, I have not have any exposure to this and this class is pretty fast pace. Here is what it is based on

payment = [$200,000(1 + .00575)360 x .00575] / [(1 + .00575)360 - 1] = $1167

But I dont know hwo to put that into code.
You missing operation markers between 200000 and bracklet of (1+0.00575) plus other places, interest is less then 0.5% acording to this, I do not understand why do you apply interest to number of months ( 360 * 0.00575) and last part what is point to take one of final calculation. That will be last step to your output. Shouldn't be like this (1+0.00575)*(360-1)? So if I take empty places are multiplication then code will be like this

payment = ( 200000 * (1 + 0.00575) * 360 * 0.00575) / ((1 + 0.00575) * 360 -1)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java Calculator- wrong calculation

 
0
  #8
Jan 27th, 2007
Using your variables it is as such:

  1. double Payment = Loan * (1 + (InterestRate / 10)) * term * (InterestRate / 10) / (((InterestRate / 10) + 1) * Term - 1);

But I don't know if that is the right formula as it results in 1153 not 1167.

It's always ( InterestRate / 10 ) InterestRate 0.0575, but your formula states 0.00575
Last edited by masijade; Jan 27th, 2007 at 3:16 am. Reason: Forgot the last paren in the equation.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: skol10 is an unknown quantity at this point 
Solved Threads: 0
skol10 skol10 is offline Offline
Newbie Poster

Re: java Calculator- wrong calculation

 
0
  #9
Jan 27th, 2007
The only thing is that the payment should be 1167.00 I dont know ahy this doesn't come out to 1167.00; 5.75% on a 200,000 loan over 30 years, the monthly payment will be 1167.
http://www.mortgage-calc.com/mortgage/simple.php
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,450
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java Calculator- wrong calculation

 
0
  #10
Jan 27th, 2007
Well, the line of code provided to you directly implements the formula you provided. If the answer is wrong, the formula is wrong. I'm sorry, but the calculation, given the formula provided, is correct. Do it yourself using a scientific calculator (so you can enter the parens) and you will see for yourself. Check the formula again.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC