Question: design a state machine for this code MarsRover.java import java.util.Scanner; public class MarsRover { private String inst_set; private Plateau p; private Cell currentLocation; public enum
design a state machine for this code
MarsRover.java
import java.util.Scanner;
public class MarsRover {
private String inst_set;
private Plateau p;
private Cell currentLocation;
public enum Direction{N,S,E,W}
Direction dir;
public MarsRover(Plateau p1, int x, int y, Direction dir, String instSet) {
p=p1;
inst_set=instSet;
this.dir=dir;
currentLocation=new Cell(x, y);
}
public MarsRover(){}
private void executeInst() {
int i = 0;
while (i < inst_set.length()) {
char inst = inst_set.charAt(i++); //get each inst separately as a character
if (inst == 'M') {
Cell nextCell = p.getNeighbour(dir, currentLocation);
if (nextCell != null)
currentLocation = nextCell;
else
System.out.println("This move is not possible. Going to next Instruction");
}
if (inst == 'L' || inst == 'R') {
dir = setDirection(dir, inst);
}
}
}
public void showCurrentLocation(){
System.out.println("Current Cell is:");
System.out.print(currentLocation.x);
System.out.print("\t"+currentLocation.y);
System.out.print("\t"+dir+" ");
}
public Direction setDirection(Direction dir, char inst) {
if(inst=='L') {
switch (dir) {
case N:
dir = Direction.W;
break;
case W:
dir = Direction.S;
break;
case S:
dir = Direction.E;
break;
case E:
dir = Direction.N;
break;
}
}
else if(inst=='R'){
switch (dir) {
case N:
dir = Direction.E;
break;
case W:
dir = Direction.N;
break;
case S:
dir = Direction.W;
break;
case E:
dir = Direction.S;
break;
}
}
return dir;
}
public static void main(String... a){
Plateau p1=new Plateau();
Scanner in = new Scanner(System.in);
int x,y;
String instSet;
String dir;
System.out.println("Enter the direction the rover is Headed");
dir=in.nextLine();
System.out.println("Enter the Initial XLocation:");
x=in.nextInt();
in.nextLine();
System.out.println("Enter the Initial YLocation:");
y=in.nextInt();
in.nextLine();
System.out.println("Enter the Instructions\t");
instSet=in.nextLine();
MarsRover mr1=new MarsRover(p1,x,y,Direction.valueOf(dir),instSet);
mr1.executeInst();
mr1.showCurrentLocation();
}
}
Plateau.java
import java.util.Scanner;
class Plateau {
private int size;
public Plateau() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the Size of the plateau");
size=in.nextInt();
in.nextLine();
}
public Plateau(int sz){
size=sz;
}
public Cell getNeighbour(MarsRover.Direction dir,Cell c){
Cell neighbour = null;
switch (dir) {
case N:
neighbour=new Cell(c.x,c.y+1);
break;
case W:
neighbour=new Cell(c.x-1,c.y);
break;
case S:
neighbour=new Cell(c.x,c.y-1);
break;
case E:
neighbour=new Cell(c.x+1,c.y);
break;
}
if((neighbour.x>=0)&&(neighbour.y>=0)&&(neighbour.x return neighbour; return null; } } Cell.java class Cell{ int x; int y; public Cell(int PosX, int PosY) { x=PosX; y=PosY; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
