Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By JosAH

Thread: Trouble with increments

  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Trouble with increments

    Hello,

    I was wondering if anyone could give me a hand with one of homework assignments. The object of the program is:

    Write a program using a for loop to calculate how high a ball bounces on each of its first 20 bounces. The ball starts at 10 meters above the bounce surface. Each bounce brings the ball to 4/5 of its previous height. Thus, the first bounce it will reach 8 meters. Print the 20 heights, labeled such as: (Ignore the strange precision of some output values.)

    After bounce 1 the ball reaches 8.0 meters
    After bounce 2 the ball reaches 6.4 meters
    After bounce 3 the ball reaches 5.120000000000001 meters
    After bounce 4 the ball reaches 4.096000000000001 meters


    This is as far as I have been able to get:

    public class Bouncy_Ball {


    public static void main(String[] args) {

    bounce();

    }
    public static void bounce() {
    for (int i=1; i<=20; ++i){
    System.out.print("After bounce " + i + " the ball reaches ");
    for (double j=10; j>0; j--){
    System.out.println(j-j*.2+ "meters.");
    }
    }

    }
    }

    Example of my output:

    After bounce 1 the ball reaches 8.0meters.
    7.2meters.
    6.4meters.
    5.6meters.
    4.8meters.
    4.0meters.
    3.2meters.
    2.4meters.
    1.6meters.
    0.8meters.
    After bounce 2 the ball reaches 8.0meters.
    7.2meters.
    6.4meters.
    5.6meters.
    4.8meters.
    4.0meters.
    3.2meters.
    2.4meters.
    1.6meters.
    0.8meters.
    After bounce 3 the ball reaches 8.0meters.
    7.2meters.
    6.4meters..... and so on to bounce number 20.

    Is this an easy fix or am I way off the mark here? I have spent quite a bit of time on this problem and I am not making any progress. Can someone let me know what I am doing wrong here?

    Your time is much appreciated,
    Steve

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble with increments

    You never replied to answers given to your question posted on 2/7/13. Consider replying to the kind folks who took the time from their day to try to help you out. It often motivates others to help you with current and future posts.

  3. #3
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default Re: Trouble with increments

    I'll say it before someone else does. You need to use the code tags. It really does here read your code.

    Java Code:
     
    private static void chars(String input) {
        charCount += input.replaceAll("\\s", "").length();
    }

  4. #4
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Trouble with increments

    Quote Originally Posted by Fubarable View Post
    You never replied to answers given to your question posted on 2/7/13. Consider replying to the kind folks who took the time from their day to try to help you out. It often motivates others to help you with current and future posts.
    I went and thanked them. I had just forgot and I don't take the help and time people dedicate on these types of forums for granted.

  5. #5
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Trouble with increments

    hmm still not quite sure what you mean by code tags. The professor wants to see us use nested for loops and parameterized methods.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Trouble with increments

    Wrap your code in [code] tags [/code] so it retains its formatting.
    That's what's meant by code tags.
    Unformatted code is hard to read and follow.

    As for your problem.
    You seem to have a loop within a loop.
    You also don't seem to have any sort of starting height variable.

    Describe how you would do this by hand, because it looks like you dove into the code without coming up with a strategy.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble with increments

    Your 'simulation' assumes a linear relationship between the number of bounces and the height of the ball after that many bounces, say, 'n'; your simulation does 0.8*n where n goes down 10, 9, 8 ... but that's not what the assignment text states; it states after every bounce: newHeight= oldHeight*0.8; and the oldHeight starts at 10. That is not a linear relationship.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Trouble with increments

    Is it necessary to take nested loops?
    IF not I have modified the code a bit.
    Java Code:
    public class Bouncy_Ball {
    
        public static void main(String[] args) {
            bounce();
        }
    
        public static void bounce() {
            int j=10;
            for (int i=1; i<=10; ++i){
                System.out.print("After bounce " + i + " the ball reaches ");
                System.out.println(j-j*.2+ "meters.");
                j--;
    
            }
    
        }
    }
    Check if it works correctly for you?
    **And sorry to the senior members and staffs who thins i am feeding him with code.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Trouble with increments

    Quote Originally Posted by harshit shah View Post
    **And sorry to the senior members and staffs who thins i am feeding him with code.
    You obviously feel this is a solution, and realise posting full solutions is frowned upon, so why do it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble with increments

    Quote Originally Posted by harshit shah View Post
    Check if it works correctly for you?
    Have you checked it yourself? (spoiler: it is utterly incorrect)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Trouble with increments

    Quote Originally Posted by JosAH View Post
    Have you checked it yourself? (spoiler: it is utterly incorrect)

    kind regards,

    Jos
    okay just rempve the j-- from my code and it shall work fine
    Java Code:
    public class Bouncy_Ball {
     
        public static void main(String[] args) {
            bounce();
        }
     
        public static void bounce() {
            double j=10;
            for (int i=1; i<=10; ++i){
                System.out.print("After bounce " + i + " the ball reaches ");
                j=j-(j*.2);
                System.out.println(j+ "meters.");
                
     
            }
     
        }
    }
    Last edited by harshit shah; 02-13-2013 at 03:40 PM.

  12. #12
    nmn
    nmn is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Trouble with increments

    Stephen, apart from the missing code tags, there are a number of problems in your program.
    - you most probably did not want to count down from 10 to 1 for every single bounce, did you?
    - as Tolls mentioned, the bouncing height does not decrease in a linear fashion. You should use a height variable and modify it to the new jumping height for each bounce in the way your homework asks

    Possible output for a correct solution:
    Start: 10.0m
    1: 8.0m
    2: 6.4m
    3: 5.12m
    4: 4.096m
    5: 3.2768m
    6: 2.62144m
    7: 2.097152m
    8: 1.6777216m
    9: 1.34217728m
    10: 1.073741824m
    11: 0.8589934592m
    12: 0.68719476736m
    13: 0.549755813888m
    14: 0.43980465111m
    15: 0.351843720888m
    16: 0.281474976711m
    17: 0.225179981369m
    18: 0.180143985095m
    19: 0.144115188076m
    20: 0.115292150461m

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trouble with increments

    Quote Originally Posted by harshit shah View Post
    okay just rempve the j-- from my code and it shall work fine
    That is not just 'removing the j--'; but I noticed you figured out why your previous spoonfeeding was utterly incorrect. Better test your code before posting it and even better than that is not to spoonfeed at all.

    kind regards,

    Jos
    harshit shah likes this.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Trouble with increments

    Quote Originally Posted by JosAH View Post
    That is not just 'removing the j--'; but I noticed you figured out why your previous spoonfeeding was utterly incorrect. Better test your code before posting it and even better than that is not to spoonfeed at all.

    kind regards,

    Jos
    yup,mate.it was my bad was in a hurry but I do not call it a spoon feeding.he may get knowledge from my code and learn not to do unnecessary nested for loops because the make code complex

Similar Threads

  1. Replies: 5
    Last Post: 10-29-2012, 02:51 AM
  2. set increments in data plot
    By lamorak24 in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2011, 07:01 AM
  3. Im having trouble with.......
    By Java Noobie in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 05:06 PM
  4. ++/--increments for (C/JAVA) explained!!
    By _ShivamKapoOr_ in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 03:02 PM
  5. Pre- and post-increments
    By SirFalcon in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 03:29 AM

Posting Permissions

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