Question: Write a class Bug that models a bug moving in the x-y plane. The bug either moves north, east, south, or west. Initially, the bug

Write a class Bug that models a bug moving in the x-y plane. The bug either moves north, east, south, or west. Initially, the bug moves to the north, but it can turn to the right or the left by 90 degrees at a time. In each move, its position changes by one unit in the current direction. Provide a constructor

public Bug(intinitX, intinitY) // places the bug at initial position

// initX, initY

and methods:

public void turnRight() // turn right by 90 degrees

public void turnLeft() // turn left by 90 degrees

public void move() // move in current direction by 1 unit

public intgetPositionX() // get X position

public intgetPositionY() // get Y position

public String getDirection() // returns the direction (e.g. east)

Also, you must override the method public String toString() so that it returns a string that displays the bugs position (x and y coordinates) as well as the direction that it is currently facing. Note: you dont need to draw the bug on the screen (we have not learned how to do this yet). You just need the bug object to keep track of its current position and direction.

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!