Results 1 to 3 of 3
  1. #1
    fonzi is offline Member
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    2
    Rep Power
    0

    Default 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);
    }



    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by fonzi View Post
    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];
    years[0] = new Account5, 7.0, 8.0, 1.0
    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);
    }



    }
    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:

    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));
    }
    Fill in the "blabla" and "parameters". If you get any errors let me know, I didn't test this.
    Last edited by Solarsonic; 03-27-2011 at 08:48 PM.

  3. #3
    fonzi is offline Member
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    2
    Rep Power
    0

    Default

    thank you very much , this works so well for me i needed to use += and declared all the variables outside of the for loop :D thank you again you helped me a lot

Similar Threads

  1. Replies: 4
    Last Post: 03-09-2011, 06:54 AM
  2. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Replies: 7
    Last Post: 02-02-2010, 12:01 PM
  5. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 01:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •