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
How do you calculate the total resistance in a series circuit with more than one resistor?
yan [13]
<span>Resistors are said to be connected in series when they are daisy chained together in a single line. The serial circuit of resistors result  has a common current flowing through the resistors.
</span><span>The current that flows through one resistor must also flow through the others.
</span>

The total equivalent resistance, is given as:

<span>Rtotal = R1 + R2 + R3 , where R1 is the resistance of the first resistor, R2 of the second and R3 f the third</span>

4 0
3 years ago
Pointsfor a failover cluster, what type of network would you use to communicate with an iscsi device?
guajiro [1.7K]
A local Network..........
8 0
3 years ago
Consider that a large online company that provides a widely used search engine, social network, and/or news service is consideri
k0ka [10]

Answer:

I think that they should ban ads for all four products.  These products, e-cigarettes, abortion clinics, ice cream, and sugared soft drinks, are all discreet adult products that should not be advertised because of their health hazards.  Since the "large online company provides a widely used search engine, social network, and/or news service, which are mainly patronized by younger people, such ads that promote products injurious to individual health should be banned.  Those who really need or want these products know where they could get them.  Therefore, the products should not be made easily accessible to all people.  Nor, should ads promote their patronage among the general population.

Explanation:

Advertisements create lasting and powerful images which glamourize some products, thereby causing the general population, especially children, to want to consume them without any discrimination of their health implications.  If online banning is then contemplated, there should not be any distinguishing among the four products: e-cigarettes, abortion clinics, ice cream, and sugared soft drinks.

7 0
3 years ago
What is an administrator?
Drupady [299]
A person responsible for running a business, organization, etc.
3 0
3 years ago
Which is the best programming practice?
irina [24]

Answer:

Open a file when the program needs to work with it and close the file as soon as you have finished using it.

Explanation:

In programming, we have some files that are stored in memory, or created in some other section of the program to save information.

<u><em>for example</em></u>, we want to save the data of a class or school faculty to create a database. For this purpose I need to create a file.

As the files that we will create may contains data, these files will utilize computer resources such as, memory (RAM, Cache) and other resources. While programming, we need these resources as well.

<em>Therefore, It is necessary to open a file when the program needs to work with it and close the file as soon as we have finished using it. The reason is that, it takes computer resources which are limited.    </em>

4 0
3 years ago
Other questions:
  • Why were mechanical calculators developed?
    8·2 answers
  • A computer is made of up 6 main components: a) CPU (central processing unit)
    7·1 answer
  • With a _____ network connection, the computers and other devices on the network are physically connected via cabling to the netw
    13·1 answer
  • Mohammed's parents learn that his classmates have
    7·2 answers
  • To what would you compare the transport layer?
    14·1 answer
  • Machu Picchu is located in modern day _______<br><br>​
    14·2 answers
  • Information is data converted into useful, meaningful context. What are data characteristics that change or vary over time?
    8·1 answer
  • Type the correct answer in each box. Spell all words correctly.
    14·2 answers
  • Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s), before recompiling?
    14·1 answer
  • Please help with this question
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!