package simulator; public class Bus { public final int number; private final Roa
ID: 3884438 • Letter: P
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 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
Explanation / Answer
I found that you are using an external interface or class called RoadMap which is passed as an argument to the Bus constructor when an object is created for the for Bus class. But I did not find any implementation regarding the RoadMap and its method. RoadMap also has a method called isRoad which is not implemented in your code.See that the RoadMap is implemented and you will get error free program.
**Code if RoadMap is implemented as an interface:
public interface RoadMap {
boolean isRoad(int x, int i);
}
**Code if RoadMap is implemented as an class:
public class RoadMap {
public boolean isRoad(int x, int i) {
// TODO Auto-generated method stub
// Perform the necessary calculations here and return a boolean value
return false;
}
}
So choice anyone of the above implementation.
**Comment for any further queries
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.