PDA

View Full Version : Arrays and loops


Stephandeye
06-25-2009, 08:26 AM
I am trying to create a calculator that will calculate the monthly payments for 3 different loans mortgage loans

My question is how do I increment an array element using a loop. It would be very easy to do seperate calculations for each element but (in my mind, and maybe I am wrong) that defeats the purpose of using an array.

Also, yes this is homework. I know how sensitive the issue of homework can be, however, I am not asking anyone to give me any answers or do this for me. I DO want to learn and am only asking for a point in the right direction. I don't have to use arrays or loops but I am trying to be ambtious and combine several of the concepts from recent chapters into one program.

Here is what I have so far:

/*
File name: Week4
Program that calculates and displays monthly payment amount 3 different loans
Loan 1: 7 years at 5.35%
Loan 2: 15 years at 5.5%
Loan 3: 30 years at 5.75%
Program uses the following formula to calculate payments
[dollars * (1 + [tax/100]) ^ (years * 12) x tax / 100] / [(1 + (tax / 100)) ^ (years * 12) - 1]
*/

import java.text.DecimalFormat;

public class Week4
{
public static void main(String [] args)
{
//declare and construct arrays and variables
double [] rate = {5.35, 5.5, 5.75};
int [] years = {7, 15,30};

int dollars = 200000;
double tax;
int month;
double tax_rate, tax_num, month_tax, monthly_pay;

DecimalFormat twoDigits = new DecimalFormat("$###,###.00");

System.out.println();
System.out.println("\t\tMonthly Mortgage Payment Calculator");
System.out.println("\n");

//I want the loop to start here and increment the array index number so that I do not need to replicate the code 3x (once for each loan)

// loan 1
tax = (rate [0] / 100) / 12;
tax_rate = tax + 1;
month = years [0] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 1");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [0]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [0]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");

//loop should end here when all three array elements have been calculated and result printed the rest of the code would them be obsolete

// loan 2
tax = (rate [1] / 100) / 12;
tax_rate = tax + 1;
month = years [1] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 2");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [1]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [1]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");

// loan 3
tax = (rate [2] / 100) / 12;
tax_rate = tax + 1;
month = years [2] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 3");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [2]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [2]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");
}
}


Here is what I have tried:

declare variable int i=0

while (i<=3)
{code for loop
++rate[];} <--------Creates an infite loop

or

for (int i= 0; i<3; i++)
{
rate [i] = rate [i] + 1;
code fpr loop } <------- Increments the value of the array not the element

Trust me I have tried much much much much more but this is the closest I can get.

Once again, I do not want any one to give me any answers only point me n the right direction. I have plenty of time to figure this out on my own once I am no longer backed into this corner.

Any advice would be greatly appreciated.

Fou-Lu
06-25-2009, 10:01 AM
I am trying to create a calculator that will calculate the monthly payments for 3 different loans mortgage loans

My question is how do I increment an array element using a loop. It would be very easy to do seperate calculations for each element but (in my mind, and maybe I am wrong) that defeats the purpose of using an array.

Also, yes this is homework. I know how sensitive the issue of homework can be, however, I am not asking anyone to give me any answers or do this for me. I DO want to learn and am only asking for a point in the right direction. I don't have to use arrays or loops but I am trying to be ambtious and combine several of the concepts from recent chapters into one program.

Here is what I have so far:

/*
File name: Week4
Program that calculates and displays monthly payment amount 3 different loans
Loan 1: 7 years at 5.35%
Loan 2: 15 years at 5.5%
Loan 3: 30 years at 5.75%
Program uses the following formula to calculate payments
[dollars * (1 + [tax/100]) ^ (years * 12) x tax / 100] / [(1 + (tax / 100)) ^ (years * 12) - 1]
*/

import java.text.DecimalFormat;

public class Week4
{
public static void main(String [] args)
{
//declare and construct arrays and variables
double [] rate = {5.35, 5.5, 5.75};
int [] years = {7, 15,30};

int dollars = 200000;
double tax;
int month;
double tax_rate, tax_num, month_tax, monthly_pay;

DecimalFormat twoDigits = new DecimalFormat("$###,###.00");

System.out.println();
System.out.println("\t\tMonthly Mortgage Payment Calculator");
System.out.println("\n");

//I want the loop to start here and increment the array index number so that I do not need to replicate the code 3x (once for each loan)

// loan 1
tax = (rate [0] / 100) / 12;
tax_rate = tax + 1;
month = years [0] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 1");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [0]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [0]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");

//loop should end here when all three array elements have been calculated and result printed the rest of the code would them be obsolete

// loan 2
tax = (rate [1] / 100) / 12;
tax_rate = tax + 1;
month = years [1] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 2");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [1]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [1]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");

// loan 3
tax = (rate [2] / 100) / 12;
tax_rate = tax + 1;
month = years [2] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);

System.out.println("\t\t\t\tLoan 3");
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [2]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [2]) + "%");

System.out.printf("\tThe Monthly Mortgage Payment for this Loan is: " + twoDigits.format(monthly_pay));
System.out.println("\n");
}
}


Here is what I have tried:

declare variable int i=0

while (i<=3)
{code for loop
++rate[];} <--------Creates an infite loop

or

for (int i= 0; i<3; i++)
{
rate [i] = rate [i] + 1;
code fpr loop } <------- Increments the value of the array not the element
Trust me I have tried much much much much more but this is the closest I can get.

Once again, I do not want any one to give me any answers only point me n the right direction. I have plenty of time to figure this out on my own once I am no longer backed into this corner.

Any advice would be greatly appreciated.


Using a for loop is the easiest for arrays in pretty much every language:

int iArrayLength = rate.length;
for (int i = 0; i < iArrayLength; ++i)
{
// Do stuff. No need to perform any increment since the ++i takes care of that
// as a post condition for the loop
}


In other words, you're code is (mostly) correct for you're for loop. I don't recommend 'magic numbers' or numbers that are controlled with a constant (the 3 in this case), rather to prevent try/catch calls I recommend using the length attribute to determine the max.
Then, you just adjust you're current code to reflect the iteration of the array with the 'i' instead of using a constant: 0, 1, 2.
That would be these guys:

tax = (rate [0] / 100) / 12;
// Would become
tax = (rate[i] / 100) / 12;

Where i is interpreted as the value of the current iteration.

Does that answer what you're looking for? Just keep in mind that a foreach (new to java, represented with for (<? extends Object>obj : objectArray)) is always incremented on each loop, a for loop is usually incremented (the ++i), but may not be: for (int i = 0; i < iLength; ;), and a while loop always needs a control added in (sentinal loop if I recall my old classes?).