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

package simulator;

public class Bus {

public final int number;

private final RoadMap roadMap;

private int x;

private int y;

private int stored;

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

this.number = number;

this.roadMap = roadMap;

this.x = x;

this.y = y;

this.stored = -1;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

public void moveNorth() {

y -= 1;

stored = 1;

}

public void moveSouth() {

y += 1;

stored = 2;

}

public void moveWest() {

x -= 1;

stored = 3;

}

public void moveEast() {

x += 1;

stored = 4;

}

public int moveSameDirection(){

if(stored == 1 && roadMap.isRoad(x, y-1)){

moveNorth();

}

else if(stored == 2 && roadMap.isRoad(x, y+1)){

moveSouth();

}

else if(stored == 3 && roadMap.isRoad(x-1, y)) {

moveWest();

}

else if(stored == 4 && roadMap.isRoad(x+1, y)){

moveEast();

}

else {

return 0;

}

return 1;

}

/**

* Move only (north, south, west, east)

* 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() {

int moveX = this.x;

int moveY = this.y;

if(stored == -1) {

if(roadMap.isRoad(moveX, moveY - 1)){

moveNorth();

}

else if(roadMap.isRoad(moveX, moveY + 1)){

moveSouth();

}

else if(roadMap.isRoad(moveX + 1, moveY)){

moveWest();

}

else if(roadMap.isRoad(moveX - 1, moveY)){

moveEast();

}

}

else if(stored != -1){

if(moveSameDirection() == 0){

if(stored == 1){

if(roadMap.isRoad(moveX + 1, moveY)){

moveEast();

}

else if(roadMap.isRoad(moveX - 1, moveY)){

moveWest();

}

else if(roadMap.isRoad(moveX, moveY + 1)){

moveSouth();

}

else{

stored = -1;

}

}

else if(stored == 2){

if(roadMap.isRoad(moveX - 1, moveY)){

moveWest();

}

else if(roadMap.isRoad(moveX + 1, moveY)){

moveEast();

}

else if(roadMap.isRoad(moveX, moveY + 1)){

moveNorth();

}

else{

stored = -1;

}

}

}

else if(stored == 4){

if(roadMap.isRoad(moveX, moveY - 1)){

moveNorth();

}

else if(roadMap.isRoad(moveX, moveY + 1)){

moveSouth();

}

else if(roadMap.isRoad(moveX + 1, moveY)){

moveEast();

}

else{

stored = -1;

}

}

else if(stored == 3){

if(roadMap.isRoad(moveX, moveY + 1)){

moveSouth();

}

else if(roadMap.isRoad(moveX, moveY - 1)){

moveNorth();

}

else if(roadMap.isRoad(moveX - 1, moveY)){

moveWest();

}

else{

stored = -1;

}

}

else if(stored!=1 && stored!=2 && stored!=3 && stored!=4)

stored =-1;

}

}

}

getting errors here

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!