Question: In this exercise, you are to write a simple object-oriented program to simulate the movement of a bug along a horizontal line on the floor

In this exercise, you are to write a simple object-oriented program to simulate the movement of a bug along

a horizontal line on the floor of a room. The horizontal line starts at the 0th position and ends at the 50th

position. In other words, the length of the horizontal line is 51 steps - there is a wall at the left and right

ends of the line.

The bug starts at an initial position between 0 and 50 (both inclusive) and an initial direction, left or right.

It can move one step at a time, either to the left or to the right (depending upon its direction). If it is at the

left wall, it changes direction and moves one step. Similarly, if it is at the right wall, it changes direction

and moves one step.

You need to write a class called Bug that models the bugs position and direction, and has the method

move() in addition to other methods. The tester program simulates a bug at an initial random position

between 0 and 50, makes it move a random number of steps and displays its initial and final position. The

tester program has been given! All you need to write is the code for the Bug class. Complete the code,

compile and run it, and you should see results similar to the one given at the end of the program below.

FULL QUESTION IN IMAGES BELOW

In this exercise, you are to write a simple object-oriented program tosimulate the movement of a bug along a horizontal line on thefloor of a room. The horizontal line starts at the 0th position

Exercise 1: In this exercise, you are to write a simple object-oriented program to simulate the movement of a bug along a horizontal line on the floor of a room. The horizontal line starts at the 0th position and ends at the 50th position. In other words, the length of the horizontal line is 51 steps there is a wall at the left and right ends of the line. The bug starts at an initial position between 0 and 50 (both inclusive) and an initial direction, left or right. It can move one step at a time, either to the left or to the right depending upon its direction). If it is at the left wall, it changes direction and moves one step. Similarly, if it is at the right wall, it changes direction and moves one step You need to write a class called Bug that models the bug's position and direction, and has the method move 0 in addition to other methods. The tester program simulates a bug at an initial random position between 0 and 50, makes it move a random number of steps and displays its initial and final position. The tester program has been given! All you need to write is the code for the Bug class. Complete the code. compile and run it, and you should see results similar to the one given at the end of the program below public class Bug /TODO: add the attributes an integer variable for position

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!