Results 1 to 20 of 47
Thread: Incorrect formulas
 01292012, 02:55 AM #1Member
 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; 01292012 at 03:02 AM. Reason: added code tags
 01292012, 03:01 AM #2
Re: Incorrect formulas
I think the formulas may be wrong
Cross posted at http://www.javaprogrammingforums.com...tresults.htmlLast edited by Norm; 01292012 at 03:08 AM.
 01292012, 03:21 AM #3Member
 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.
 01292012, 03:39 AM #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?
 01292012, 03:55 AM #5Member
 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.
 01292012, 04:07 AM #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.
 01292012, 07:01 AM #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; } }
 01292012, 02:36 PM #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.
 01292012, 06:37 PM #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
 01292012, 07:21 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,823
 Blog Entries
 7
 Rep Power
 21
 01292012, 07:48 PM #11
Re: Incorrect formulas
if i would have the patience to read the book I would not be asking on the forum.Last edited by Norm; 01292012 at 09:01 PM.
 01292012, 08:27 PM #12Member
 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?

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.
 01292012, 08:43 PM #14Member
 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.

Re: Incorrect formulas
 01292012, 10:17 PM #16
Re: Incorrect formulas
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?
 01292012, 10:42 PM #17
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,823
 Blog Entries
 7
 Rep Power
 21
 01292012, 11:16 PM #18
Re: Incorrect formulas
I don't understand your point? Your looking for a function N*2?
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.
 01302012, 10:03 AM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,823
 Blog Entries
 7
 Rep Power
 21
 01302012, 03:56 PM #20
Similar Threads

Help with average amount formulas
By dimesnnix in forum New To JavaReplies: 6Last Post: 06032011, 01:45 AM 
lucene3.0.2: getting incorrect no. of occurrence in file
By ranjitots in forum LuceneReplies: 0Last Post: 12062010, 04:36 PM 
Formulas in methods or constructors?
By kyameron in forum New To JavaReplies: 11Last Post: 11202010, 01:22 PM 
What in my function incorrect?
By artemff in forum New To JavaReplies: 5Last Post: 01022010, 05:25 PM 
Incorrect Package? Help!
By chaits86 in forum NetBeansReplies: 10Last Post: 12172008, 04:08 AM
Bookmarks