Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package simulator; public class Bus { public final int number; private final Roa

ID: 3884549 • 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 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?

Explanation / Answer

BusVisualizer.java
--------------------------------------------------
import processing.core.PApplet;

public class BusVisualizer extends PApplet {
    static RoadMap roadMap;
    static Bus[] buses;
    static int busCount;

    public static void main(String[] args) {
        PApplet.main("BusVisualizer");
    }

    public void settings() {
        roadMap = new RoadMap(10, 10); // you could replace this with a call to
        // RoadMap.fromString(); see the tests for an example.
        buses = new Bus[5];
        busCount = 0;
        size(roadMap.xSize * 20, roadMap.ySize * 20);
    }

    public void setup() {
    }

    public void draw() {
        background(223);

        // first draw the road/non-road
        stroke(255);
        for (int y = 0; y < roadMap.ySize; y++) {
            for (int x = 0; x < roadMap.xSize; x++) {
                if (roadMap.isRoad(x, y)) {
                    fill(31);
                }
                else {
                    fill(223);
                }
                rect(x*20,y*20,20,20);
            }
        }

        // now draw the buses (numbers)
        fill(255);
        stroke(255);
        for (int i = 0; i < busCount; i++) {
            Bus b = buses[i];
            text(Integer.toString(b.number), b.getX()*20, b.getY()*20, 20, 20);
        }
    }

    public void keyPressed() {
        if (key == ' ') {
            for (int i = 0; i < busCount; i++) {
                buses[i].move();
            }
        }
        else if (key == BACKSPACE || key == DELETE) {
            if (busCount > 0) {
                busCount--;
                buses[busCount] = null;
            }
        }
    }

    public void mousePressed() {
        int x = mouseX / 20;
        int y = mouseY / 20;
        if (mouseButton == LEFT) {
            boolean isClear = true;
            for (int i = 0; i < busCount; i++) {
                Bus b = buses[i];
                if (b.getX() == x && b.getY() == y) {
                    isClear = false;
                    break;
                }
            }
            if (isClear) {
                roadMap.setRoad(x, y, !roadMap.isRoad(x, y));
            }
        }

        if (mouseButton == RIGHT) {
            // is there space for a new bus and is the cell a road?
            if ((busCount < buses.length) && (roadMap.isRoad(x, y))) {
                // and, is it clear (no bus)?
                boolean isClear = true;
                for (int i = 0; i < busCount; i++) {
                    Bus b = buses[i];
                    if (b.getX() == x && b.getY() == y) {
                        isClear = false;
                        break;
                    }
                }
                if (isClear) {
                    buses[busCount] = new Bus(busCount, roadMap, x, y);
                    busCount++;
                }
            }
        }
    }
}
---------------------------------------------------------------------------
Bus.java
----------------------------------
import java.awt.Point;

public class Bus {
    public final int number;
    private final RoadMap roadMap;
    private int x;
    private int y;
    Point movedLastTurn;

    public Bus(int number, RoadMap roadMap, int x, int y) {
        this.number = number;
        this.roadMap = roadMap;
        this.x = x;
        this.y = y;
        movedLastTurn = new Point(0,0);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    //Right, left back, stop
    public void move() {
        if(movedLastTurn.equals(new Point(0,0))){
            if(doesNorthExist()) moveNorth();
            else if(doesSouthExist()) moveSouth();
            else if(doesEastExist()) moveEast();
            else if(doesWestExist()) moveWest();
        }else{
            if(movedLastTurn.getY() == -1.0){
                if(doesNorthExist()) moveNorth();
                else if(doesEastExist()) moveEast();
                else if(doesWestExist()) moveWest();
                else if(doesSouthExist()) moveSouth();
                else movedLastTurn = new Point(0,0);
            }
            else if(movedLastTurn.getY() == 1.0){
                if(doesSouthExist()) moveSouth();
                else if(doesWestExist()) moveWest();
                else if(doesEastExist()) moveEast();
                else if(doesNorthExist()) moveNorth();
                else movedLastTurn = new Point(0,0);
            }
            else if(movedLastTurn.getX() == -1.0){
                if(doesWestExist()) moveWest();
                else if(doesNorthExist()) moveNorth();
                else if(doesSouthExist()) moveSouth();
                else if(doesEastExist()) moveEast();
                else movedLastTurn = new Point(0,0);
            }
            else if(movedLastTurn.getX() == 1.0){
                if(doesEastExist()) moveEast();
                else if(doesSouthExist()) moveSouth();
                else if(doesNorthExist()) moveNorth();
                else if(doesWestExist()) moveWest();
                else movedLastTurn = new Point(0,0);
            }
        }
    }

    private void moveSouth(){
        y++;
        movedLastTurn = new Point(0,1);
    }

    private void moveNorth(){
        y--;
        movedLastTurn = new Point(0,-1);
    }

    private void moveEast(){
        x++;
        movedLastTurn = new Point(1,0);
    }

    private void moveWest(){
        x--;
        movedLastTurn = new Point(-1,0);
    }


    private boolean doesNorthExist(){
        return ( y-1>=0 && roadMap.isRoad(x, y-1));
    }

    private boolean doesSouthExist(){
        return (y+ 1 <= roadMap.ySize && roadMap.isRoad(x, y+1));
    }

    private boolean doesEastExist(){
        return (x + 1 <= roadMap.xSize && roadMap.isRoad(x+1,y));
    }

    private boolean doesWestExist(){
        return (x - 1 >= 0 && roadMap.isRoad(x-1, y));
    }
}
---------------------------------------------------------------------------
RoadMap.java
-----------------------------------------
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>= xSize || y>=ySize || x<0 || y<0) return false;
        else return isRoad[x][y];
    }

    public void setRoad(int x, int y, boolean isRoad) {
        this.isRoad[x][y] = isRoad;
    }

}
---------------------------------------------------------------
BusTest.java
------------------------------
import static org.junit.Assert.*;

