1. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Incorrect formulas

Hi I was hoping to get some help with my code. I think the formulas may be wrong and I just cant figure it out. Here is the assignment with my code. Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen

Java Code:
```public class MortgageCalculator3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double loanAmount = 200000; //This is the loan amount
double monthlyPayment[] = new double[3];//creates the new variable
monthlyPayment[0] = (loanAmount * 5.35 * (Math.pow((1 + 5.35 / 1200), 84))) / (1200 * (Math.pow((1 + 5.35 / 1200), 84) - 1));//creates the formula for 7 year loan
monthlyPayment[1] = (loanAmount * 5.5 * (Math.pow((1 + 5.5 / 1200), 180))) / (1200 * (Math.pow((1 + 5.5 / 1200), 180) - 1));//creates the formula for 15 year loan
monthlyPayment[2] = (loanAmount * 5.75 * (Math.pow((1 + 5.75 / 1200), 360))) / (1200 * (Math.pow((1 + 5.75 / 1200), 360) - 1));//creates the formula for 30 year loa

double interestRate[] = new double[3];
interestRate[0] = 5.75;
interestRate[1] = 5.5;
interestRate[2] = 5.35;

double loan[] = new double[1];
loan[0] = 200000.00;

System.out.println("MortgageCalculator");
System.out.println("Loan A");
System.out.println("Loan Amount: \$200,000");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Length: 30 years");
System.out.println("Monthly Payment: " + monthlyPayment[2]);

double currentLoanAmount;
currentLoanAmount = loanAmount;
for (int i = 1; i <= 360; i++) {   //begins the loop process for 360 payments
if (loanAmount > currentLoanAmount) {
currentLoanAmount = 0;
}

double interestPayment = loanAmount * currentLoanAmount;
currentLoanAmount -= (loanAmount - interestPayment);
System.out.println("For payment #" + i + ", The Interest Payment is \$" + (interestPayment));//displays the results of the interest payment
System.out.println("For payment #" + i + ", The Remaining Balance is \$" + (currentLoanAmount));//displays the results of the remaining balance
currentLoanAmount = 0;
}
System.out.println("Loan B");
System.out.println("Loan Amount: \$200,000");
System.out.println("Annual Interest: 5.55%");
System.out.println("Loan Length: 15 years");
System.out.println("Monthly Payment: " + monthlyPayment[1]);

currentLoanAmount = loanAmount;
for (int i = 1; i <= 360; i++) {   //begins the loop process for 360 payments
if (loanAmount > currentLoanAmount) {
currentLoanAmount = 0;
}

double interestPayment = loanAmount * currentLoanAmount;
currentLoanAmount -= (loanAmount - interestPayment);
System.out.println("For payment #" + i + ", The Interest Payment is \$" + (interestPayment));//displays the results of the interest payment
System.out.println("For payment #" + i + ", The Remaining Balance is \$" + (currentLoanAmount));//displays the results of the remaining balance
currentLoanAmount = 0;
}
System.out.println("Loan C");
System.out.println("Loan Amount: \$200,000");
System.out.println("Annual Interest: 5.35%");
System.out.println("Loan Length: 7 years");
System.out.println("Monthly Payment: " + monthlyPayment[0]);

currentLoanAmount = loanAmount;
for (int i = 1; i <= 360; i++) {   //begins the loop process for 360 payments
if (loanAmount > currentLoanAmount) {
currentLoanAmount = 0;
}
double interestPayment = loanAmount * currentLoanAmount;
currentLoanAmount -= (loanAmount - interestPayment);
System.out.println("For payment #" + i + ", The Interest Payment is \$" + (interestPayment));//displays the results of the interest payment
System.out.println("For payment #" + i + ", The Remaining Balance is \$" + (currentLoanAmount));//displays the results of the remaining balance
currentLoanAmount = 0;
}

}
}```
Last edited by Norm; 01-29-2012 at 03:02 AM. Reason: added code tags

2. ## Re: Incorrect formulas

I think the formulas may be wrong
Have you researched what the formulas should be? What does google give you?

