Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Total beginner

Hi,
I am working on a project for school and I have it mostly done but there is one part I can't figure out. This is the assignment:

Modify the mortgage program for week 2 to display 3 mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. Use an array to hold the three interest rates and an array for the three loan terms. Display the mortgage payment amount for each loan. Do not use a graphical user interface. Insert comments in the program to document the program.
I know, I think everyone in a Java I class has this assignment. The problem is that instead of giving me just values 7 years-5.35, 15 years-5.5, and 30 years-5.75, which is three statements I am getting all the interest rate for all the years-ex: 7 years-5.35, 5.5 and 5.75 and so on. Anyway this is my code-hope it is readable. Help please. :o

import java.text.NumberFormat;

public class MortgageCalculator2 {
public static void main(String args[]) {

//Declare all variables.
double payment;
double principal = 200000;
double[] rate = {5.35, 5.5, 5.75};
double monthlyRate;
int[] years = {7, 15, 30};
int months;

NumberFormat currency = NumberFormat.getCurrencyInstance(); /*This
is so payment can be converted to currency format*/

for (int y = 0; y < years.length; y++) {
for (int r = 0; r < rate.length; r++) {

monthlyRate = (rate[r] / 1200);
months = (years[y] * 12);

//Calculate monthly payment
payment = principal *(monthlyRate / (1 - Math.pow(1 + monthlyRate, - months)));

//Display loan and payment information
System.out.println("The borrowed amount is " + currency.format(principal) + ".");
System.out.println("The interest rate is " + rate[r] + "%.");
System.out.println("The term is " + years[y] + " years.");
System.out.println("The monthly payment is " + currency.format(payment) + ".");
System.out.println();

}
}


}
}
I know I am missing something but what???
[2194 byte] By [cvt] at [2007-11-11 7:06:38]
# 1 Re: Total beginner
Could you post the output from your program? A picture is worth a 1000 words!

The problem is probaby with your nested loops. You are doing everything inside the inner loop. Should some of it be in the outer loop?
Norm at 2007-11-11 22:39:22 >
# 2 Re: Total beginner
Hello,
Couldn't do this over the weekend-computer problems. Thank you in advance for any help you can give me. Here is the output I am getting:

The borrowed amount is $200,000.
The interest rate is 5.35%.
The term is 7 years.
The monthly payment is $2,859.79.

The borrowed amount is $200,000.
The interest rate is 5.5%.
The term is 7 years.
The monthly payment is $2,874.01.

The borrowed amount is $200,000.
The interest rate is 5.75%.
The term is 7 years.
The monthly payment is $2,897.80.

The borrowed amount is $200,000.
The interest rate is 5.35%.
The term is 15 years.
The monthly payment is $1,618.29.

The borrowed amount is $200,000.
The interest rate is 5.5%.
The term is 15 years.
The monthly payment is $1,634.17.

The borrowed amount is $200,000.
The interest rate is 5.75%.
The term is 15 years.
The monthly payment is $1,660.82.

The borrowed amount is $200,000.
The interest rate is 5.35%.
The term is 30 years.
The monthly payment is $1,116.83.

The borrowed amount is $200,000.
The interest rate is 5.5%.
The term is 30 years.
The monthly payment is $1,135.58.

The borrowed amount is $200,000.
The interest rate is 5.75%.
The term is 30 years.
The monthly payment is $1,167.15.

I really only need 3 statements-like this, but can't figure it out how to make it happen.

The borrowed amount is $200,000.
The interest rate is 5.35%.
The term is 7 years.
The monthly payment is $2,859.79.

The borrowed amount is $200,000.
The interest rate is 5.5%.
The term is 15 years.
The monthly payment is $1,634.17.

The borrowed amount is $200,000.
The interest rate is 5.75%.
The term is 30 years.
The monthly payment is $1,167.15.
cvt at 2007-11-11 22:40:22 >
# 3 Re: Total beginner
You are right! I took the print statements and put them outside the loops. I just made one for each element. Thanks. :WAVE:
cvt at 2007-11-11 22:41:19 >