programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Yet another loop problem

Greenhorn
Posts: 13
Hi All,

I'm having an issue with a program that computes the cost of college tuition if you start 10 years from now and the increase in tuition is 5% Year over Year. I'm having difficulty with the second loop adding the proper values to get the total four year cost. I'm attaching my code. Right now it's just adding the first years tuition 4 times. Any help would be appreciated.

Bartender
Posts: 1812
12
I ran your app, and I need clarification. Is it supposed to compound every year and the total for four years is of the original price plus ALL the interest?

Janeice

Janeice DelVecchio
Bartender
Posts: 1812
12
Got it! I made some modifications.... you don't need the "for" loop when it's inside the "if == 2018" loop. It's only running that loop four times with that 2018 total. What you need is a separate if statement, because you want to run the addition for every time it's above 2018....

Mitch Kazlo
Greenhorn
Posts: 13
There were 2 parts of the problem. The first was to print out what the tuition would be for every year starting in 2009, initial tuition is 10K and it increases by 5 % every year, then print that cost for every year out (This part is working fine).

The second part is to calculate the cost of 4 years of tuition beginning 10 years from now in 2019 (2019 - 2022). Then print out what the total cost is for a student starting college in 2019. The cost should be the tuition's from year 2019 - 2022 added together but I keep getting the wrong result. What I'm getting now is the cost of year 2019 * 4, I think I need another variable or something.

Janeice DelVecchio
Bartender
Posts: 1812
12
You don't need another variable.... your loop logic is off. I have your solution.... do you want me to help you figure it out, or just give you the answer?

Mitch Kazlo
Greenhorn
Posts: 13
I think I just got it to work, this gives me what I want... I removed the loop and I have a long boolean to add the years up. I'm wondering if there is a cleaner (more efficient) way to do this without the long boolean I have written in my code.

Janeice DelVecchio
Bartender
Posts: 1812
12
Nice work.... you should add the increase AFTER you add the four year total..... do you know why?

Check this out....

Mitch Kazlo
Greenhorn
Posts: 13
Well I know that it is still in the loop in that position and is increased after it is added to fourYearCost. My solution gave me too high a number but I don't know why. I ended up with 70207.39 and the correct value is 66864.19 which is what your solution provides. Can you point me to what was happening that was incorrect with my solution?

Janeice DelVecchio
Bartender
Posts: 1812
12
If you add the increase to the tuition BEFORE you calculate your four year total, you're adding NEXT years price to the total.

This is why you wait to increment the tuition AFTER the fourYearTotal is calculated.

See your line #18 vs my line #25

Mitch Kazlo
Greenhorn
Posts: 13
Great. Thanks for all the help. I'll have to watch out for that and remember to adjust values at the end of the loop.

Janeice DelVecchio
Bartender
Posts: 1812
12
My total is 70207..... hold on

70207 is the correct answer. I added it on a calculator. Here's my output:

C:\programs\java>java FutureTuition
Year Tuition
----------------
2009 10000.00
2010 10500.00
2011 11025.00
2012 11576.25
2013 12155.06
2014 12762.82
2015 13400.96
2016 14071.00
2017 14774.55
2018 15513.28
----------------
2019 16288.95
2020 17103.39
2021 17958.56
2022 18856.49
----------------
The cost of 4 years of tuition starting in 2019 is 70207.39

16288.95 + 17103.39 + 17958.56 + 18856.49 = 70207.39

Mitch Kazlo
Greenhorn
Posts: 13
You are correct. I adjusted the year variable / loop control to get it right..

Thanks again for all your help. This one was certainly frustrating me.. I'm new to all of this so I'm hoping the more I write these programs the better I get, especially when I can get some help along the way!

Janeice DelVecchio
Bartender
Posts: 1812
12
I ran your app. It's getting the correct answer, but has the wrong output.

WAIT A MINUTE

Why are you starting your loop at 1??

mine is too, because I copied and pasted it, but mine says 1 and 15.

YOUR app starts at 1 and loops til 14, only going through 13 times. Count them. That's why your output (and your calculator total) is incorrect.

Marshal
Posts: 57437
175
What's a long boolean? I have never heard of it. I can see neither a long nor a boolean value in your code (but you have at least 3 boolean expressions).

Janeice DelVecchio
Bartender
Posts: 1812
12
He meant long as in extended I think. :-)

Campbell Ritchie
Marshal
Posts: 57437
175
I have never heard of an extended boolean either.

Janeice DelVecchio
Bartender
Posts: 1812
12
I mean I think he meant it was a long statement..... not like it was a "long" or a "boolean" as in data types....

...just like it was large, spacewise and it had boolean statements in it. I don't think he was giving it an official name.

You speak English, too, right? Not just Java?

Campbell Ritchie
Marshal
Posts: 57437
175
You mean a boolean expression? This isn't English, but Java. If you don't use the correct jargon, how can you expect anybody to understand you?

 Ruth Stout was famous for gardening naked. Just like this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS