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 you are testing the Orders database introduced in Watt (2014) - Appendix C. Discuss the problems and possible conseq
madreJ [45]

Answer:

129 \frac{2}{?} 23.4. \div 164 \times 5y1 + . \\ .00487ggh

6 0
3 years ago
There's an App for That!
AleksandrR [38]

Answer:

Im confused man

Explanation:

6 0
3 years ago
_______ is the protocol suite for the current Internet.
Gala2k [10]
A? not very sure sorry
8 0
3 years ago
What are all the physical components called?
Alecsey [184]

Physical Components to a computer are called hardware.

3 0
3 years ago
At your job, you often have to address letters to the customer support manager, Tyson Kajewski. The problem is that you are cons
krek1111 [17]
Input his name in the dictionary function, you can also copy the given name an paste as much as you need
5 0
3 years ago
Other questions:
  • What do you click to create a new presentation in Normal view
    14·1 answer
  • Develop a program that will calculate the area and perimeter of a rectangle. The length and width can be given as constant.(LENG
    8·1 answer
  • How do headphones work? (From pc)
    8·1 answer
  • Narrow margins are helpful for which task?
    6·1 answer
  • What is the disadvantages of using proprietary software
    10·2 answers
  • I need help with this work
    15·1 answer
  • Tier 1 networks form the internet ____.​
    15·1 answer
  • Match the ernerging technologies to their descriptions.
    14·1 answer
  • The most widely used computer device​
    6·2 answers
  • Which response best completes the following IF-ELSE statement?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!