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
Hello guys please help me <br>list and identify the components of computer ​
attashe74 [19]

Answer:  A motherboard

A Central Processing Unit (CPU)

A Graphics Processing Unit (GPU), also known as a video card

Random Access Memory (RAM), also known as volatile memory

Storage: Solid State Drive (SSD) or Hard Disk Drive (HDD)

7 0
3 years ago
How to eject a flash drive from chromebook?
Dmitrij [34]
Go to the files app, and there should be an eject symbol next to the flash drive
8 0
3 years ago
____ are programs that run independently and travel between computers and across networks.
fiasKO [112]
<span>Worms are programs that run independently and travel between computers and across networks.
</span>Those are malicious programs which are exploiting operating system vulnerabilities and by doing so they spread itself. They <span>self-replicate on computers or via computer networks without the user being aware.</span>
3 0
4 years ago
Which company provides a crowdsourcing platform for corporate research and development?
777dan777 [17]

Answer:

OA.

MTurk(mechanical turk)

Explanation:

Mechanical Turk is a Microwork service provided by Amazon. Correct answer: C It is an online “crowdsourcing” marketplace that was launched in 2005 by Amazon.

This site (service)   allows businesses and individuals to post short tasks and pay workers to complete them.

6 0
3 years ago
The part of the poppet valve that contacts the valve seat is called the A. face. B. margin. C. head. D. stem.
Phantasy [73]
It should be A face, I think
6 0
3 years ago
Read 2 more answers
Other questions:
  • A custom date format set for a date/time field that contains the symbols mmm/dd/yy would display the date as ____.
    12·1 answer
  • Brian wants to conduct an online search using a certain phrase he intends to use the word books that belong to the 1800's in his
    8·1 answer
  • Write a function called lucky_sevens that takes in one #parameter, a string variable named a_string. Your function #should retur
    15·1 answer
  • ) How many switching functions of two variables (x and y) are there?
    11·1 answer
  • Anybody good with Microsoft excel? I need help with this class.
    9·2 answers
  • What is the color difference between the iMac and iMac Pro
    14·2 answers
  • Kyle returns to work the next day and he would like to continue working on the document from yesterday. What should Kyle do?
    7·1 answer
  • What are some best practices for file management
    8·1 answer
  • Consider the following code snippet that appears in a subclass: public void deposit(double amount) { transactionCount ++; deposi
    14·1 answer
  • GIF is example of raster image<br><br><br> RIGHT OR WRONG?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!