Results 1 to 5 of 5
 02262011, 03:24 AM #1Member
 Join Date
 Feb 2011
 Posts
 25
 Rep Power
 0
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
Java 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++; } } }
Last edited by gaborn415; 02262011 at 03:25 AM. Reason: Found a better way to word my question
 02262011, 04:34 AM #2Senior Member
 Join Date
 Feb 2011
 Posts
 118
 Rep Power
 0
Here's an even easier solution. You already have a MortgageBalance class, so just implement its toString() method to output all of its data in one line. Then, in the while loop, instantiate a MortgageBalance with the appropriate values, then call System.out.println(mb.toString()).
 02262011, 04:58 AM #3Member
 Join Date
 Feb 2011
 Posts
 25
 Rep Power
 0
Thanks for your reply, I don't know what the toString() method is, and I'm not sure I understand the rest, what would be the appropriate values in the while loop? Would it be the same values I'm using here?
Java Code:MortgageBalance (interest1, principle1, payment1, loanRemainder1);
 02262011, 12:52 PM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
Java Code:class MyClass{ int x; public String toString(){ return "my classes to string method " + x; } }
Java Code:System.out.println(theItem);
You ask about arrays but I don't see where you intend to use one. Is the goal of this assignment to display every step of the payment until it is paid off?Last edited by sunde887; 02262011 at 02:01 PM.
 03042011, 03:51 AM #5Member
 Join Date
 Feb 2011
 Posts
 25
 Rep Power
 0
Sorry I've been so long away from this, between school, work and being very sick I've been sleeping a lot. Feeling better now and coming up on the final assignment. I hate to admit my ignorance here but I'm still confused about the toString method you posted. I'm going to reread it and see if I get a better understanding. Let me update you on what I changed and did for the new assignment. We had to use an array to store the variables of 3 different loans and display the monthly payments for each I did this as shown below...
Java Code:import java.text.*; public class MortageCalc { //Declare variables which make up loan double interestRate; double term; double loanAmount; //Constructor to create Mortgage instance public MortageCalc (double iR, double t, double lA) { interestRate = iR; term = t; loanAmount = lA; } //Method to determine Monthly payment double payment() { return loanAmount * ((interestRate/12) * (Math.pow((1+(interestRate/12)),(term * 12))))/(Math.pow((1+(interestRate/12)),(term * 12) )1); //Monthrate = loan * [c(1+c)^n]/[(1 + c)^n 1] Determine monthly rate } public static void main (String[] args) { //Declare variables double payment1; double payment2; double payment3; //Create array with loan variables double[][] loans = new double [3][3]; loans [0][0] = .0535; loans [0][1] = 7; loans [0][2] = 200000; loans [1][0] = .055; loans [1][1] = 15; loans [1][2] = 200000; loans [2][0] = .0575; loans [2][1] = 30; loans [2][2] = 200000; //Create new loan instances and calculate monthly payments MortageCalc m1 = new MortageCalc (loans [0][0], loans [0][1], loans [0][2]); MortageCalc m2 = new MortageCalc (loans [1][0], loans [1][1], loans [1][2]); MortageCalc m3 = new MortageCalc (loans [2][0], loans [2][1], loans [2][2]); payment1 = m1.payment(); payment2 = m2.payment(); payment3 = m3.payment(); DecimalFormat df = new DecimalFormat("#,###.##"); //Print payment results System.out.println("Monthly payment for loan #1 is $" + df.format(payment1)); System.out.println("Monthly payment for loan #2 is $" + df.format(payment2)); System.out.println("Monthly payment for loan #3 is $" + df.format(payment3)); } }
Similar Threads

Swapping elements of an array help please
By ikillu in forum New To JavaReplies: 11Last Post: 01152012, 08:49 PM 
Recursive method that checks if the first n elements of an array is sorted?
By matrixcool in forum New To JavaReplies: 8Last Post: 01152011, 09:39 PM 
incrementing array elements
By porchrat in forum New To JavaReplies: 2Last Post: 10062010, 02:37 PM 
sum of elements in array
By myst in forum New To JavaReplies: 7Last Post: 07172010, 08:36 AM 
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07242007, 05:33 PM
Bookmarks