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.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,
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()
//Declares the instance field.
I answered exactly this question in another thread.
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,
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.
Originally Posted by Nightshadow_t2