I'm very new to Java and I'm confused on how to create a method that goes back to original position, going through each step and turn.
This is an assignment for school and essentially, I have to create a Bug class, which moves 1 unit of distance on a horizontal line, turns, reverses, and moves back to original position.
Here's the code I have so far:
public class Bug
{
private int position;
private String direction;
public void initialPosition(int position ) {
position=0;
}
public void initialDirection(String direction) {
direction="right";
}
public void setPosition (int position) {
this.position = position;
}
public int getPosition() {
return position;
}
public void setDirection (String direction) {
this.direction = direction;
}
public String getDirection() {
return direction;
}
public void move() {
position+=1;
}
public void reverseDirection() {
if (direction=="right") {
direction="left";
}
else {
direction="right";
}
}
public String toString() {
return "Position: " + position + " & Direction: " + direction;
}
}
Any tips on code will be super appreciated as well. Thanks!