Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class Bug that models a bug moving in the x-y plane. The bug either move

ID: 3782006 • Letter: W

Question

Write a class Bug that models a bug moving in the x-y plane. The bug either moves north, east, south, or west. Initially, the bug moves to the north, but it can turn to the right or the left by 90 degrees at a time. In each move, its position changes by one unit in the current direction. Provide a constructor

        public Bug(intinitX, intinitY) // places the bug at initial position

                                    // initX, initY

and methods:

        public void turnRight()          // turn right by 90 degrees

   public void turnLeft()           // turn left by 90 degrees

   public void move()               // move in current direction by 1 unit

   public intgetPositionX()         // get X position

   public intgetPositionY()         // get Y position

   public String getDirection()     // returns the direction (e.g. “east”)

Also, you must override the method public String toString() so that it returns a string that displays the bug’s position (x and y coordinates) as well as the direction that it is currently facing. Note: you don’t need to draw the bug on the screen (we have not learned how to do this yet). You just need the bug object to keep track of its current position and direction.

Explanation / Answer

Bug.java

/**
* Bug Class
* @author Anonymous
*
*/
public class Bug {
  
   private int positionX;
   private int positionY;
   private Direction direction;
  
   /**
   * Constructor
   * @param initx
   * @param inity
   */
   Bug(int initx, int inity){
       this.positionX = initx;
       this.positionY = inity;
       this.direction = Direction.NORTH;
   }
  
   /**
   * Method to turn right
   */
   public void turnRight(){
      
       switch(this.direction){
       case NORTH: this.direction = Direction.EAST;
           break;
       case EAST: this.direction = Direction.SOUTH;
           break;
       case SOUTH: this.direction = Direction.WEST;
           break;
       case WEST: this.direction = Direction.NORTH;
           break;
       }      
   }
  
   /**
   * Method to turn left
   */
   public void turnLeft(){
      
       switch(this.direction){
       case NORTH: this.direction = Direction.WEST;
           break;
       case EAST: this.direction = Direction.NORTH;
           break;
       case SOUTH: this.direction = Direction.EAST;
           break;
       case WEST: this.direction = Direction.SOUTH;
           break;
       }      
   }
  
   /**
   * Method to move 1 unit in the facing direction
   */
   public void move(){
       switch(this.direction){
       case NORTH: this.positionY+=1;
           break;
       case EAST: this.positionX+=1;
           break;
       case SOUTH: this.positionY-=1;
           break;
       case WEST: this.positionX=-1;
           break;
       }
   }
  
   /**
   * Getter of Position X
   * @return int
   */
   public int getPositionX(){
       return positionX;
   }  
  
   /**
   * Getter of Position Y
   * @return int
   */
   public int getPositionY() {
       return positionY;
   }
  
   /**
   * Getter of Direction
   * @return String
   */
   public String getDirection(){
       return this.direction.getDirection();
   }
  
   @Override
   public String toString(){
       String s = "Position X:"+this.positionX+" Position Y:"+this.positionY+" Direction:"+direction.getDirection();
       return s;
      
   }

   /**
   * Private Enum for Bug Class
   * Defining Enum because we have a fixed set of directions - North, South, East, West
   * @author Anonymous
   *
   */
   private enum Direction{
       NORTH("North"),
       EAST("East"),
       SOUTH("South"),
       WEST("West");
      
       private String d;
       private Direction(String direction){
           this.d = direction;
       }
      
       public String getDirection(){
           return d;
          
       }
   }
  
   /**
   * Main method to test the class
   * @param args
   */
   public static void main(String args[]){
       Bug bug = new Bug(2,3);
      
       System.out.println(bug);
       bug.move();
       bug.move();
       bug.turnRight();
       bug.move();
       bug.turnLeft();
       bug.turnLeft();
       bug.turnLeft();
       bug.turnLeft();
       bug.move();
       bug.move();
       bug.turnRight();
       System.out.println(bug);
       System.out.println("Direction:"+bug.getDirection());
       System.out.println("Position X:"+bug.getPositionX()+" Position Y: "+bug.getPositionY());
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote