Method for populating elements of an array
I'm new on this forum, and it looks like exactly what I'm looking for, so hi to all!
Ok, I've been looking for an answer to this question and I am perhaps not wording it right in my searches or I'm not understanding what I'm reading correctly, but if I have an array and I want to have the answers from a loop populate the array how would I go about this? I'll post the program and loop I'm looking at specifically below, however this is for a school assignment and I don't want anyone to do it for me, but I would like to understand how I can do what I want to do. If perhaps someone posts an example you can do it for some other program or in some different way so as not to do the program for me that's great, I just want to get clear that I'm not looking for anyone to do my work for me but I'd like to understand what I'm missing! ;)
Ok specific program is here
Code:
import java.text.*;
public class Mortgage2 {
double intrate, term, loan, monthint;
Mortgage2(double i,double t,double l,double mi)
{
intrate = i;
term = t;
loan = l;
monthint = mi;
}
//calculating monthly payments
double payment() {
return loan * (monthint * (Math.pow((1+monthint),term)))/(Math.pow((1+monthint),term)-1); //Monthrate = loan * [c(1+c)^n]/[(1 + c)^n -1] Determine monthly rate
}
public static class MortgageBalance {
double interest, principle, payment, loanRemainder;
MortgageBalance(double intr, double princ, double pay, double loanR)
{
interest = intr;
principle = princ;
payment = pay;
loanRemainder = loanR;
}
//Calculate interest payed per payment
double interest () {
return loanRemainder * (.0575/12);
}
//Calculate principle payed per payment
double principle () {
return payment - interest;
}
//Calculate loan remainder
double loanRemainder (){
return ((200000 * (1 + (.0575/12))) - payment);
}
//Calculate loan remainder after first run
double loanBalance (){
return ((loanRemainder * (1 + (.0575/12))) - payment);
}
}
public static void main(String[] args) {
double payment1;
double loan = 200000;
Mortgage2 mortgage1 = new Mortgage2 (.0575, 360, loan, .0575/12);
payment1 = mortgage1.payment();
System.out.println("Monthly Payment is" +" "+ "$" + payment1);
System.out.println("Total Loan is $200,000");
System.out.println("Interest rate is 5.75%");
System.out.println("Term is 30 years." );
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("Payment # Payment Interest Principle Balance");
double loanRemainder1;
loanRemainder1 = 200000 + (200000 * .0575/12) ;
double interest1;
interest1 = loanRemainder1 * (.0575/12);
double principle1 = payment1 - interest1;
int paymentCounter = 1;
while (loanRemainder1 > 0 && paymentCounter < 361)
{
Mortgage2 mortgage = new Mortgage2 (.0575, 360, loan, .0575/12);
MortgageBalance balance = new MortgageBalance (interest1, principle1, payment1, loanRemainder1);
loanRemainder1 = balance.loanBalance();
interest1 = balance.interest();
principle1 = balance.principle();
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(paymentCounter + " " + "$" + df.format(payment1) + " " + "$" + df.format(interest1) + " " + "$" + df.format(principle1) + " " + "$" + df.format(loanRemainder1));
paymentCounter++;
}
}
}
I know the math is wrong here, and there's something wrong with the loop, I'm rewriting the program anyways, however I initially hoped to have instead of the System.out.println function using each individual answer I wanted to have each answer, payment1, principle1, loanRemainder1, etc populate an array and then I could just print the array but I don't know how to code the answers to populate an array, can anyone point me in the right direction? I guess what I was looking for was to have the outputs of the methods, loanRemainder, principle, interest etc, populate an array. I appreciate the help!