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

This method will be used to match a robot\'s current state and sensor readings a

ID: 3588981 • Letter: T

Question

This method will be used to match a robot's current state and sensor readings against a program statement to see if they are a match. The parameters n/e/w/s will each be true if there is a wall in the immediately adjacent square. If state does not match "the trigger state" return false. If n is true and the first character of the trigger sensors is x, or n is false and the first character of the trigger sensors is n, return false. The remaining parameters should behave similarly to n except with the second, third and fourth characters of the trigger sensors. If none of these tests causes the method to return false, then true should be returned.

Explanation / Answer

I have answered this question earlier and I think this is for SimpleBotStatement.

Given below is the code for the class SimpleBotStatement. Hope the answer helped. If it did, please don't forget to rate it. Thank you.

To indent code in eclipse, select the code by pressing Ctrl+A and then Ctrl+i


public class SimpleBotStatement {
private int state;
private String sensors;
private int nextState;
private char action;
public SimpleBotStatement(String s)
{
String[] tokens = s.split("(\s)+"); //split tokens using 1 or more whitespaces
try{
state = Integer.parseInt(tokens[0]);
sensors = tokens[1];
checkSensors(); //call the private method to check if the trigger sensors are valid
if(!tokens[2].equals("->"))
throw new IllegalArgumentException("Expected -> after trigger sensors . Found " + tokens[2]);
//next token should be single char action, check if its valid value
String validAction = "newsx";
if(tokens[3].length() != 1 || validAction.indexOf(tokens[3]) == -1 )
throw new IllegalArgumentException("Action should be a single character and one of n/e/w/s/x");
action = tokens[3].charAt(0);
nextState = Integer.parseInt(tokens[4]);
}catch(Exception e)
{
throw new IllegalArgumentException(e.getMessage());
}
}
public boolean match(int state, boolean n, boolean e, boolean w, boolean s)
{
if(this.state != state)
return false;
char ch = sensors.charAt(0);
if((n == true && ch == 'x' ) || (n == false && ch =='n'))
return false;
ch = sensors.charAt(1);
if((e == true && ch == 'x' ) || (e == false && ch =='e'))
return false;
ch = sensors.charAt(2);
if((w == true && ch == 'x' ) || (w == false && ch =='w'))
return false;
ch = sensors.charAt(3);
if((n == true && ch == 'x' ) || (s == false && ch =='s'))
return false;
return true;
}
public int nextState()
{
return nextState;
}
public char nextAction()
{
return action;
}
private void checkSensors()
{
boolean valid = true;
String err = "";
if(sensors.length() == 4)
{
String[] allowed = {"nx*", "ex*", "wx*", "sx*"}; //allowed characters for each of character positions
//if position is 0, then only n, x * are allowed
//if position is 1, then only e, x and * are allowed and so on
//now check if each character has valid character according to its position
for(int pos = 0; valid && pos < sensors.length(); pos++)
{
char ch = sensors.charAt(pos);
if(allowed[pos].indexOf(ch) == -1)
{
err = "Trigger Sensors invalid. Expected one of the characters in " + allowed[pos]+" at index "
+ pos + ". Found "+ ch;
valid = false;
}
}
}
else
{
err = "Trigger sensors be only 4 characters long";
valid = false;
}
if(!valid)
throw new IllegalArgumentException(err);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote