Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

Discuss the key ambient conditions and their effects on customers.

Answered: 1 week ago

Question

Understand the roles of signs, symbols, and artifacts.

Answered: 1 week ago