Results 1 to 3 of 3
- 03-27-2011, 06:44 PM #1
Member
- Join Date
- Mar 2011
- Location
- Chicago
- Posts
- 2
- Rep Power
- 0
A retierment Calculator with arrays to pass and three classes
Hi, i am struggling with this assignment . I have to create a retierment calculator that keep track of x amount of years in x.length of array for each index in the array it keeps the info. and the index after the one before uses the results of the one before as a starting point so i have a constructor and my main to test this but i have no idea where to go now and why my for loop is not working
public class RetiermentCalculator
{
AccountInformation[] years = new AccountInformation[2];
public RetiermentCalculator(int aYear, double aBeginIncome, double aContribution, double aReturn )
{
double answer = 0.0;
for (int index = 0; index < years.length; index ++)
{
double finance = (aBeginIncome + aContribution) ;
double intrestMade = finance * aReturn;
answer = finance + intrestMade;
}
System.out.println(answer);
}
}
- 03-27-2011, 07:28 PM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Since you want your answer variable to be used by every instance, make it static. Also, initalize all of your variables outside the for loop. Last but not least, make a method for the for loop.
From what you're telling me, it would look like this:
Fill in the "blabla" and "parameters". If you get any errors let me know, I didn't test this.Java Code:public class RetirementCalculator{ static double answer; RetirementCalculator[] years = new RetirementCalculator[2]; public RetirementCalculator(parameters){ double finance=blabla; double interestMade=blabla; } years[0] = new RetirementCalculator(parameters); years[1] = new RetirementCalculator(parameters); public static String sumOfAnswers(RetirementCalculator[] info){ for (RetirementCalculator c : info) { answer+= c.finance+c.interestMade; } return answer; } System.out.println(sumOfAnswers(years)); }Last edited by Solarsonic; 03-27-2011 at 07:48 PM.
- 03-28-2011, 04:15 PM #3
Member
- Join Date
- Mar 2011
- Location
- Chicago
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
how to pass user input into multi-dimensional arrays
By ziongio in forum New To JavaReplies: 4Last Post: 03-09-2011, 05:54 AM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Reading arrays from different classes without getters and setters
By Psyclone in forum New To JavaReplies: 7Last Post: 02-02-2010, 11:01 AM -
methods, classes, arrays.. oh my!
By katalyst in forum New To JavaReplies: 30Last Post: 03-30-2009, 12:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks