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 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 1/4 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 ... Position of bugs 6 6 6 7 7 7 8 8 8 9 9 7 10 10 6 11 11 7 12 12 6 13 13 5 14 12 4 15 11 5 The winner is bug 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
