Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Provide a constructor

public Bug(int initialPosition)
and methods
public void turn()
public void move()
public int getPosition()

Sample usage:

Bug bugsy = new Bug(10);
bugsy.move(); // now the position is 11
bugsy.turn();
bugsy.move(); // now the position is 10

Complete the following file:

Bug.java

/** This class models a bug that crawls along a horizontal line. */ public class Bug { private int position; private int direction; // 1 or -1 /** Constructs a bug with a given position, facing right. @param initialPosition the initial position */ public Bug(int initialPosition) { ... } /** Moves the bug by one unit in the current direction. */ public void move() { ... } /** Flips the direction of this bug. */ public void turn() { ... } /** Gets the current position of this bug. @return the position */ public int getPosition() { ... } }

Use the following files:

BugTester.java

public class BugTester
{
   public static void main(String[] args)
   {
      Bug lady = new Bug(10);
      lady.move();
      lady.move();
      lady.turn();
      lady.move();
      System.out.println(lady.getPosition());
      System.out.println("Expected: 11");
   }
}

BugTester2.java

public class BugTester2
{
   public static void main(String[] args)
   {
      Bug lady = new Bug(9);
      lady.turn();
      lady.move();
      lady.move();
      lady.turn();
      lady.turn();
      lady.move();
      lady.move();
      lady.move();
      lady.move();
      System.out.println(lady.getPosition());
      System.out.println("Expected: 3");
   }
}