aspose file tools*
The moose likes Beginning Java and the fly likes Loop problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop problems" Watch "Loop problems" New topic
Author

Loop problems

David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
alright i have 2 loops. Im sure once i get the first loop correctly ill be able to figure the 2nd one out but this is for an assignment I had a while back which i never figured out. Well here it goes.
I've tried about 100 different ways to use the loop logic, a few which almost actually crashed my computer >.< but I'm gonna go to school for this so may as well figure it out.


Description: This lab is designed to exercise your knowledge of "for" loops and "while" loops. You are to write a method that calculates simple interest using a for loop and then overload the method with an argument and use a while loop to do the calculation.
Step 1 - Create a new project named "Lab6".
Step 2 - Add a class named "Formulas" to the project.
Step 3 - In the Formulas class add instance variables for startBalance, endBalance, years and rate.
Step 4 - Write a single constructor to initialize the instance variables and a set method for the years only.
Step 5 - Write a method named "simpleInterest" that uses a "for" loop to calculate the end balance. Simple interest is calculated by multiplying the balance times the rate and then adding that interest back to the balance. for example; $100 for the first year at 10% is $10 in interest. The new balance is $110. For the second year the interest on $110 at 10% is $11. The new balance is $121. For the third year the interest on $121 at 10% is $12.10. The new balance is $133.10. I think you see the pattern.
Step 6 - Write an overloaded simpleInterest method with an argument of integer. This is in case the user supplies the interest rate as a whole number like 10 instead of .1 but this time use a "while" loop to calculate the end balance instead of the "for" loop.
Step 7 - Write a display method to print the start balance, years, rate and end balance. See my output for the exact wording. You can use a printf instead of a println if you like.
Step 8 - Add a second class named TestFormulas to the project. In the main method use the following code:

Step 9 - Build and Run your program. The output should look like the following:
100.00 dollars for 1 year(s) at 10.00% has a future value of 110.00
100.00 dollars for 3 year(s) at 10.00% has a future value of 133.10
100.00 dollars for 3 year(s) at 15.00% has a future value of 152.09
100.00 dollars for 10 year(s) at 20.00% has a future value of 619.17
Step 10 - You need to zip the project and send it to me using the link at the top of this page. Right click your Lab6 folder, select "Send To", then select "compressed (zipped) folder". Change the name of the zip file to Lab6_Your_Name and then send it to me.



Output is as follows


110.0 dollars for 1 year(s) at 10.0% has a future value of 110.0
146.41 dollars for 3 year(s) at 10.0% has a future value of 146.41
146.41 dollars for 3 year(s) at 10.0% has a future value of 146.41
146.41 dollars for 10 year(s) at 10.0% has a future value of 146.41
BUILD SUCCESSFUL (total time: 0 seconds)
Which is incorrect

Thanks if anyone is able to help.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all welcome to the JavaRanch.

A couple of things which make you program behave weird:

Look at the loop on line 26. It does things you probably don't want it to do.
On line 44 you're reducing the rate to 1% of its previous value each. So in year two you have a rate of 1/10000 of its original value.
The rate on line 44 is hiding another variable with the name rate.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
well for the for loop i've tried
for(int val = 1; val >= years; ++val)
it's closer but it only uses 100$ over and over.
As for rate / 100 i'm trying to get the rate converted to a decimal value.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
For your loop at line 26, fall back to basics. Review the assignment's step 5:

Step 5 - Write a method named "simpleInterest" that uses a "for" loop to calculate the end balance. Simple interest is calculated by multiplying the balance times the rate and then adding that interest back to the balance. for example; $100 for the first year at 10% is $10 in interest. The new balance is $110. For the second year the interest on $110 at 10% is $11. The new balance is $121. For the third year the interest on $121 at 10% is $12.10. The new balance is $133.10. I think you see the pattern.


Do you see the pattern? It's something like:

newValue = startingValue + startingValue * interestRate

and then it repeats by substituting the startingValue with the newValue and calculating a new newValue. It's a simple pattern for a for loop. Using the same terms, the for loop would look something like:

After the loop executes the required number of times, startingValue (or newValue) gives the resulting simple interest.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18

that's what I think you mean. Maybe im looking at it wrong. But when I do that the output I need comes out wrong.


110.0 dollars for 1 year(s) at 10.0% has a future value of 110.0
146.41 dollars for 3 year(s) at 10.0% has a future value of 146.41
146.41 dollars for 3 year(s) at 10.0% has a future value of 146.41
146.41 dollars for 10 year(s) at 10.0% has a future value of 146.41
For now I'm ignoring the 2nd two because they deal with a different loop. But the starting value for all of them should be 100. For some reason my loop isn't running something right. I can't even use a calculator to find out what it's doing.
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
Would declaring startBalance as a final variable give me the desired results?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
You want the loop to execute a desired number of years, 1 to x. It doesn't matter what you call the loop index. i, val, whatever you want, though val might be confusing to someone else in this context. I'm not sure why your program gives the wrong results, but I can't see the whole thing to determine if you're introducing an error somewhere else.

Here's a working example of my approach. Vary the parameters sent to the method simpleInterest() to see that it works according to your assignment's example.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop problems
 
Similar Threads
Interesting exception errors
cannot resolve symbol
Thread question
Generating compound interest with integers
illegal start of type error