Payment Mortgage Calculator

Reviews
COMP 110 Fall 2008: Assignment 4 The Mortgage Calculator Due: 11:59 pm, Wed, Oct. 8, 2008 Readings: Snyder 18-21 "Neither a borrower nor a lender be" Polonius’ advice to Prince Hamlet (3.1.78) Introduction “If I borrow X dollars for Y years at Z percent interest, what will my monthly payments be?” When you take out a loan for a car or a house, you specify three parameters: the amount of the loan (how much do you want to borrow), the period of the loan (how many years do you want to take to repay), and the annual interest rate. From these three numbers, the monthly payment can be calculated. There are several ways to calculate the monthly payment. One way is a direct solution to an algebraic equation; this is the practical way to do it, but it is NOT what we will be doing. In this assignment you will do it by brute force using a simple mathematical technique called bisection. We are using a brute force method in order to practice writing loops, functions, and function calls. We will essentially be taking a guess at what the monthly payment is, and then computing out how the amount we borrowed drops towards zero month by month as we pay. If we hit zero after the full number of payments, we have guessed the payment correctly; if we do not hit zero, we guessed badly and need to try another guess. The cleverness in bisection is in how we make the guesses. Basics of Bisection If you are trying to guess the answer to a math question and you have some way of knowing if your guess is too high or too low, then there’s an easy and efficient way to get the answer (or very close to it). You start by determining two extremes: one that is clearly too low (which we’ll call min) and one that is clearly too high (which we’ll call max). You then know that the right answer is trapped between min and max. You then guess at the midpoint between the extremes: mid = (min + max)/2. If this is too high, then you know the correct answer must lie in the range min…mid. If your guess is too low, then you know the answer is in the range mid…max. We’ve now cut the possible range in half. We continue this way successively halving the possible range until we get close enough. For example, on the quiz show The Price is Right, you are asked to guess the price of a fabulous prize: a big screen television. You make a guess and then Bob Barker tells you if your guess is too high or too low. You start with min of 0 (the prize clearly costs more than nothing) and a max of, say, $10,000 – clearly more than the TV is worth. You first guess is (min+max)/2 or $5000. Bob says that’s too high, so now you know that the price is between 0 and $5000. So your next guess is (0+5000)/2 or $2500. Bob says that is too low. So now you know the price is in $2500…$5000. Your next guess is (2500+5000)/2 or $3750. You continue this way until you get the correct price. Because you are halving the size of the possible range at each step, you arrive at the correct price very quickly. For example, starting with min of $0 and max of $10,000, you can get the correct price (within one dollar) in fourteen guesses or fewer1. Determining monthly payment If you borrow $10,000 at 6% annual interest, here’s how the process goes. First, we have to convert annual interest to monthly fraction by dividing by 1200. (You are dividing annual percentage by 100 to get the annual fraction and then by 12 to get the monthly fraction.) So the monthly rate is 0.5%; the monthly fraction is 0.005. Then each month, the balance you owe is increased because of the interest and decreased by your payment. new balance = old_balance + ( old_balance * monthly_int_fraction ) – payment The perfect monthly payment is the one that will reduce your balance to zero (or, in this case, within a penny of zero) after the specified number of payments. Determining the initial min and max If you borrow $10,000 for 10 years at 6% interest, your monthly payment is clearly more than $0 and clearly less than $10,000. So these will be our starting points. Our first guess will be (0+10000)/2 or 5000. Paying 5000 per month for 10 years will result in a negative balance (we paid too much), so our next guess will be (0+5000)/2 or 2500. This is also too big. The process continues until we discover, after about 28 steps that $111.02 is the perfect monthly payment. The perfect monthly payment is the one that has a final balance (the amount left to pay after the final payment) between $0.00 and $0.01 (that is, between zero and one cent). The loan will cost us a total of $111.02*120 = $13,322.40 (the monthly payment multiplied times the number of months.) Program Your job is to build a monthly payment calculator. The user will enter a loan amount, a period (in years) and a rate (as a percent, possibly with fractional part), press a button, and the calculator will display the monthly payment, the total amount paid, and the number of steps required to reach the solution. Your page should contain the following fields and buttons (in this order):  Appropriately labeled text boxes for the loan amount, loan period, and rate. 1 For the mathematically minded, you can guess a number between 0 and N in log 2(N) guesses (or fewer if you’re lucky).   A “Calculate payment” button Text box for the perfect monthly payment In addition to producing the monthly payment in the text box, your program should use “alert” to tell the user the total paid over the life of the loan, and the number of steps needed to guess the payment (both in one alert box). As before, the HTML should give proper documentation for your program as well. Error checking Your program should detect and handle the following errors. An alert message is sufficient to inform the user: 1. 2. 3. 4. A loan amount that is zero or less (no one borrows zero). A loan amount that is not a number2 (e.g. “Hello”). A loan period that is not an integer3, is less than one, or not a number. A rate that is less than zero or not a number Writing functions In this program you will write a main function (called when you click the button) and at least one helper function. There are several obvious candidates for helper functions. One is a function that takes the current payment guess and returns the final balance once that payment has been paid for all the months of the loan. Another is a function that will generate a new guess at a monthly payment, given the current interval. Rounding dollar amounts You should display both the perfect monthly payment and the total paid with two decimal places (see monthly payment above). You can easily round a value to 2 decimal places by multiplying by 100, rounding using Math.round, then dividing by 100. If the ones digit happens to be zero, you will get one decimal place rather than 2 (see total payment above). Don’t worry about this. What to turn in Save your html file under your onyen in the public_html folder. Don’t link it to anything. You will turn in your program via BlackBoard as before. Put the following header at the top of the HTML file (with your ONYEN and name of course): 2 The built-in function isNaN(x) is true if x is not a number. Sort of backwards, but that’s what’s available. Note that the empty string is interpreted as the number 0, so isNaN(“”) is false. 3 To determine if a number is an integer, take its floor (Math.floor) and see if it equal to the original. For example Math.floor(4) and Math.floor(4.5) are both 4. So Math.floor(4)==4 is true while Math.floor(4.5)==4.5 is false.

