Question: Write an error-free Java program to do the following things. Type your program and turn in at the beginning of class on 28 April 2017.

Write an error-free Java program to do the following things. Type your program and turn in at the beginning of class on 28 April 2017.

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 at random times it will turn and change its direction. In each move, its position changes by one unit in the current direction.

Provide a constructor:

public Bug(int initialPosition)

Provide methods: void turn(), void move(), int getPosition().

The main program should construct three bugs, have them all start at position = 5 (use the constructor!), make them move and turn them approximately of the time but not necessarily at the same time.

Use a loop to make the bugs move 10 times with an occasional turn. Have a race between the bugs and print the position of each bug after each move and declare a winner at the end of 10 moves.

Sample output

On your mark ... get set ... MMMPosition of bugs MMM6 6 6 MMM7 7 7 MMM8 8 8 MMM9 9 7 MMM10 10 6 MMM11 11 7 MMM12 12 6 MMM13 13 5 MMM14 12 4 MMM15 11 5 MMMThe winner is bug 1 MMM

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!