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
motikmotik
2 years ago
13

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
Computers and Technology
1 answer:
Zinaida [17]2 years ago
5 0

Answer:

Following are the constructor to the given code:

Bug(int position) //defining a constructor that defines a integer parameters

{

       this.position = position;//use this key word to hold parameter value

       right = true;//defining a variable right that hold a boolean value

   }

Explanation:

In this code, a constructor is declared that defines integer parameters with it, Inside the constructor, this keyword is used to holds the "position" parameter value and defines a boolean variable "right" that holds a boolean value.

Full program:

public class Bug //declaring a class Bug

{

   private int position;//declaring integer variable position

   private boolean right;//declaring boolean variable

   public Bug(int position) //defining a parameterized constructor

   {

       this.position = position;//use this to hold position value

       right = true;//holding boolean value

   }

   public void turn() //defining a method turn

   {

       right = !right;//holding value

   }

   public void move() //defining method move

   {

       if(right)//use if to check boolean value

       {

           position++;//incrementing position value

       }  

       else//else block

       {

           position--;//decreasing position value

       }

   }

   public int getPosition()//defining method getPosition  

   {

       return position;//return position value

   }

   public static void main(String[] args) //main method

   {

       Bug bug = new Bug(10);//creating class object

       System.out.println("Expected = 10, Actual = " + bug.getPosition());//calling method with printing value

       bug.move();//calling method

       System.out.println("Expected = 11, Actual = " + bug.getPosition());//calling method with printing value

       bug.move();//calling method

       bug.move();//calling method

       bug.move();//calling method

       System.out.println("Expected = 14, Actual = " + bug.getPosition());//calling method with printing value

       bug.turn();//calling method

       bug.move();//calling method

       bug.move();//calling method

       System.out.println("Expected = 12, Actual = " + bug.getPosition());//calling method with printing value

       bug.turn();//calling method

       bug.move();//calling method

       System.out.println("Expected = 13, Actual = " + bug.getPosition());//calling method with printing value

   }

}

Output:

Please find the attached file.

You might be interested in
Assume that a program uses the named constant PI to represent the value 3.14. The program uses the named constant in several sta
mihalych1998 [28]

Answer:

The advantage for the above condition is as follows:-

Explanation:

  • If a user creates a defined constant variable and assigns a value on its and then uses that variable instead of the value, then it will a great advantage.
  • It is because when there is a needs to change the value of that variable, then it can be done when the user changes the value in one place. There is no needs to change the vale in multiple places.
  • But if there is a value in multiple places instead of a variable and there is no constant variable, then the user needs to change the value in multiple places.
7 0
3 years ago
A red bullet in a microflow indicates: a. that the microflow has been completed b. that the microflow contains errors c. that on
Viefleur [7K]

Answer:

d. an end point of the microflow mendix

Explanation:

In Computer programming, Microflows can be defined as a visual representation of textual program codes based on the Business Process Model and Notation (BPMN), it enables users to express the logic of application.

It is capable of performing various actions such as creating and updating objects, showing pages and making choices.

Microflows cannot be used in offline apps as it basically runs in the runtime server.

A red bullet in a microflow indicates an end point of the microflow mendix.

8 0
3 years ago
A college asks you to write about a novel that is meaningful to you. What should you include in this essay?
Marina CMI [18]

The answer is C because they want you to compare it your or others lives.

4 0
3 years ago
Which of the following does not accurately describe asynchronous communication?
worty [1.4K]

Answer:

A. While new media is dependent on whether users function in real-time or delayed mode, feedback is not as timely when communication is asynchronous.

Explanation:

Asynchronous communication can be defined as a data communication technique in which exchange of data between a sender and a recipient isn't in real time.

There's usually a time lag, meaning it doesn't require the recipient to respond immediately.

For example, responding to an email or text several hours later.

6 0
3 years ago
Read 2 more answers
Do warnings ever expire?
DIA [1.3K]
What’s a warning? how do i get one haha
7 0
3 years ago
Read 2 more answers
Other questions:
  • What is a good monitor for console gaming? (Any price)
    13·2 answers
  • What is a URL (Please explain)?
    9·1 answer
  • In python,_______ are used to define the conditions necessary for a while loop to run.
    7·2 answers
  • The Internet acts as a(n) blank services such as The World Wide Web and email
    6·2 answers
  • What is the role of the W3C? Group of answer choices oversee research and set standards for many areas of the Internet supervise
    10·1 answer
  • What are some other features of sending attachments in Outlook 2016? Check all that apply.
    9·2 answers
  • Cindy tried to delete a customer but the customer still had open invoices so the system would not allow her to delete the custom
    11·1 answer
  • Time shifting occurs when
    8·2 answers
  • The rhythmic note that three beats is called a____half note.
    15·2 answers
  • What does this mean? And how did I get it?
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!