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
The scope of a variable is __________________
Goryan [66]

Answer:

The scope of a variable is where it can be read or modified.

6 0
2 years ago
A window may be resized by dragging the___ or side of it
dangina [55]

top i believe is the answer


4 0
4 years ago
Read 2 more answers
What's 3+3 and stop deleting my question i just wasted 41 points
bazaltina [42]

Answer:

I am pretty confident that the answer is 6

Explanation:

3+3=6

3 0
4 years ago
Read 2 more answers
Which feature of virtualisation helps increase the IT productivity of a business
PIT_PIT [208]

Answer:

Add, remove, organize and reorder commands on the Quick Access Toolbar. ... The Quick Access Toolbar is a customizable toolbar that contains a set of ... The only way to increase the size of the buttons is to lower the screen resolution you use. ... However, you can Customize the ribbon in Office to personalize the ribbon ...

Explanation:

because

8 0
4 years ago
What uses curly braces containing a number that indicates the desired position for a variable in a list that follows a string us
cluponka [151]

In programming technology it used as common and some place it is varies.

If variable declared as string data type position represents the character of the string. If variable declare as integer data type position represent as cell reference.

Curly braces containing a number indicates the position of character in it data type is string

At the same it also indicates the position of a array .

Dim a as string = “HELLO”

A(2) – it is represents “L” where it started 0 optional bas

Dim a(10) as integer =(1,2,3,4,5,6,7,8,9,10)

A(2) – it represented array cell position answer is “3”.

3 0
3 years ago
Other questions:
  • PLEASE HURRY
    6·1 answer
  • A soldier white line down the center of a two lane road indicates
    5·1 answer
  • Which hexadecimal-type address refers to a physical networking device such as a network interface card?
    10·1 answer
  • You want to boot a linux system into singer user mode/ what options might you add to a linux kernel options list in a boot loade
    9·1 answer
  • What's a computers C hard drive
    13·2 answers
  • What is the empty space inside the character 'O' called?
    12·1 answer
  • Which 2 processes are operational processes
    10·1 answer
  • Which precaution should you take while working on a computer
    9·1 answer
  • The space that helps you organize your PowerPoint or Web Page is called ______.
    13·1 answer
  • Yahoo Messenger is an example of a/an __________ service on the Internet.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!