Click here to Skip to main content
15,171,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MAIN:
Java
package loantable;
import java.util.ArrayList;
public class LoanTable
{
    private double loanAmount;
    private double loanLength;
    private double lowInterest;
    private double highInterest;
    private int count;
    ArrayList<Double> payments = new ArrayList<>();
    public LoanTable(double loanAmount, double loanLength, double lowInterest, double highInterest)
    {
        this.loanAmount = loanAmount;
        this.loanLength = loanLength;
        this.lowInterest = lowInterest;
        this.highInterest = highInterest;
    }
    public ArrayList getInterestRates()
    {
        count = -1;
        while(lowInterest <= highInterest)
        {
            double monthlyInterest = lowInterest / 12.0;
            double length = loanLength * 12;
            double c = Math.pow((1 + monthlyInterest), length);
            double monthlyPayment = (loanAmount * monthlyInterest * c)/ (c - 1);
            payments.add(monthlyPayment);
            lowInterest += .25;
            count++;
        }
        return payments;
    }
    public String to()
    {
        String theory = "";
        double currentInterest = highInterest;
        ArrayList<Double> t = new ArrayList<>();
        t = getInterestRates();
        while(count >= 0)
        {
            theory = theory + "\n" + "Interest Rate: " + currentInterest + " Monthly Payment: " +  t.get(count);
            count--;
            currentInterest -= .25;
        }
        return theory;
    }
}


TESTER:
Java
package loantable;

import java.util.Scanner;

public class LoanTableTester 
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Principal: ");
        double loanAmount = sc.nextDouble();
        System.out.print("\nTime: ");
        double time = sc.nextDouble();
        System.out.println("\nLow Rate: ");
        double lowRate = sc.nextDouble();
        System.out.println("\nHigh Rate: ");
        double highRate = sc.nextDouble();
        LoanTable t = new LoanTable(loanAmount, time, lowRate, highRate);
        System.out.println(t.to());
    }
}

CONSOLE:
Principal: 100000
Time: 30
Low Rate: 
11
High Rate: 
12
Interest Rate: 12.0 Monthly Payment: 100000.0
Interest Rate: 11.75 Monthly Payment: 97916.66666666666
Interest Rate: 11.5 Monthly Payment: 95833.33333333334
Interest Rate: 11.25 Monthly Payment: 93750.0
Interest Rate: 11.0 Monthly Payment: 91666.66666666664


CORRECT ANSWER:
Principal = $100000.00

Time = 30 years

Low rate = 11%

High rate = 12%

 11.00 952.32

 11.25 971.26

 11.50 990.29

 11.75 1009.41

 12.00 1028.61


FORMULA:
The formula for determining payments is:
(p * k * c)/ (c - 1)

p = principal, amount borrowed

k = monthly interest rate (annual rate/12.0)

n = number of monthly payments (years * 12)

c = (1 + k)^n

a = monthly payment (interest and principal paid)


I have tried everything I can think of, not sure what is causing this logic issue! Please help, any tips or ideas are greatly appreciated.
Posted
Updated 10-Nov-14 2:19am
Comments
Kornfeld Eliyahu Peter 10-Nov-14 1:39am
   
And your question is?
Richard MacCutchan 10-Nov-14 4:13am
   
Use your debugger to step through the code and see exactly what the various values are at each step.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900