Reputation: 11
My java program is suppose to take in hard-coded loan information and produce an output that shows payments made, balance, payment amount, interest paid, principal application, and new balance all through a period of 1-60 days. I put comments in my code below in the areas that I need help with. Also, my output I have now isn't formatted neatly. Any help would be very appreciated.
public static void main(String[] args) {
// TODO Auto-generated method stub
double loanAmount = 25000.00;
double annualInt = 0.0525;
double numberOfMonths = 60.00;
double monthlyPayment;
double rate;
double balance;
double intPaid=0;
double prAppl=0;
int paymentsMade;
System.out.println("Principal : "+loanAmount);
System.out.println("Annual Int: "+annualInt*100+"%");
System.out.println("Term (mo) : "+numberOfMonths);
paymentsMade = numOfPaymentsMade(numberOfMonths);
rate = calcMonthlyInterestRate(annualInt);
monthlyPayment = calcMonthlyPayment(rate,loanAmount,numberOfMonths);
balance = calcBalance(loanAmount,rate,numberOfMonths,monthlyPayment);
intPaid = interestPaid(rate,balance,intPaid);
prAppl = prApp(monthlyPayment,intPaid,prAppl);
displayResults(numberOfMonths,rate,loanAmount,paymentsMade,balance,intPaid,monthlyPayment,prAppl);
}
public static int numOfPaymentsMade(double numberOfMonths)
{
int paymentsMade=0;
for(paymentsMade=1; paymentsMade<numberOfMonths+1; paymentsMade++)
{
System.out.println(paymentsMade);
}
return paymentsMade;
}
//--------------------------------------------------------------------------------------------------------
public static double calcMonthlyInterestRate(double annualInterestRate){
double monthlyInterestRate;
monthlyInterestRate = (annualInterestRate/12);
return monthlyInterestRate;
}
//--------------------------------------------------------------------------------------------------------
public static double calcMonthlyPayment(double rate, double loanAmount, double numberOfMonths )
{
double monthlyPayment;
int i;
monthlyPayment = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));
for(i=0; i<60; i++)
{
System.out.println(monthlyPayment);
}
return monthlyPayment;
}
//--------------------------------------------------------------------------------------------------------
public static double calcBalance(double loanAmount, double rate, double numberOfMonths, double monthlyPayment)
{
double balance=0;
// this method needs fixing
for(balance=loanAmount; balance<monthlyPayment-1; balance--){
balance = (loanAmount-monthlyPayment)+(balance*rate);
}
return balance;
}
//--------------------------------------------------------------------------------------------------------
public static double interestPaid(double rate, double balance, double intPaid)
{
// need a loop corresponding with the balance
intPaid=balance*rate;
return intPaid;
}
//--------------------------------------------------------------------------------------------------------
public static double prApp(double monthlyPayment, double intPaid, double prAppl)
{
// need a loop corresponding with intPaid
prAppl=monthlyPayment-intPaid;
return prAppl;
}
//--------------------------------------------------------------------------------------------------------
public static void displayResults (double numberOfMonths, double rate, double loanAmount, double paymentsMade, double balance, double intPaid, double monthlyPayment, double prAppl){
double monthPay;
monthPay = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));
System.out.println("Payment :"+monthPay);
System.out.println("");
System.out.println("Pmt Balance Payment Int Pd Prin Appl New Bal");
System.out.println("--- ---------- --------- -------- --------- ----------");
System.out.println(paymentsMade+" "+balance+" "+monthlyPayment+" "+intPaid+" "+prAppl);
//maybe a method should be added for the new balance section
//how do I add all of the computations up?
} }
Upvotes: 0
Views: 59
Reputation: 31699
There are three problems with this loop:
for(balance=loanAmount; balance<monthlyPayment-1; balance--){
balance = (loanAmount-monthlyPayment)+(balance*rate);
}
First of all, balance--
subtracts 1 from the balance. There is no reason you would want to do that (well, if the mortgagee is a relative of the banker, gratuitously reducing their balance a little each month would be a nice small gift, but otherwise you wouldn't want to do that). The vast majority of for
loops involve indexes that you increase by 1 or decrease by 1 each time through the loop. But that's not the case here, so you don't want balance--
. The body of the loop is supposed to reduce the balance, so you don't need anything at all for the third part of the loop. You could simply write:
for (balance = loanAmount; balance < monthlyPayment - 1; ) {
But I'd tend to use a while loop instead. The above is equivalent to:
balance = loanAmount;
while (balance < monthlyPayment - 1) { ... }
which means the loop will keep going as long as the balance is below the monthly payment.
Which is the second problem. You want the loop to keep going as long as the balance is above the monthly payment, and stop when it drops below. That means that the condition in your for
loop is backwards (same as it is in my while
loop example). You probably want
for (balance = loanAmount; balance >= monthlyPayment - 1; ) {
The third problem is that the formula in the body is wrong. You want to determine how much of the payment is principal, and reduce the balance by that amount. Your loop body almost does this, but there's a big error. I'll let you figure this one out, once you fix the problems with the loop header.
Upvotes: 1