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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started