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
Nick’s computer has been restarting on its own. what action should he take to solve this issue?
serg [7]
Check for possible software updates, my reasoning for that is the computer could be trying to initiate an update but is stopped by the user if you don't want it to update go to settings and turn off auto update
5 0
3 years ago
Can both mediated interpersonal communication and mass communication has an ability to reach huge number of recipients or audien
mars1129 [50]

Answer:

Explanation:

Somewhat

7 0
2 years ago
In the second example with modulus math, why can't Eve find the solution?
Leno4ka [110]
C. She would have to resort to trial and errors to find a matching exponent
6 0
2 years ago
Tamera was responding to a complaint that one of the employees is having problems with the wired network connection on their lap
WARRIOR [948]

Answer:

tamera like from sister sister hehe

Explanation:

sorry i dont know the awnser

3 0
3 years ago
The faster an object is moving, the ________ the shutter speed needs to be in order to freeze motion.
statuscvo [17]

Answer:

jkdsdjdshj,dfh.jhdfbhjf

Explanation:

bchSDCMHCXZ NHCXHBDSVCHDH,KC NBDBSDMJCBDBFD,JHCDSMNBBNCSCBFDNJCFJKC FMNSDNMSDFCH ĐS,CJDBS,CBSJBV,FJNDBFDSFDVBFĐDVFBVJFDCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDHCD

5 0
3 years ago
Read 2 more answers
Other questions:
  • Ming is building an inexpensive computer to use for her online classes, and needs to purchase Windows. What is the most cost-eff
    8·1 answer
  • What is a symptom of a failing power supply? The display has only a blinking cursor. The computer displays a POST error code. Th
    12·1 answer
  • Match each career with the education required for each job
    12·1 answer
  • Which of the following is not a key way that a LAN shares
    15·1 answer
  • Write an expression to compute the quartic root of x. the quartic root of a number is the square root of its square root. exampl
    11·1 answer
  • This project is to mimic a meeting request system by checking the requesting party’s validity, and the number of people attendin
    15·1 answer
  • Write a program that reads from the user any three points in two dimensional space: x1, y1, x2, y2, x3, y3. Assume these points
    13·1 answer
  • What are 25 items that trees made?
    6·2 answers
  • Q1 To remove filter
    15·2 answers
  • These brainly bots need to stop!!
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!