import org.junit.Test;


public class BusTest {

    @Test
    public void testConstructor() {
        Bus b = new Bus(1, new RoadMap(0, 0), 2, 3);
        assertEquals(1, b.number);
        assertEquals(2, b.getX());
        assertEquals(3, b.getY());
    }

    @Test
    public void testMoveNorthFromStop() {
        Bus b = new Bus(0, RoadMap.fromString("X.X X.X X.X"), 1, 2);
        assertEquals(1, b.getX());
        assertEquals(2, b.getY());
        b.move();
        assertEquals(1, b.getX());
        assertEquals(1, b.getY());
    }

    @Test
    public void testMoveNorthTwice() {
        Bus b = new Bus(0, RoadMap.fromString("X.X X.X X.X"), 1, 2);
        assertEquals(1, b.getX());
        assertEquals(2, b.getY());
        b.move();
        assertEquals(1, b.getX());
        assertEquals(1, b.getY());
        b.move();
        assertEquals(1, b.getX());
        assertEquals(0, b.getY());
    }

    @Test
    public void testMoveNorthThenEastOneWay() {
        Bus b = new Bus(0, RoadMap.fromString("XXX X.. X.X"), 1, 2);
        assertEquals(1, b.getX());
        assertEquals(2, b.getY());
        b.move();
        assertEquals(1, b.getX());
        assertEquals(1, b.getY());
        b.move();
        assertEquals(2, b.getX());
        assertEquals(1, b.getY());
    }

    @Test
    public void testMoveSouthThenWestAtTee() {
        Bus b = new Bus(0, RoadMap.fromString("X.X ... XXX"), 1, 0);
        assertEquals(1, b.getX());
        assertEquals(0, b.getY());
        b.move();
        assertEquals(1, b.getX());
        assertEquals(1, b.getY());
        b.move();
        assertEquals(0, b.getX());
        assertEquals(1, b.getY());
    }
}
-------------------------------------------------------------------------
RoadMapTest.java
------------------------------------------
import static org.junit.Assert.*;

import org.junit.Test;


public class RoadMapTest {
    @Test
    public void testConstructor() {
        RoadMap r = new RoadMap(1,1);
        assertNotNull(r);
    }

    @Test
    public void testFromString() {
        RoadMap r = RoadMap.fromString("... " +
                ".X. " +
                "X.X");
        assertTrue(r.isRoad(0, 0));
        assertTrue(r.isRoad(1, 0));
        assertTrue(r.isRoad(2, 0));
        assertTrue(r.isRoad(0, 1));
        assertFalse(r.isRoad(1, 1));
        assertTrue(r.isRoad(2, 1));
        assertFalse(r.isRoad(0, 2));
        assertTrue(r.isRoad(1, 2));
        assertFalse(r.isRoad(2, 2));
    }

    @Test
    public void testToString() {
        RoadMap r = RoadMap.fromString("X.X ... .X.");
        assertEquals("X.X ... .X.", r.toString());
    }

    @Test
    public void testSetRoad() {
        RoadMap r = new RoadMap(2,2);
        assertFalse(r.isRoad(0, 1));
        r.setRoad(0, 1, true);
        assertTrue(r.isRoad(0, 1));
        r.setRoad(0, 1, false);
        assertFalse(r.isRoad(0, 1));
    }
}