Question: Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug

Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug 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:

public Bug (int initialPosition) and methods public void turn() public void move()

public Bug (int initialPosition) and methods public void turn() public void move() public int getPosition () Sample usage: Bug bugsy = new Bug (10); bugsy.move(); // Now the position is 11 bugsy.turn(); bugsy.move(); // Now the position is 10 Your BugTester should construct a bug, make it move and turn a few times, and print the actual and expected position.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the question provided you need to write a simple class in Java that simulates a bug moving ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!