Results 1 to 20 of 20

Thread: Help

  1. #1
    snoeberger is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default 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++ )
    {

    // display headers
    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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

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

    Quote Originally Posted by snoeberger View Post
    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. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    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. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    It's supposed to represent

  8. #8
    snoeberger is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    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. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by snoeberger View Post
    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. #10
    snoeberger is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.

    Quote Originally Posted by Junky View Post
    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. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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. #12
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    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. #13
    Zeramat is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Default

    Junky - the second smilie is a raised eyebrow. (Think Spock from Star Trek.) ((Sorry for the off topic post.))

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Zeramat View Post
    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. #16
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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. #17
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

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

    db

  19. #19
    snoeberger is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by JDScoot View Post
    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. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by snoeberger View Post
    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
  •