1. Member
Join Date
Jun 2011
Posts
4
Rep Power
0

## Help

Modify the compound-interest program of to repeat its steps for interest rates of 5%, 6%, 7%, 8%, 9%, and 10%. Use a for loop to vary the interest rate. I'm doing something wrong, just don't know what. Any help is appreciated.

public class p416
{
public static void main( String args[] )
{
double amount; // amount on deposit at end of each year
double principal = 1000.0; // initial amount before interest

for ( int rate = 5; rate <= 10; rate++ )
{

System.out.printf( "%s%20s\n", "Year", "Amount on deposit" );

// calculate amount on deposit for each of ten years
for ( int year = 1; year <= 10; year++ )
{
// calculate new amount for specified year
amount = principal * Math.pow(1 + (rate / 100), year );

// display the year and the amount
System.out.printf( "%4d%,20.2f\n", year, amount );
} // end for
}
} // end main
} // end class Interest

2. Ahhh yes. The ol' "dump your code and expect someone else to fix it" trick.

Originally Posted by snoeberger
I'm doing something wrong
That provides ZERO information. The only response possible is "Then do something right instead".

Do you get errors? Then post the full and exact error messages and indicate on which line they occur.

Do you get incorrect output? Then post the actual output and the expected output.

3. Originally Posted by Junky
The only response possible is "Then do something right instead".
Huh? I can think of several responses. like:

Or, the day that my boss admits to doing something wrong:

4. OK so there are plenty of responses, such as:

Then get a pony.
You probably need to hop on one leg while sticking your tongue out.
I think lime jelly is best.

By the way what is that second smilie supposed to be?

5. Originally Posted by Junky
OK so there are plenty of responses, such as:

Then get a pony.
You probably need to hop on one leg while sticking your tongue out.
I think lime jelly is best.
You forgot the classic "Would you like fries with that?"

By the way what is that second smilie supposed to be?

6. I can see it Daryl, I was just wondering what it is supposed to represent.

7. It's supposed to represent

8. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
IF I just wanted a answer to this issue, I could just copy one off the internet. There are plenty out there to this answer. You dump it into an editor and its intuitively easy to see the error. I wasn't looking for answer, but an explanation to an issue. I would say thanks, but its just trolling...

9. Originally Posted by snoeberger
You dump it into an editor
Why should I? It is your problem, the onus is upion you to fully explain what your problem is. I am offering my time free of charge. I don't have to try and resolve the problem you do and if you want someone here to help you then make it as easy as possible for them to do so.

but an explanation to an issue.
I and many others are only to happy to provide that explanation as soon as you tell us what your problem is.

Do you call you doctor and say "I'm sick" and expect them to cure you?
Do you call your mechanic and say "My car won't start" and expect them to fix it?

10. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
Originally Posted by Junky
Why should I? It is your problem, the onus is upion you to fully explain what your problem is. I am offering my time free of charge. I don't have to try and resolve the problem you do and if you want someone here to help you then make it as easy as possible for them to do so.
Apparently you feel your time is worth trolling forums, so why would you think I don't think you have a 30 seconds to cut and paste code, because you apparently don't mind wasting time.

Originally Posted by Junky
Do you call you doctor and say "I'm sick" and expect them to cure you?
Do you call your mechanic and say "My car won't start" and expect them to fix it?
Some things are obvious, if I walk into the doctors office with a knife sticking into my chest. He doesn't spend time asking me what the problem, he actually does something productive instead of wasting time.

11. I am not trolling. I am offering advice on how you can maximise you chances of getting help. If you want help then you would follow that advice. Instead you are just bitchin' and whinin' about my attitude. That is a sure way to ensure you get no help. Not very smart!

12. Member
Join Date
Jun 2011
Location
San Diego, CA
Posts
24
Rep Power
0
Ok I think I know what the issue is. You are using the "rate" variable from the for loop to be divided by 100. dividing 5/100 will give you .05 which will not fit into an int. It will be truncated to 0 every time.

Just declare a new double variable, and inside of the first for loop, set the new double variable say doubleRate = (double) rate; as follows:

Java Code:
```        double amount; // amount on deposit at end of each year
double principal = 1000.0; // initial amount before interest
double doubleRate;

for ( int rate = 5; rate <= 10; rate++ )
{
doubleRate = (double) rate;```
And then replace all of your rate variables with doubleRate.

Java Code:
`amount = principal * Math.pow(1 + (doubleRate / 100), year );`
and you should be good to go.

13. Member
Join Date
Jun 2011
Posts
5
Rep Power
0
Junky - the second smilie is a raised eyebrow. (Think Spock from Star Trek.) ((Sorry for the off topic post.))

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
It's not so much "not fitting" as performing integer division which is a different operation from its floating point cousin. (Same symbol, different operation)

You can get the same effect by using a floating point literal in place of "100".

Java Code:
`rate / 100.0`
@OP: The advice that you actually describe the problem was both good and well intentioned. You are a programer now and the code is what you make it. Whenever you encounter a problem you play the parts of patient and doctor. With more experience "integer division" may well appear to you as straight forward as a knife sticking out of the patient's chest. For now it (evidently) isn't, so you must rely on what the rest of us rely on - the ability to describe a problem: to ourselves in the first instance and, if need be, to others.

15. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Originally Posted by Zeramat
Junky - the second smilie is a raised eyebrow. (Think Spock from Star Trek.) ((Sorry for the off topic post.))
There are emoticons for Vulcans?

16. Member
Join Date
Jun 2011
Location
San Diego, CA
Posts
24
Rep Power
0
Originally Posted by pbrockway2
It's not so much "not fitting" as performing integer division which is a different operation from its floating point cousin. (Same symbol, different operation)

You can get the same effect by using a floating point literal in place of "100".

Java Code:
`rate / 100.0`
Good point, I didn't think of that. I'm still quite new myself.

17. Here is a website that will help you in your journeys of support from other programmers. Seeing as when we volunteer our time to help others out we are essentially using up the time we could be out doing other things like hopscotch or that really cool yo-yo thing. Some people here spend hours in the forums for kicks, but others only come on for a short while at a time. Instead of trying to recreate your problem, which will take up more time than one would think, people will often move on because of lack of information. To resolve this problem I suggest using an SSCCE, see the below link for more information.

Short, Self Contained, Correct Example

18. A more relevant link is How to ask questions the smart way.

db

19. Member
Join Date
Jun 2011
Posts
4
Rep Power
0
Originally Posted by JDScoot
Ok I think I know what the issue is. You are using the "rate" variable from the for loop to be divided by 100. dividing 5/100 will give you .05 which will not fit into an int. It will be truncated to 0 every time.
So the int will always always round to a whole number? I guess I need to read up on more about the types. It does make sense, x multiplied by 0 is always 0. Thus that's why I wasn't seeing the interest calculation. Thanks for your help.

@Darrly and Junky, its not what you said, but how you say it. What you say is true, but its all in the presentation.

20. Originally Posted by snoeberger
So the int will always always round to a whole number?
If doing int division, yes.

I guess I need to read up on more about the types.
Always a great idea.

@Darrly and Junky, its not what you said, but how you say it. What you say is true, but its all in the presentation.
Here I think it is more in the interpretation. I've seen nothing rude, non-helpful or troll-like in Junky's posts, but I see you taking it that way for some reason.

Much luck!

#### Posting Permissions

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