1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Ant on a rubber band program

  1. Nov 15, 2011 #1
    1. The problem statement, all variables and given/known data

    Write a Java program to solve the following problem.
    An elastic rope starts out with length 100 meters. The start of the rope is fixed to a pole and a worm is placed on the rope at the start and the worm starts crawling towards the end. Each day the worm crawls 6 meters along the rope. Each day the rope is stretched by 100 meters. After how many days does the worm reach the end of the rope?


    2. Relevant equations

    This is a harmonic series, so it will eventually have a solution. the equation i end up getting is:
    for the nth term:
    distance traveled = 6/(100+100) + 6/(100+200) + 6/(100+300)+.......+6/(100+n100)


    3. The attempt at a solution

    attempt 1:
    my code looks like:

    ppublic class Worm2 {
    public static void main(String[] arg) {

    long day, distancefrend, length, n, j, i;

    day = 1;
    length = 100;
    distancefrend = 0;
    n = 1;
    j = 0;
    i = 0;

    while (distancefrend >= 0){
    while (n <= day){
    j = 6/(100 + (n * 100));
    i = i + j;
    n++;
    }
    length = length + 100;
    distancefrend = length - (i * length);
    System.out.println("Day = " + day + " Distance from end: " + distancefrend + " meters.");
    day++;
    }
    }
    }


    this code was going to take probably 3 hours to run, so i upped the time step to day = day + 1000000 but it STILL didnt work, my distance from end keeps growing forever ..so i tried a whole new approach:

    attempt 2 (not using a harmonic series):

    public class Worm {
    public static void main(String[] arg) {

    long day, distance, length, distancefrend;

    day = 1;
    length = 100;
    distance = 0;
    distancefrend = 0;

    while (distance <= length){
    length = length + (100);
    distance = (distance/(length - 100))*length + (6);
    distancefrend = length - distance;
    System.out.println("Day = " + day + " Distance from end: " + distancefrend + "million meters.");
    day++;
    }
    }
    }

    This was the same...there must be a problem with my code, but i cant find it. This is a first year CS problem and im a 3rd year astrophysics major, this shouldn't be hard for me but ive had no luck. Please help :)
     
  2. jcsd
  3. Nov 15, 2011 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    Each day the worm progresses 6 metres, yet by day's end the rope has become 100 metres longer. On the face of it, it sounds like an exercise in futility! :smile: I'll have to ponder it further, since it has been set as an exercise.

    That's one very elastic rope, by the way.

    The theory seems to be that although the worm steps out 6 metres, at the end of the day the start of the rope has receded by more than 6 metres because the rope behind him has stretched, too.
     
    Last edited: Nov 15, 2011
  4. Nov 15, 2011 #3
    put it this way:
    the rope starts at 100 meters on day = 0, and the ant is at x = xo

    over night the rope stretches to 200 meters,, so on day 2, the rope is 200m and in the morning the ant walks 6 meters along the rope so x = 6m.

    over night the rope is stretched another 100m BUT the ant's distance has stretched also, which makes the rope at 300m, the ant at 9 meters. However the ant walks another 6 meters after the rope has been stretched. So, on day 3, the rope is 300m and the x = 15m......etc
     
  5. Nov 15, 2011 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    Assuming I have the right idea, this is how I'd word it: each day the worm crawls 6 metres then builds a cocoon and sleeps away the cold night hours. Meanwhile, as he sleeps, the rope stretches an additional 100 metres overall. (This makes it clear how the cocoon gets carried along during the rope's stretching phase, moving him farther from the start even though he is comatose.)

    EDITED (swapped day and night activities)
     
    Last edited: Nov 15, 2011
  6. Nov 15, 2011 #5
    this is exactly right, and a much better way of describing it.
     
  7. Nov 15, 2011 #6

    NascentOxygen

    User Avatar

    Staff: Mentor

    The most obvious check would be to examine the first half dozen lines your program prints out. Compare with your hand calculations done on a calculator.
     
  8. Nov 15, 2011 #7
    i think thers a problem with my inner while loop.

    It i break it down to just:


    public class Worm2 {
    public static void main(String[] arg) {

    long day, distance, length, n, j, i;

    day = 1;
    length = 100;
    distancefrend = 0;
    n = 1;
    j = 0;
    i = 0;

    while (n <= 3){
    j = 6/(100 + (n * 100));
    i = i + j;
    n++;
    }

    distance = i * 400;
    system.out.print(distance);
    }
    }

    it prints out that my distance traveled is 0, when it should be 26
     
  9. Nov 16, 2011 #8

    cepheid

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Isn't this just a problem with integer arithmetic? I mean, in your while loop, if n = 1, then j = 6/200 = 0 (in integer arithmetic). Also, could you please enclose your code in [noparse]
    Code (Text):
     
    [/noparse] tags? It makes your code more readable, because it preserves your whitespace like this:

    Code (Text):


    public class Worm2 {
        public static void main(String[] arg) {
     
            long day, distance, length, n, j, i;
     
            day = 1;
            length = 100;
            distancefrend = 0;
            n = 1;
            j = 0;
            i = 0;

                          while (n <= 3){
                                j = 6/(100 + (n * 100));
                                i = i + j;
                                n++;                   
                          }

                          distance = i * 400;
                          system.out.print(distance);
               }
    }
     
     
  10. Nov 16, 2011 #9
    bingo! You got it :) I ran it with int instead of int and it worked beautifully. I ended up with 26.4 million days, which is what i was expecting. its my first time dealing with the long variable, I'm not sure why i didn't pick up on that.

    Sorry about the code, its my first time posting code in a forum, i had no idea.
     
  11. Nov 16, 2011 #10

    cepheid

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    No problem. Did you mean that you ran it with float instead of int? It wouldn't work with any integer data type (short, int, or long, or any of their unsigned variants).

    EDIT: Disclaimer: My specific data type examples are for C, which is the only language I really know well. But the statement that you'd need some sort of floating point data type is generally true.
     
  12. Nov 16, 2011 #11
    woops, yes thats right, i ran it with a floating point number
     
  13. Nov 16, 2011 #12
    is there a way do make a floating point number only display to a certain decimal place in java? im sure there is but i have no idea how.
     
  14. Nov 16, 2011 #13

    cepheid

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If have no idea how either. There almost certainly is though (I mean, I know you can do it in C, so you ought to be able to do it in Java). You'll have to look up some info on the print function.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Ant on a rubber band program
  1. Matlab - Banded Matrix (Replies: 1)

  2. Band brakes (Replies: 3)

Loading...