| | |
java Calculator- wrong calculation
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
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 ();
}
}
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 ();
}
}
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.
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
----------------------------------------------
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
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
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
double Payment = Loan * InterestRate / 1 - (InterestRate + 1) - 12 * Term;
Java's rules of operator precedence are
- 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
- 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
- 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
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
----------------------------------------------
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
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
payment = [$200,000(1 + .00575)360 x .00575] / [(1 + .00575)360 - 1] = $1167
But I dont know hwo to put that into code.
•
•
•
•
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Using your variables it is as such:
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
Java Syntax (Toggle Plain Text)
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
----------------------------------------------
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
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
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
http://www.mortgage-calc.com/mortgage/simple.php
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
----------------------------------------------
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
![]() |
Other Threads in the Java Forum
- Previous Thread: unexpected type error
- Next Thread: Java for PDA
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel jtable julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






