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; 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 goNorth() {

y -= 1;

stored = 1;

}

public void goSouth() {

y += 1;

stored = 2;

}

public void goWest(){

x -= 1;

stored = 3;

}

public void goEast() {

x += 1;

stored = 4;

}

public int goSameDirection() {

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

goNorth();

}

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

goSouth();

}

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

goWest();

}

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

goEast();

}

else {

return 0;

}

return 1;

}

/**

* Move the bus. Buses only move along the cardinal direction (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.

*/

public void move() {

int goX = this.x;

int goY = this.y;

if(stored == -1) {

if(roadMap.isRoad(goX, goY-1)) {

goNorth();

}

else if(roadMap.isRoad(goX, goY+1)) {

goSouth();

}

else if(roadMap.isRoad(goX+1, goY)) {

goWest();

}

else if(roadMap.isRoad(goX-1, goY)) {

goEast();

}

}

else if(stored != -1) {

if(goSameDirection() == 0) {

if(stored == 1) {

if(roadMap.isRoad(goX+1, goY)) {

goEast();

}

else if(roadMap.isRoad(goX-1, goY)) {

goWest();

}

else if(roadMap.isRoad(goX, goY+1)) {

goSouth();

}

else {

stored = -1;

}

}

else if(stored == 2) {

if(roadMap.isRoad(goX-1, goY)) {

goWest();

}

else if(roadMap.isRoad(goX+1, goY)) {

goEast();

}

else if(roadMap.isRoad(goX, goY+1)) {

goNorth();

}

else {

stored = -1;

}

}

}

else if(stored == 4) {

if(roadMap.isRoad(goX, goY-1)) {

goNorth();

}

else if(roadMap.isRoad(goX, goY+1)) {

goSouth();

}

else if(roadMap.isRoad(goX+1, goY)) {

goEast();

}

else {

stored = -1;

}

}

else if (stored == 3) {

if(roadMap.isRoad(goX, goY+1)) {

goSouth();

}

else if(roadMap.isRoad(goX-1, goY)) {

goWest();

}

else {

stored = -1;

}

}

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

stored = -1;

}

}

}

}

===========================================================================================

public class RoadMap {

public final int xSize;

public final int ySize;

private final boolean[][] isRoad;

public RoadMap(int x, int y) {

isRoad = new boolean[x][y];

xSize = x;

ySize = y;

}

public static RoadMap fromString(String s) {

String[] lines = s.trim().split("\\s+");

final int xSize = lines[0].length();

final int ySize = lines.length;

RoadMap roadMap = new RoadMap(xSize, ySize);

for (int y = 0; y < ySize; y++) {

for (int x = 0; x < xSize; x++) {

if (lines[y].charAt(x) == '.') {

roadMap.isRoad[x][y] = true;

}

}

}

return roadMap;

}

public String toString() {

StringBuilder sb = new StringBuilder();

for (int y = 0; y < ySize; y++) {

for (int x = 0; x < xSize; x++) {

if (isRoad[x][y]) {

sb.append('.');

} else {

sb.append('X');

}

}

sb.append(' ');

}

return sb.substring(0, sb.length() - 1);

}

public boolean isRoad(int x, int y) {

if(x<0 || x>xSize-1 || y<0 || y>ySize-1) {

return false;

}

return isRoad[x][y];

}

public void setRoad(int x, int y, boolean isRoad) {

this.isRoad[x][y] = isRoad;

}

}

using an external class called RoadMap which has a method called isRoad as you can see above

I still have error from this broken error like:

testFigureEight (0.0/1.0)

testLessSmallWorldLoopCCW (0.0/1.0)

testZigZag (0.0/1.0)

testLessSmallWorldLoopCW (0.0/1.0)

Can you please fix this for me?

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

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions

Question

proud of something in particular?

Answered: 1 week ago

Question

=+How should it be delivered?

Answered: 1 week ago

Question

=+4 How does one acquire a global mindset?

Answered: 1 week ago