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
Write a method called min that takes three integers as parameters and returns the smallest of the three values, such that a call
Debora [2.8K]

Answer:

public class Main

{

public static void main(String[] args) {

 System.out.println(min(3, -2, 7));

}

public static int min(int n1, int n2, int n3){

    int smallest = Math.min(Math.min(n1, n2), n3);

    return smallest;

}

}

Explanation:

*The code is in Java.

Create a method named min that takes three parameters, n1, n2, and n3

Inside the method:

Call the method Math.min() to find the smallest among n1 and n2. Then, pass the result of this method to Math.min() again with n3 to find the min among three of them and return it. Note that Math.min() returns the smallest number among two parameters.

In the main:

Call the method with parameters given in the example and print the result

7 0
2 years ago
Which of the following best explains the concept of a prototype
gladu [14]
A prototype<span> is an early sample, model, or release of a product built to test a </span>concept<span> or process or to act as a thing to be replicated or learned from. It is a term used in a variety of contexts, including semantics, design, electronics, and software programming.</span>
3 0
2 years ago
Marta is creating a program that will guide the user through a series of physical exercises. She wants the program to coach the
san4es73 [151]
She should use a condition-controlled loop. Because it uses a True/False and it will not continue until the task is done
7 0
2 years ago
Read 2 more answers
This tag element is the last one used in the html code sequence
satela [25.4K]

Answer:

Looking closely at the HTML code sequence, it does seem that the tag element is the endpoint from where the HTML form gets the input, like input boxes, checkboxes, radio buttons, etc. And hence it is correct to say that the tag elements are the last one used in the HTML code sequence. And in the HTML code sequence head title script and styles comes at the top, and hence we mention the script or the Javascript code basically at the last, such that the HTML code or the web page run one time at least.

Explanation:

Please check the answer section.

3 0
3 years ago
Canada’s energy plan, named __________, makes three governmental agencies accountable for safeguarding Canada’s environment.
stepan [7]

Answer:

Canada launched the national Generation Energy Council of leading Canadians, who proposed principles to the Government of Canada on how to build our energy future. Today, these principles guide government decisions to support Canadian families, businesses, and communities. Together, we are: saving energy powering clean communities.

Explanation:

8 0
2 years ago
Other questions:
  • What term refers to a piece of software that interfaces with the hardware on your computer?
    10·2 answers
  • What are the advantages and disadvantages to a home user of replacing dial-up Internet access with broadband?
    15·1 answer
  • Ryan needs to see the space available to insert content on a slide in his presentation. Which feature of a presentation program
    5·1 answer
  • Which type of network topology has a central networking device which manages the network and acts as a communications conduit fo
    7·1 answer
  • Which ergonomic principle helps to maintain good posture?
    8·2 answers
  • Compared to a virtual image, a real image
    15·2 answers
  • What is a Slide Master? A. the placeholder used to insert objects B. the sequence of slides in a presentation C. the default des
    14·1 answer
  • Create a basic program that accomplishes the following requirements: Allows the user to input 2 number , a starting number x and
    12·1 answer
  • From which country samsung is​
    6·2 answers
  • WHATS YALL FAVORITE GAME WHOEVER GIVES THE BEST ANSWER WINS 200
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!