Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Turning a Bug around.

    This is what I currently have. I need to get the bug to turn around without subtracting from its current position. The book provides this sample:
    Bug bugsy = new Bug(10)
    bugsy.move(); //It is now at 11
    bugsy.turn();
    bugsy.move(); //It is now at 10.

    Everything I've done to turn it simply makes it move backwards so the turn is at 10. And then the next move puts it at 11 again instead of 10. Or it turns it twenty units so it's at -11, which isn't what I'm supposed to do.

    Just as a warning I come from an English major background. I still have enough trouble remembering which is my x axis and which is my y axis.

    Thank you ever so much,

    Shadow



    public class ShellBugMover
    {

    //Constructs the Bug
    public ShellBugMover(int initialPosition)
    {
    currentPosition = initialPosition;

    }

    //Moves the Bug 1 unit to the right.
    public void move()
    {
    currentPosition = currentPosition + 1;
    }

    //Changes the direction of the Bug.
    public void turn()
    {
    currentPosition = ????
    }

    //Denotes the current position of the Bug.
    public int getPosition()
    {
    return currentPosition;
    }

    //Declares the instance field.
    int currentPosition;

    }
    Last edited by Nightshadow_t2; 02-17-2010 at 07:28 AM. Reason: clarification

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

    Default

    I answered exactly this question in another thread.

    kind regards,

    Jos

  3. #3
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Does that mean I need to create another variable for the direction? I tried creating one earlier, but then the tester wouldn't compile anymore. And thank you for pointing me at the thread, though I still find myself a bit lost.

    May the day be yours,
    Shadow
    Last edited by Nightshadow_t2; 02-17-2010 at 08:02 AM.

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

    Default

    Quote Originally Posted by Nightshadow_t2 View Post
    Does that mean I need to create another variable for the direction? I tried creating one earlier, but then the tester wouldn't compile anymore.
    Yes, you have to keep track of 'x' (the current position) and 'dx' (the direction); a move does this x+= dx; and a turn does this dx= -dx; the rest are just details.

    kind regards,

    Jos

Similar Threads

  1. Turning an image to binary
    By morghul in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 07:47 PM
  2. Replies: 3
    Last Post: 08-11-2009, 12:18 AM
  3. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 07:38 AM

Tags for this Thread

Posting Permissions

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