Discover the answers you need at Westonci.ca, a dynamic Q&A platform where knowledge is shared freely by a community of experts. Get immediate answers to your questions from a wide network of experienced professionals on our Q&A platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

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 turnC * public void move( °public int getPositionO Sample usage: Bug bugsy new Bug(10) bugsy.moveO; // Now the position is 11 bugsy.turnO bugsy.moveO; // Now the position is 10 Your main method should construct a bug, make it move and turn a few times, and print the actual and expected positions

Sagot :

Answer:

Explanation:

The following code is written in Java. It creates the Bug class with the position and direction variables. Then it creates a constructor, move method, turn method, and getPosition method. Finally, a bug object called bugsy is created in the main method, and we move it once to the right, then again to the right, and then we turn it and move it 5 times to the left, printing out the position when it is done moving. Output can be seen in the attached picture below.

class Brainly {

   public static void main(String[] args) {

       Bug bugsy = new Bug(10);

       bugsy.move();

       System.out.println("Current bug position: " + bugsy.getPosition());

       bugsy.move();

       System.out.println("Current bug position: " + bugsy.getPosition());

       bugsy.turn();

       bugsy.move();

       bugsy.move();

       bugsy.move();

       bugsy.move();

       bugsy.move();

       System.out.println("Current bug position: " + bugsy.getPosition());

   }

}

class Bug {

   char direction = 'r';

   int position = 0;

   public Bug(int initialPosition) {

       this.position = initialPosition;

   }

   public void turn() {

       if (this.direction == 'r') {

           this.direction = 'l';

       } else {

           this.direction = 'r';

       }

   }

   public void move() {

       if (this.direction == 'r') {

           this.position += 1;

       } else {

           this.position -= 1;

       }

   }

   public int getPosition() {

       return this.position;

   }

}

View image sandlee09

In this exercise we want to use computer and Java knowledge to write the code correctly, so it is necessary to add the following to the informed code:

Output can be seen in the attached picture below

The following secret language system exist composed fashionable Java. It develop in mind or physically the bug class accompanying the position and direction variables. Then it develop in mind or physically a builder, move arrangement, turn means, and catch position procedure.

Finally, a bug object named bugsy happen develop in mind or physically usually pattern, and we fast once to the right, therefore another time to the right, and before we turn it and speedy 5 period to the abandoned, a process of reproduction out the position when it exist thoroughly cooked mobile. We have that:

class Brainly {

  public static void main(String[] args) {

      Bug bugsy = new Bug(10);

      bugsy.move();

      System.out.println("Current bug position: " + bugsy.getPosition());

      bugsy.move();

      System.out.println("Current bug position: " + bugsy.getPosition());

      bugsy.turn();

      bugsy.move();

      bugsy.move();

      bugsy.move();

      bugsy.move();

      bugsy.move();

      System.out.println("Current bug position: " + bugsy.getPosition());

  }

}

class Bug {

  char direction = 'r';

  int position = 0;

  public Bug(int initialPosition) {

      this.position = initialPosition;

  }

  public void turn() {

      if (this.direction == 'r') {

          this.direction = 'l';

      } else {

          this.direction = 'r';

      }

  }

  public void move() {

      if (this.direction == 'r') {

          this.position += 1;

      } else {

          this.position -= 1;

      }

  }

  public int getPosition() {

      return this.position;

  }

}

See more about computer at brainly.com/question/950632

View image lhmarianateixeira