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
there are these numbers that I've seen before but I don't know what they mean can someone help. I will give brainiest
Ne4ueva [31]

Answer:

Those numberss are binary code

Explanation:

Computers use binary - the digits 0 and 1 - to store data. A binary digit, or bit , is the smallest unit of data in computing. It is represented by a 0 or a 1. Binary numbers are made up of binary digits (bits), eg the binary number 1001.

5 0
3 years ago
Read 2 more answers
Why do i keep losing points in brainly.com and how do you lose it?
kati45 [8]

Answer:

because you are asking questions like this

Explanation:

when you ask questions you lose points

6 0
3 years ago
Read 2 more answers
Suppose you were assigned to develop a logical model of the registration system at a school or college. Would you be better off
BaLLatris [955]

Answer:

top down approach

Explanation:

Top down approach is process of breaking down system into sub-system which helps in formulating the overall system. At the end every sub system can be refined with more details.

While bottom up approach is process of gathering systems to design large complex system.

Top down is better as basic information is present and resources can be utilized effectively without impacting maintennance and operations.

6 0
3 years ago
Characteristics of successful entrepreneurs include
crimeas [40]
The answer would be A because grit means to fight hard to get what you want. Submission would be to give in to pressure and give up. Self-importance would be to only focus on yourself and no one else. A short attention span would not work out for you because you get distracted easily.
8 0
4 years ago
Words like "free" or "percent off" will not only trigger spam filters for your email, but will also ___________
weeeeeb [17]

Answer:

eliminasnados

Explanation:

6 0
3 years ago
Other questions:
  • In a typical system design specification, the _____ section contains the complete design for a new system, including the user in
    5·1 answer
  • ZeroIt is a function that takes one argument and returns no value. The argument is a pointer to int. The function stores the val
    15·1 answer
  • External auditing is an independent appraisal function established within an organization to examine and evaluate its activities
    11·1 answer
  • In the accompanying figure, the split double arrow mouse pointer____.
    7·1 answer
  • A company that manufactures machine parts orders a new system that makes products at ten times the speed of the earlier machine.
    6·2 answers
  • Which would be included in a SaaS platform?<br><br> A.data link<br> B.IaaS<br> C.Java<br> D.vb.net
    5·1 answer
  • The encapsulating security ______ protocol provides secrecy for the contents of network communications as well as system-to-syst
    14·1 answer
  • Design a base class, Road, with the following members:
    13·1 answer
  • Name the box where the name of the first cell of the selected range appears?
    7·1 answer
  • Which step do you think is most useful from big data life cycle and why?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!