Cross posted at http://www.javaprogrammingforums.com...t-results.html
Last edited by Norm; 01-29-2012 at 03:08 AM.

3. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: Incorrect formulas

That is the only formula I can find that seems correct and that same formula keeps coming up for the mortgage calculator codes. It worked on a previous assignment but isnt now so maybe im doing something else wrong? I dont know I cant figure it out.

4. ## Re: Incorrect formulas

Can you show what the program prints out now and add some comments that shows what the print out should be?

5. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: Incorrect formulas

Here is what I get

run:
MortgageCalculator
Loan A
Loan Amount: \$200,000
Annual Interest: 5.75%
Loan Length: 30 years
Monthly Payment: 1167.1457128870982
For payment #1, The Interest Payment is \$4.0E10
For payment #1, The Remaining Balance is \$4.0E10
For payment #2, The Interest Payment is \$0.0
For payment #2, The Remaining Balance is \$-200000.0
For payment #3, The Interest Payment is \$0.0
For payment #3, The Remaining Balance is \$-200000.0

this continues on throughout each loan up to payment 360 and the remaining balance always prints out as \$-200000.0 and interest payment always prints out \$0.0 ... It is supposed to display the interest payment amount per month as well as the remaining balance.

6. ## Re: Incorrect formulas

You DID NOT SHOW what the correct output should be!!!

You need to try debugging the code to see why the variables have a value of 0.0.
Look at the equation that computes the value and check that the values used are correct.
You can do that by printing out the values on the console.

You need to work out the correct values manually so you can compare them with what the computer gets.

7. ## Re: Incorrect formulas

Well, a lot of stuff is incorrect and I really didn't get the point of the assignment? But I believe that you want to do something like this (with corrected math):

Java Code:
```public class MortgageCalculator3 {

static double interestRate[] = new double[3],loanAmount = 200000,Left = 200000,
daysInAMonth = 30,years[] = new double[3];

public static void main(String[] args) {
interestRate[0] = 5.75;
interestRate[1] = 5.5;
interestRate[2] = 5.35;
years[0] = 30;
years[1] = 15;
years[2] = 7;

for (int i =0;i< interestRate.length;i++){
System.out.println("--------------------------------------\nMortgageCalculator");
System.out.println("Loan " + i);
System.out.println("Loan Amount: \$" + ((int)loanAmount));
System.out.println("Annual Interest: "+interestRate[i]+"%");
System.out.println("Loan Length: "+(int) years[i] +" years");
System.out.println("Monthly Repayment: \$"+ payOff(i));
System.out.println("Monthly Interest: \$" + monthlyPayment(i,loanAmount));
System.out.println("Monthly Total: \$" + (monthlyPayment(i,loanAmount) + payOff(i)));
for(int ii = 1; ii <=(years[i]*12);ii++){
System.out.println("\nPayment Plan for month: " + ii + "\n"+
"Payed Interest: " + monthlyPayment(i,Left) + "\n"
+ "Payed Repayment: " +(int) payOff(i) +"\n"

+ "Total depth: " + (Left -= payOff(i)) );
}
Left = 200000;
}
}
private static double monthlyPayment(int i, double loanAmount){
return ((loanAmount * (interestRate[i]/100))/12);
}
private static double payOff(int i){
return (loanAmount/years[i])/12;
}
}```
I just made a quick draft ... didn't test it but i'll give you a general idea. Hope it helps

8. ## Re: Incorrect formulas

@santa Please don't spoonfeed the OP. Doing their assignment for them doesn't help them to learn how to program. You don't learn from copy and pasting.
You should spend more time helping them understand what the problem with their code was.

9. ## Re: Incorrect formulas

I agree, but I hope you'll understand that I did it out of well meaning. I disagreed with the math that was used and I did not fully understand the task. Because in Sweden the interest rate only represents the part you owe the bank each year for loaning the money and you'll still have to pay of the loan for it do decrease. I do however strongly believe that you can become a better programmer by watching how others have solved the problem. Ex if i cant solve a problem in the univ then I'll check my books to see how others have solved it. When i started learning java and asked at the forums people would (at most) give me a hint in the right direction. But however if you check any school book (math, programming etc) they will always present you with a example solution. Why do you think that is? Would you have known how to solve 6*6 if someone didn't show you first?
I did not test my example and I'm not sure if its correct, but I do believe that if the person truly want to become a better programmer he/she will study my example and not copy it. Sometimes when I ask on the forums i just want to have a plain answer and not a reference to some webpage or a book, because I already know its in the book and if i would have the patience to read the book I would not be asking on the forum. regards

