Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions

Question

What is the LIFO reserve, and when is it used?

Answered: 1 week ago

Question

11-1 What is the role of knowledge management systems in business?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago