Need Help Completing a Loan Table
I'm continuing to try and solve the problem on my own but if any people have tips I would appreciate it.... Focus on the generateLoanTable() method because everything else is good. When I run the program in my main method (not included) it shows the loan amount entered multiplied by the rate (not as a percentage) and year number instead of the total payment for the loan period and rate associated in the table. Also, I don't know the code to put in to highlight reserved words on the thread posts so if I could get that info too that would help :)
Code java:
import java.text.*;
class Loan {
//---------------------
//DATA MEMBERS
//----------------------
private final int MONTHS_IN_YEAR = 12;
private double loanAmount;
private double monthlyInterestRate;
private int numberOfPayments;
private static final int BEGIN_YEAR = 5;
private static final int END_YEAR = 30;
private static final int YEAR_INCR = 5;
private static final double BEGIN_RATE = 6.0;
private static final double END_RATE = 10.0;
private static final double RATE_INCR = 0.25;
//Constructor
public Loan(double amount, double rate, int period) {
setAmount(amount);
setRate(rate);
setPeriod(period);
}
//Returns the loan amount
public double getAmount() {
return loanAmount;
}
//Returns the loan period in number of years
public int getPeriod() {
return numberOfPayments / MONTHS_IN_YEAR;
}
//Returns the loan's annual interest rate
public double getRate() {
return monthlyInterestRate * 100.0 * MONTHS_IN_YEAR;
}
//Returns the loan's monthly payment amount
public double getMonthlyPayment() {
double monthlyPayment;
monthlyPayment = (loanAmount * monthlyInterestRate)
/
(1 - Math.pow(1/(1 + monthlyInterestRate),
numberOfPayments));
return monthlyPayment;
}
//Returns the total payment amount for the loan
public double getTotalPayment() {
double totalPayment;
totalPayment = getMonthlyPayment() * numberOfPayments;
return totalPayment;
}
//Generates loan table
public void generateLoanTable(double loanAmt) {
DecimalFormat df = new DecimalFormat("0.00");
System.out.print("\t");
for(int colLabel = 5; colLabel <= 30; colLabel += 5){
System.out.format("%11d", colLabel);
}
System.out.println("\n");
for(double rate = BEGIN_RATE; rate <= END_RATE; rate += RATE_INCR){
System.out.print(df.format(rate) + "% ");
for(int year = BEGIN_YEAR; year <= END_YEAR; year += YEAR_INCR){
loanAmt = getAmount() * rate * year;
System.out.print(" " + df.format(loanAmt));
}
System.out.println();
}
System.out.println();
}
//Sets the loan amount
public void setAmount(double amount) {
loanAmount = amount;
}
//Sets the annual interest rate
public void setRate(double annualRate) {
monthlyInterestRate = annualRate / 100.0 / MONTHS_IN_YEAR;
}
//Sets the loan period
public void setPeriod(int periodInYears) {
numberOfPayments = periodInYears * MONTHS_IN_YEAR;
}
}
Re: Need Help Completing a Loan Table
I found your errors.
First at the beginning of the program where you defined your BEGIN_RATE, END_RATE AND RATE_INCR, you should change that to percentage value. It is because you have not changed the rate into percentage anywhere else. Rate is always in percentage.
Make it 0.06, 0.1 and 0.0025 respectively.
Then coming down to your generateLoan() method this is what you needed there. loanAmt = getAmount() + getAmount() * rate * year. It is because if you wanted to calculate the total payable amount during that period, then it should be the interest plus the principle amount.
Your program generates outputs without converting the rate into percentage and also without adding the principle amount.
It should solve your problem. Good luck
Re: Need Help Completing a Loan Table
Thanks for the input... The code you suggested still gave me a few bugs due to the other methods I defined but I finally got it worked out. I had to use the equation for computing the total payment on a loan.... Here's the working method if you just wanted to see how I fixed it
Code java:
//Generates loan table
public void generateLoanTable(double loanAmt) {
DecimalFormat df = new DecimalFormat("0.00");
System.out.print("\t");
for(int colLabel = 5; colLabel <= 30; colLabel += 5){
System.out.format("%11d", colLabel);
}
System.out.println("\n");
for(double rate = BEGIN_RATE; rate <= END_RATE; rate += RATE_INCR){
System.out.print(df.format(rate) + "% ");
//----------------
//FIXED CODE
//-----------------
for(int year = BEGIN_YEAR; year <= END_YEAR; year += YEAR_INCR){
loanAmt = getAmount() * (rate/100/MONTHS_IN_YEAR)
/
(1 - Math.pow(1/(1 + (rate/100/MONTHS_IN_YEAR)),
year*MONTHS_IN_YEAR))
*
year * MONTHS_IN_YEAR;
System.out.print(" " + df.format(loanAmt));
}
System.out.println();
}
System.out.println();
}
Re: Need Help Completing a Loan Table