10. ## Re: Incorrect formulas

Originally Posted by santa
if i would have the patience to read the book I would not be asking on the forum. regards
I strongly disagree with that: you should have the patience to read (and study) the book; living in the age of "instant gratification" is no excuse.

kind regards,

Jos

11. ## Re: Incorrect formulas

if i would have the patience to read the book I would not be asking on the forum.
Thanks for confirming my suspicion. I often wonder if an OP is too lazy to look up something and thinks it's easier to ask someone else to do it for him.
Last edited by Norm; 01-29-2012 at 09:01 PM.

12. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: Incorrect formulas

Although I thank you for posting that.... that is something I cannot turn in. My professor would obviously know its not my code and I would really like to fix "my own" code if it can be corrected. I agree with you both in that it is hard for me to understand how this all works without someone actually showing me. I am not a programmer by any means and this being my first programming class Im surprised I was even to come up with what I have so far. I still have not figured this out though. Is any of my code even usable?

13. ## Re: Incorrect formulas

You first need to understand the concepts that have been discussed so far. If you understand them, then you can fix your code. If you don't understand them, then you have to ask specific questions about what confuses you.

14. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: Incorrect formulas

To be honest it now all confuses me I thought I was on the right track because I was able to create the arrays and the loop process but thought it may have been just incorrect math causing the results to be wrong.

15. ## Re: Incorrect formulas

Originally Posted by kprofgold
To be honest it now all confuses me I thought I was on the right track because I was able to create the arrays and the loop process but thought it may have been just incorrect math causing the results to be wrong.
You probably should re-read the posts above and the Wikipedia article on calculating interest and come on back with some questions, I think.

16. ## Re: Incorrect formulas

Originally Posted by Norm
Thanks for confirming my suspicion. I often wonder if an OP is too lazy to look up something and thinks it's easier to ask someone else to do it for him.
Well it's kind of the purpose of forums in general, to ask about things that you don't understand and it really should not matter if you have read the book or not. Ex if i'm wondering something about C++ it's pretty likely to be mention in a C++ book, but if I just want a fast answer without all of the related specs im likely to ask in a C++ forum. I'm not a good java programmer just a Software Engineering student at a univ in Sweden. However I was just trying to help =)

Don't you agree that it's possible to learn from examples?

17. ## Re: Incorrect formulas

Originally Posted by santa
Don't you agree that it's possible to learn from examples?
No (in a very fundamental way); e.g the examples are 1, 2, 4, 8, 16, 32, 64 .. what is the next example?

kind regards,

Jos

18. ## Re: Incorrect formulas

Originally Posted by JosAH
No (in a very fundamental way); e.g the examples are 1, 2, 4, 8, 16, 32, 64 .. what is the next example?

kind regards,

Jos
In math you usually is provided a way to solve a similar example.
In this case I think you are referring to an example as a number or a result to a problem? If so then I do not agree that it is an example.

I do not think that moderators need to have each others backs, how ever if you have a point to make I'm willing to listen. There is no need to criticize someone how is making an effort to help anyway.

19. ## Re: Incorrect formulas

Originally Posted by santa
No, the next number in my sequence is 42 but you didn't read my book that explains it all; you thought you could learn by example and this is what you get ...

kind regards,

Jos

20. ## Re: Incorrect formulas

Originally Posted by JosAH
No, the next number in my sequence is 42 but you didn't read my book that explains it all; you thought you could learn by example and this is what you get ...

kind regards,

Jos
Wow, that is literary the worst comparison I'v ever seen. However had you given me an example of how to solve it, I would have know that the answer was 42 and how to calculate it. You're just trolling man.

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•