1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nadusha1986 [10]
3 years ago
7

Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bu

g 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
Computers and Technology
1 answer:
Harrizon [31]3 years ago
7 0

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;

   }

}

You might be interested in
What is an example of a hard skill?
Serjik [45]
3 would be self management
4 0
3 years ago
Read 2 more answers
Who's better Kapkan or Tachanka?
navik [9.2K]
If you want fun operator, get chanka, if you want good operator, get kapkan it also depends if depends you are a stationary man who likes big guns or a mobile man who likes traps
4 0
3 years ago
Which of the following did you include in your notes?
iVinArrow [24]

Answer:

How you will be innovative in what you offer

Explanation:

personally I think all 3 but it is what it is

6 0
3 years ago
List the difference between sdram and dram​
IRISSAK [1]

Answer:

i need this for a challenge

Explanation:

4 0
3 years ago
50PTS!! does anyone know how you turn in a section of your slides my teacher wants us to turn in only a portion of our google sl
HACTEHA [7]

Answer:

The best way to turn in one part of google slides, since you can't split the slide when turning assignments on classroom, is to copy and paste the part you want to turn in into another slide and submit that portion. I've done for many projects before and if you name the different slides (Ex: Assignment Portion One 5/6/20) so it won't get confusing with the multiple slides.

This is the best way I can think of, I hope I helped, and please correct me if there is a better way!

5 0
4 years ago
Other questions:
  • You can access various sites on the WWW by using hyperlinks or by
    14·1 answer
  • Someone who participates online for social reasons usually has a:
    9·1 answer
  • Edhesive 3.2 Lesson Practice question 1
    5·1 answer
  • Cheri needs to write a small program that interacts with the user. What step will allow her to do this?
    14·2 answers
  • sparse(compact) Description: A sparse matrix is a 2D matrix that has many zeros. Assume that for storage efficiency someone has
    9·1 answer
  • Which result is most likely if a network packet has no header?
    9·2 answers
  • Which view in the View tab of the ribbon is the easiest place to add a header or a footer? Normal view Custom Views Page Layout
    5·2 answers
  • Create a public class Dog that stores a single double age set by the constructor. (Reject negative ages using assert.) Dog shoul
    13·1 answer
  • 75 pts. Conduct online research about 10 of the most famous and dangerous computer viruses that threatened computer networks aro
    13·1 answer
  • 1. Name the three kinds of Eraser in Photoshop. <br>​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!