0

So in my intro to java book, I was tasked with this:

Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In one year, the tuition will be $10,500. Write a program that computes the tuition in ten years and the total cost of four years' worth of tuition after the tenth year.

I can calculate the tenth year tuition easily enough, but what has me stumped is how to add the unique tuition values at years 11, 12, 13 and 14. If I'm right, it should add up to 73717.764259. What my code is giving me is 158394.52795515177. As code goes, It may just be that i'm thinking about this in the wrong way and that my code did the addition correctly, but I think I'm more right here. Here's the code i'm using now:

    double initialTuition = 10000;
    final double theRate = 0.05;

    for (int i = 1; i < 15; i++) {
        initialTuition = ((theRate * initialTuition) + initialTuition);
        System.out.println("Year " + i + " tuition is: " + initialTuition);
        while (i == 10){
            System.out.println("Year " + i + " tuition is: " + initialTuition);
            double startOfFourYearTuition = initialTuition;
            System.out.println(startOfFourYearTuition);
            break;
        }

        while ((i > 10) && (i < 15)) {
            System.out.println("Year " + i + " tuition is: " + initialTuition);
            initialTuition += initialTuition;
            break;
        }

    }

The last while loop is my attempt at adding the 4 years.

To reiterate the question, How could I pull out the unique values of initialTuition at iterations 11 to 14 and add them?

2
  • Usually when you need to calculate a total you create a separate variable and initialize it as zero (double sum = 0;) and then in some loop you add current values to the total (sum += thisYearTution;).
    – PM 77-1
    Feb 15, 2015 at 21:07
  • might find it easier to use if rather than while if you only want something to happen once. You have while (i == 10) rather than if (i == 10) Feb 15, 2015 at 21:08

2 Answers 2

1

You also have a lot of gratuitous code in there, while loops inside of for loops, etc. Try this on for size:

public static void main(String[] args) 
{
    double initialTuition = 10000;
    double summarizedTuition = 0;

    final double theRate = 0.05;

    for (int i = 1; i < 15; i++) {
        initialTuition = ((theRate * initialTuition) + initialTuition);
        System.out.println("Year " + i + " tuition is: " + initialTuition);

        if ((i > 10) && (i < 15)) 
        {
            summarizedTuition += initialTuition;
        }
    }

    System.out.println("Summarized tuition for years 11 - 14: " + summarizedTuition);
}

The output is:

Year 1 tuition is: 10500.0
Year 2 tuition is: 11025.0
Year 3 tuition is: 11576.25
Year 4 tuition is: 12155.0625
Year 5 tuition is: 12762.815625
Year 6 tuition is: 13400.95640625
Year 7 tuition is: 14071.0042265625
Year 8 tuition is: 14774.554437890625
Year 9 tuition is: 15513.282159785156
Year 10 tuition is: 16288.946267774414
Year 11 tuition is: 17103.393581163135
Year 12 tuition is: 17958.56326022129
Year 13 tuition is: 18856.491423232354
Year 14 tuition is: 19799.31599439397
Summarized tuition for years 11 - 14: 73717.76425901074
2
  • So use of the variable summarizedTuition, as you use it here, will take the value of initialTuition in a given iteration 'i' of the loop between 10 and 15, add it to the value of summarizedTuition, and keep doing this until (i<15) evaluates to false. Correct? Feb 15, 2015 at 23:16
  • So summarizedTuition is initialized to 0. On iterations 11, 12, 13, and 14 the value of initialTuition would be added to the current value of summarizedTuition. So on iteration 11 you get (0 + 17103.393581163135), on 12 you get (17103.393581163135 + 17958.56326022129), etc.
    – Michael
    Feb 15, 2015 at 23:19
0
        double theRate = 1.05; // rate of increase +5%
        double tuitionCost = 10000; // base cost
        double fourYearCost = 0; // four year cost (years 10-13)
        for (int i = 0; i <= 13; i++) {
            System.out.println("Cost of year: " + i + " is $" + tuitionCost); // print out this years cost
            if(i >= 10){ //if its year 10, 11, 12, 13
                fourYearCost += tuitionCost; // add this years tuition cost
            }
            tuitionCost *= theRate; // increment the tuition (mutiply it by 1.05, or 5% increase)
        }

        System.out.println("Cost of tuition for years 10, 11, 12, 13: $" + fourYearCost); // print the cost for the remaining years

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.