Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package simulator; public class Bus { public final int number; private final RoadMap roadMap; private int x; private int y; public Bus(int number, RoadMap roadMap,

package simulator;

public class Bus {

public final int number;

private final RoadMap roadMap;

private int x;

private int y;

public Bus(int number, RoadMap roadMap, int x, int y) {

this.number = number;

this.roadMap = roadMap;

this.x = x;

this.y = y;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

/**

* Move the bus. Buses only move along the cardinal directions

* (north/south/east/west), not diagonally.

*

* If the bus is stopped (that is, if it was just placed, or if it didn't

* move last time move() was called), then it should attempt to move north.

* If it cannot (no road, or off the map), then it should attempt south,

* then east, then west. If no move is available, it should stay in its

* current position.

*

* If the bus is moving (that is, if it successfully moved the last time

* move() was called), then it should attempt to continue moving in the same

* direction.

*

* If it cannot (no road, or off the map), then it should attempt to turn

* right. For example, if the bus was moving north, but there is no more

* road to the north, it should move east if possible.

*

* If it cannot turn right, it should turn left. If it cannot turn left, it

* should reverse direction (that is, move backward, if possible).

* If it cannot do any of these things, it should stay in its current position.

* @param x

* @param y

*/

public void move() {

if(roadMap.isRoad(x,y-1)){ //north

y=y-1;

}

else if(roadMap.isRoad(x,y+1)){ //south

y=y+1;

}

else if(roadMap.isRoad(x+1,y)){ //east

x=x+1;

}

else if(roadMap.isRoad(x-1, y)){ //west

x=x-1;

}

else {

//stay//

}

}

}

please fix the move() method

thank you so much

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

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions

Question

Q.No.1 Explain Large scale map ? Q.No.2 Explain small scale map ?

Answered: 1 week ago

Question

1. Signs and symbols of the map Briefly by box ?

Answered: 1 week ago

Question

5. Who should facilitate the focus group?

Answered: 1 week ago