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
3 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]3 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
A powerful computer that acts as a hub for other computers is a called a ______.
zhannawk [14.2K]
It is called a server
7 0
3 years ago
Read 2 more answers
Client/server networks are controlled by a central server that runs a specialized piece of software called
Marta_Voda [28]
It is called a wired hot spot

6 0
4 years ago
Being nice take the points​
MArishka [77]

Answer:

Sure!!! and thank you very much

4 0
3 years ago
What is the result of a network technician issuing the command ip dhcp excluded-address 10. 0. 15. 1 10. 0. 15. 15 on a cisco ro
Hunter-Best [27]
The answer to this question is 83774838473773
3 0
2 years ago
True / False
Tju [1.3M]

Answer:

TRUE, The PC is always incremented by the same amount in fixed-length instruction set architectures.

Explanation:

Its TRUE that Program Counter ( PC ) is always incremented by the same amount in fixed - length instruction set architectures ( fixed length ISA) . As the instruction set length is fixed in fixed - length instruction set architectures, the Program Counter to fetch the next instruction set it has to be incremented by fixed length. This fixed length depends on the hardware of the architecture (the number of bytes each machine word contains and number of machine words per memory location)

7 0
4 years ago
Other questions:
  • You just realized the turn signal on your vehicle is broken,
    15·1 answer
  • Describe the "Mister Splashy Pants" campaign. How did it start? What happened?
    7·2 answers
  • Which of the following should be the first page of a report?
    11·1 answer
  • Help with number 12 please!
    15·1 answer
  • What piece of software tells the operating system how to use a specific hardware device? a. User interface b. System service c.
    14·1 answer
  • Create a trigger that prevents anychange(insert, update, or delete)to the grade attribute of the takesrelation that would change
    14·1 answer
  • Software people commonly use in the workplace to make their life easier is called?
    11·2 answers
  • Write the simulate method, which simulates the frog attempting to hop in a straight line to a goal from the frog's starting posi
    10·1 answer
  • Consider the following static method.
    9·1 answer
  • The following method is intended to return true if and only if the parameter val is a multiple of 4 but is not a multiple of 100
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!