Results 1 to 10 of 10
  1. #1
    steveby63 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Assignment not working as expected

    Hello there. I am hoping someone can help me with a problem with an Java assignment that fails on one part (or so I think!). The rest apparently works.

    This is what I have been asked to create.

    Write a public instance method called sprint() which takes no argument an returns no value. The method should do the following:
    Store the existing colour of the receiver, then change its colour to red.
    Get a sprint length (a random number between 2 and 5),
    Enter a loop whose length is equal to the sprint length.
    On each iteration through the loop the method should -
    check if the receiver is on the last stone (numbered 11); if it is
    move the receiver to the first stone (numbered 1)
    otherwise move the receiver right
    increment the total distance by 1.

    Upon exit from the loop the receiver should jump once and its colour resets to its original state.

    Everything seems to work except for the fact that the receiver does not go back to the first stone, instead it continues on beyond stone 11.

    Here is the method I created.

    public void sprint()
    {
    getColour();// first, store the existing colour of the receiver
    setColour(OUColour.RED); // set its colour to red
    getSprintLength(); // get a sprint length

    int length = getSprintLength();

    do
    {
    if (totalDistanceSprinted > 10) // check if the receiver is on the last stone (numbered 11)
    {
    home(); // if it is then move the receiver to the first stone (numbered 1)
    }
    else
    {
    right(); // otherwise move the receiver right
    totalDistanceSprinted = totalDistanceSprinted +1; // increment the total distance sprinted by 1.
    }

    }
    while (length == getSprintLength()); // enter a loop whose length is equal to the sprint length

    jump(); // the receiver should be made to jump once
    green(); // Upon exit from the loop, and its colour reset to what it was prior to sprinting.
    }

    It uses other methods Right(); Home(); and getSprintLength(), which were part of the assignment and not my creation:

    /**
    * Increments the position of the receiver by 1.
    */
    @Override
    public void right()
    {
    this.setPosition(this.getPosition() + 1);
    }


    /**
    * Resets the receiver to its "home" position of 1.
    */
    @Override
    public void home()
    {
    this.setPosition(1);
    }

    /**
    * Returns a random integer value specifying a sprint length
    * between 2 and 5 inclusive.
    */
    private int getSprintLength()
    {
    return (this.randomNumber.nextInt(4) + 2);
    }

    I use bluejay, and the getColour(); setColour(OUColour.RED); are predefined as well.

    I am sure i'm missing something simple so any help would be appreciated.

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Assignment not working as expected

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    the receiver does not go back to the first stone, instead it continues on beyond stone 11
    What variable controls that? Where is its value tested and changed as required?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    steveby63 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Assignment not working as expected

    Thanks Norm -
    The variable totalDistanceSprinted takes account of the number of moves the receiver makes, or should!

    Two objects are created FitnessFrog ff1 and FitnessFrog ff2, they then become part of Coach ( c = new couch(ff1, ff2)), and then c.train(); is used, this asks the user for a number (1 - 5) and both object mover (right) a random number of times along a 'track', which is eleven 'stones' long.
    What is meant to happen when either 'frog' reaches 'stone' 11 is to go back to the first stone, but they don't'.

    I hope this helps.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Assignment not working as expected

    when either 'frog' reaches 'stone' 11 is to go back to the first stone,
    I would expect that there would be code that does something like this:
    Java Code:
    if(frog at stone 11) 
      set frog's location to stone 1
    Is there code that does that?
    What variable has the frog's location? Where is its value tested and its value set?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    steveby63 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Assignment not working as expected

    Thanks for the advice. The problem seems to be with the do while loop, it is ignoring something. I will see if I can find help on do while loops. Thanks for the help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Assignment not working as expected

    When you post code be sure to wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Assignment not working as expected

    Hmm.

    a) I think you must be more carefull about the variables. totalDistanceSprinted is not the position. So of course: totalDistanceSprinted is increasing and increasing and you set the position back. So you should check if the position is > 10 (But maybe you just check =11 because that is aked) and set it back to one.

    b) The loop is strange in my eyes. You want a loop that is as long as the sprint length. That is something different to a "loop as long as the first sprint length is the same as all other sprint length you get."
    So I would guess that a loop from 1 to length is requested.

    Konrad

  8. #8
    steveby63 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Assignment not working as expected

    Thanks you for the advice. As it turned out there was a problem with the loop. After some head scratching I finally solved the problem.
    Once again thanks.

  9. #9
    funkybetty is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default Re: Assignment not working as expected

    Hi Steveby63 are you able to point me at where the error is i've been scratching at my noggin for 3 days now and I can't seem to find the problem in the loop

  10. #10
    steveby63 is offline Member
    Join Date
    Feb 2014
    Posts
    5
    Rep Power
    0

    Default Re: Assignment not working as expected

    Hello funkybetty. After several attempt with the original code I had to re-write the entire method. I think the problem with with the do while loop which I don't think I solved, hence the re-write. I can't remember which bit caused the problem - sorry about that but I will provide the new code I came up with, which works.

    Java Code:
       public void sprint()
       {
          getColour();
          setColour(OUColour.RED); 
          getSprintLength();      
    
          while (getSprintLength() == 5)  
          {
             if (getPosition() == 11)
             {
                home();
             }
             else
                right();
                totalDistanceSprinted = totalDistanceSprinted + 1;          
          }
          jump();
          green();      
       }
    As you can see it is much sorter, but took some time for me to figure out.

Similar Threads

  1. .Jar not working as expected
    By iFrozen94 in forum New To Java
    Replies: 1
    Last Post: 02-02-2014, 09:18 PM
  2. Method not working as expected?
    By heatblazer in forum Advanced Java
    Replies: 2
    Last Post: 11-07-2013, 06:31 AM
  3. Parse int not working as expected
    By codyjava in forum New To Java
    Replies: 7
    Last Post: 11-03-2013, 08:16 PM
  4. Replies: 10
    Last Post: 04-19-2011, 04:13 AM
  5. Gridbaglayout not working as expected
    By berkeleybross in forum AWT / Swing
    Replies: 7
    Last Post: 07-06-2010, 01:11 PM

Posting Permissions

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