Related docs
Mortgage Payment Calculator
Views: 369  |  Downloads: 76
Mortgage Calculator
Views: 70  |  Downloads: 0
Payment Calculator
Views: 412  |  Downloads: 46
Mortgage Payment Calculator
Views: 31  |  Downloads: 2
Mortgage Payment Calculator
Views: 273  |  Downloads: 22
calculator for mortgage payment
Views: 56  |  Downloads: 3
Mortgage Calculator
Views: 479  |  Downloads: 27
Mortgage Payment Calculator
Views: 35  |  Downloads: 2
Mortgage Payment Calculator
Views: 16  |  Downloads: 5
Interest Mortgage Payment Calculator
Views: 24  |  Downloads: 0
Mortgage Affordability Calculator
Views: 41  |  Downloads: 2
Mortgage Calculator
Views: 97  |  Downloads: 17
home mortgage payment calculator
Views: 23  |  Downloads: 0
premium docs
Other docs by nicknameD
Secrets Revealed From
Views: 116  |  Downloads: 3
Services Invoice Form
Views: 73  |  Downloads: 1
Savings Interest Calculators
Views: 227  |  Downloads: 0
Set Work Schedule
Views: 224  |  Downloads: 0
Service Invoice Forms
Views: 172  |  Downloads: 8
Semi Annual Compound
Views: 93  |  Downloads: 0
Semi Annual Amortization
Views: 95  |  Downloads: 0
Semi Monthly Calculator
Views: 136  |  Downloads: 0
Sample M File
Views: 377  |  Downloads: 6
School Year Templates
Views: 67  |  Downloads: 0
School Year Template
Views: 145  |  Downloads: 1
Search Tracking Form
Views: 70  |  Downloads: 3
Search Free Printable
Views: 277  |  Downloads: 0
School Schedule Template
Views: 486  |  Downloads: 6
School Year Calendar
Views: 71  |  Downloads: 0