This lab will take you step by step through a process designed to produce a program that can do a sophisticated calculation. By breaking the calculation down into parts and writing methods to handle those parts of the calculation, we will make it easier to produce the final result.
The program we are ultimately going to produce will compute monthly payments given the details of a loan. The first step in the solution process is to write a method that can run a loan for a set number of months with a set payment and compute the remaining balance on the loan.
The first method you will write is
public static double runLoan(double principal,double rate,double payment,int months)
Given the original loan amount (principal), an annual interest rate (rate), a monthly payment (payment), and a term of months to run the loan (months), this method will compute and return the remaining balance on the loan after the loan has run for the indicated number of months.
To do its work, the runLoan method will contain a loop that runs for the indicated number of months. For each month the loop will do the following:
If the monthly payment is less than what is needed to pay off the loan completely, there will be a balance remaining on the loan at the end of the term. If the monthly payment is too high, the loan could actually be paid off before the end of the loan term. In that case, runLoan should return a negative balance.
Write a short test program and test your method against the following test data
| Loan Amount | Interest Rate | Term in Years | Monthly Payment | Balance |
|---|---|---|---|---|
| $100,000 | 6.5% | 15 | $600.00 | $82,293.22 |
| $80,000 | 6.0% | 20 | $700.00 | -$58,612.27 |
| $150,000 | 7.0% | 30 | $1000.00 | -$2,496.37 |
| $100,000 | 6.5% | 15 | $700 | $22,350.02 |
The interesting problem here is determining the correct monthly payment given a loan amount, an interest rate, and a term in months to run loan. The correct payment is the payment that leaves the loan balance as close to 0 as possible at the end of the term.
If we fix the other parameters and view the runLoan method above purely as a function of its payment parameter, we can pretend that runLoan is a function of one variable. The problem of finding the optimal payment in that case reduces to the problem of finding the root of a function of one variable. Fortunately, this is a problem we have already solved. Here is the code for the root finding program I showed in class.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a, b, c, d;
double left, right, x;
System.out.println("Enter values for a, b, c, and d: ");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
d = input.nextDouble();
System.out.println("Enter a range to search:");
left = input.nextDouble();
right = input.nextDouble();
// Compute the midpoint of the range
x = (left + right) / 2.0;
while (right - left > 10e-6) {
// Evaluate the polynomial at left and x
double fLeft = ((a * left + b) * left + c) * left + d;
double fX = ((a * x + b) * x + c) * x + d;
// If fLeft*fX is negative, the root is between left and x
// Otherwise, the root is between x and right.
if (fLeft * fX < 0.0)
right = x;
else
left = x;
// Compute a new midpoint
x = (left + right) / 2.0;
}
System.out.println("The approximate root is " + x);
}
Restructure this code to find the root of the runLoan method with fixed principal, rate, and term. Package your code into a method
public static double computePayment(double principal,double rate,int months)
That computes and returns the correct payment for the loan.
Modify your test program to test your method against the following test data.
| Loan Amount | Interest Rate | Term in Years | Monthly Payment |
|---|---|---|---|
| $100,000 | 6.5% | 15 | $871.11 |
| $80,000 | 6.0% | 20 | $573.14 |
| $150,000 | 7.0% | 30 | $997.96 |
| $100,000 | 6.5% | 20 | $745.57 |
The final method we are going to write is a method that will print a table of monthly payments for a loan given the amount of the loan, the term in months, and a range of interest rates.
Given a loan amount of $100,000, a loan term of 30 years, and interest rates ranging from 6.0% to 7.0% in steps of 0.1%, this method should print the following table.
Rate Payment 6.0 $599.55 6.1 $606.00 6.2 $612.47 6.3 $618.97 6.4 $625.51 6.5 $632.07 6.6 $638.66 6.7 $645.28 6.8 $651.92 6.9 $658.60 7.0 $665.30
(To get nicely formatted output, I suggest you use the System.out.printf method. You can read a discussion of that method in section 3.6 of the textbook.)
Write the code for a method
public static void printPaymentTable(double principal,double lowRate
,double highRate,int months)
that prints the table of payments as the interest rate ranges from the low rate to the high rate in steps of 0.1%. Modify your program's main to prompt the user for the appropriate loan details and then call the printPaymentTable to display the payment information.
When you are done, email me the source code for your final program for grading.
The method I suggested above for computing the monthly payment on a loan is useful mostly as an exercise in programming. A more practical way to solve the problem is to use a formula to compute the monthly payment. According to the textbook, the formula to compute the monthly payment for a loan is

where rate is the monthly interest rate and term is the length of the loan in months. If you are curious, you might want to write a method to compute the payment by this method and then modify the printPaymentTable method to print the monthly payments computed by both methods.