Imagine that a lender (e.g., a bank) has agreed to let you borrow a specific amount of money at a specific rate of interest. What is left to decide is the period of time over which you intend to repay the loan (i.e., the duration of the loan). A payment is to be made each month.
To make that decision, it would be helpful to know what the monthly payment would be for each in a range of possible durations. (For example, perhaps you would like to pay off the loan over a period of somewhere between two and four years (i.e., between 24 and 48 months).)
The formula for monthly payment is
P = | A·i |
1 − 1/(1 + i)n |
where P is the monthly payment, A is the loan amount, i is the monthly interest rate (which is 1/12 of the annual rate), and n is the duration of the loan (in months).
Enter amount borrowed:> 1000 Enter annual interest rate:> 5.0 Enter min duration (# months):> 1 Enter max duration (# months):> 12 Loan amount: $1000.00 Annual interest rate: 5.00% Minimum duration: 1 months Maximum duration: 12 months # Months Monthly Payment -------- --------------- 1 $ 1004.17 2 $ 503.13 3 $ 336.11 4 $ 252.61 5 $ 202.51 6 $ 169.11 7 $ 145.25 8 $ 127.36 9 $ 113.44 10 $ 102.31 11 $ 93.20 12 $ 85.61 |
In completing that method, you will almost certainly want to make use of the pow() method in the java.lang.Math class. As an example of its use, consider the assignment statement
where z is a variable of type double and each of x and y is of type either double or int. The effect of this statement is to assign to z (an approximation to) the value xy (i.e., x raised to the y power).
In order to make the numbers in the table line up nicely (and for exactly two digits to appear after the decimal points), you will want to make use of the System.out.printf() method. As an example, suppose that your program had variables named numMonths and monthlyPayment representing the quantities suggested by their names. Then a statement to print one row of the table might look like this:
System.out.printf("%5d $%8.2f\n", numMonths, monthlyPayment);
The "%5d" indicates that the value of numMonths should be printed, right justified, in a field of width five. The "%8.2f" indicates that the value of monthlyPayment should be printed, right justified, in a field of width eight, with two digits appearing after the decimal point. The " $" in between says that, literally, eight spaces followed by a dollar sign should appear in between those two "fields". The "\n" says to skip to the next line.
System.out.printf("%5d $%8.2f $%8.2f\n", numMonths, monthlyPayment, interest);
Note that you can submit more than one time. Hence, if, after submitting, you improve your program (e.g., by fixing logic errors or by enhancing your comments), you should submit the newer version, but its name should remain LoanRepayment.java!
The program that was provided to you (see link above) includes a comment describing the purpose/behavior of the program; a comment identifying the course, semester, and assignment number; and comments intended to identify the program's authors (including you!) and those who helped you to develop the program. Also included is a comment intended for you to describe any known deficiencies of your program. Consider this to be a template that you are expected to follow in all subsequent assignments.