Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago