# Help with Equation

Printable View

• 09-26-2013, 11:09 PM
TJRedeemer
Help with Equation
I'm working on an assignment for school. The assignment is right here:

CP 1 - Assignment # 2

And before you ask, no I am not asking anyone to do my work for me, I'm just asking for help. You see, when I run the first test, I get \$600000.00 instead of \$599.55, which is apparently what I am supposed to get. The proper equation is shown on the webpage, but I can't seem to figure out what is wrong with mine. Can anyone help? The code is attached.

Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package loanpaymentfinder; import java.text.NumberFormat; import java.util.Scanner; /**  *  * @author TJ  */ public class LoanPaymentFinder {     /**     * @param args the command line arguments     */     public static void main(String[] args) {                 Scanner in = new Scanner(System.in);                 NumberFormat num = NumberFormat.getCurrencyInstance();                 double numInputLoan;         double numInputRate;         double numInputYears;         double numMonths;         double numRateSquared;         double numOutput;         double numRatePlusOne;                 //Input         System.out.print ("Enter loan amount: ");         numInputLoan = Double.parseDouble (in.nextLine());                 System.out.print ("Enter rate: ");         numInputRate = Double.parseDouble (in.nextLine());                 System.out.print ("Enter number of years: ");         numInputYears = Double.parseDouble (in.nextLine());                 //Math goes her                 numMonths = 12 * numInputYears;                 numRatePlusOne = numInputRate + 1;                 numRateSquared = Math.pow (numRatePlusOne, numMonths);                 numOutput = numRateSquared/(numRateSquared - 1);                 numOutput = numInputRate * numInputLoan * numOutput;                 //Output         System.out.println ("The mounthly payment is:");         System.out.println (num.format(numOutput));```
• 09-27-2013, 01:48 AM
Norm
Re: Help with Equation
Please post the code on the forum. Be sure to wrap the code with code tags.
[code]
>>>>>>>COde HErE
[/code]
• 09-27-2013, 04:53 AM
TJRedeemer
Re: Help with Equation
Thanks for the warning, it's fixed.
• 09-27-2013, 05:10 AM
Norm
Re: Help with Equation
There are an number of statements used to compute the value that is printed. Have you verified that each one is generating the correct result? Add a println statement after each one that prints out the results so you can see which statements are not doing what you want.

Also you need to post what the user inputs to the program when it is executed. Three values are read in. What are they?
• 09-27-2013, 05:11 AM
jim829
Re: Help with Equation
The problem is with your interest rate. You enter it as a whole number. But it is supposed to be a percentage. That means it needs to be divided by 100.
However, since you are amortizing your load amount monthly and the entered interest rate is annual, you need to divide again by 12. So in total, your entered interest rate needs to be divided by 1200.

Regards,
Jim
• 09-27-2013, 11:12 PM
TJRedeemer
Re: Help with Equation
Quote:

Originally Posted by jim829
The problem is with your interest rate. You enter it as a whole number. But it is supposed to be a percentage. That means it needs to be divided by 100.
However, since you are amortizing your load amount monthly and the entered interest rate is annual, you need to divide again by 12. So in total, your entered interest rate needs to be divided by 1200.

Regards,
Jim

Thanks, I've almost got it, but now it gives me \$500. I can't see what else is wrong here. Can someone help me?

Code:

```/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package loanpaymentfinder; import java.text.NumberFormat; import java.util.Scanner; /**  *  * @author TJ  */ public class LoanPaymentFinder {     /**     * @param args the command line arguments     */     public static void main(String[] args) {                 Scanner in = new Scanner(System.in);                 NumberFormat num = NumberFormat.getCurrencyInstance();                 double numInputLoan;         double numInputRate;         double numInputYears;         double numMonths;         double numRateSquared;         double numOutput;         double numRate;                 //Input         System.out.print ("Enter loan amount: ");         numInputLoan = Double.parseDouble (in.nextLine());                 System.out.print ("Enter rate: ");         numInputRate = Double.parseDouble (in.nextLine());                 System.out.print ("Enter number of years: ");         numInputYears = Double.parseDouble (in.nextLine());                 //Math goes here                 numMonths = 12 * numInputYears;                 numInputRate = numInputRate / 1200;                 numRateSquared = numInputRate + 1;                 numRateSquared = Math.pow (numRateSquared, numMonths);                 numOutput = numInputRate * numInputLoan * numRateSquared/numRateSquared * -1;                 //Output         System.out.println ("The mounthly payment is:");         System.out.println (num.format(numOutput));                             } }```
• 09-27-2013, 11:31 PM
Norm
Re: Help with Equation
Have you looked at the results of each statement to see where the code is going wrong?
• 09-27-2013, 11:54 PM
TJRedeemer
Re: Help with Equation
Quote:

Originally Posted by Norm
Have you looked at the results of each statement to see where the code is going wrong?

Yes, and it seems like the problem is with Math.pow method. The equation should be like (1+Rate)^months, but when the rate is 6%, it gives me \$6.02, which is obviously wrong.
• 09-28-2013, 12:07 AM
Norm
Re: Help with Equation
Can you post statement, the values of the variables used in that statement and the results of executing the statement that shows what you are talking about. Also post what you expect the output to be.
• 09-28-2013, 12:14 AM
TJRedeemer
Re: Help with Equation
Quote:

Originally Posted by Norm
Can you post statement, the values of the variables used in that statement and the results of executing the statement that shows what you are talking about. Also post what you expect the output to be.

numRateSquared = Math.pow (numRateSquared, numMonths); The statement should be \$599.55.
• 09-28-2013, 12:38 AM
jim829
Re: Help with Equation
How can you almost have it? You should totally have it!! Take your original code (First post), add a closing brace, and simply divide the input rate by 1200.
Do not change anything else.

Regards,
Jim
• 09-28-2013, 01:10 AM
TJRedeemer
Re: Help with Equation
Thank you SOOO much. It is done.