Westonci.ca is your go-to source for answers, with a community ready to provide accurate and timely information. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.
Sagot :
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.
Thank you for visiting. Our goal is to provide the most accurate answers for all your informational needs. Come back soon. We hope our answers were useful. Return anytime for more information and answers to any other questions you have. Thank you for visiting Westonci.ca, your go-to source for reliable answers. Come back soon for more expert insights.