SimpleBotStatement Each SimpleBotStatement represents a single SimpleBot program
ID: 3588982 • Letter: S
Question
SimpleBotStatement
Each SimpleBotStatement represents a single SimpleBot program statement (eg, 0 x*** -> n 0). Write a class called SimpleBotStatement with the following public methods (it should have no other public methods).
If you consider each grouping of non-whitespace characters in the string to be "tokens", then the sequence of tokens in the string should be: an integer (the trigger state), a four-character string (the trigger sensors), the string "->" (unused), a single character (the action), and an integer (the new state). The four-character token should have the first character one of n/x/*, the second one of e/x/*, the third one of w/x/*, and the fourth one of s/x/*. The single action character should be one of n/e/w/s/x. Any amount of whitespace is allowed before, between and after each token, but these should be the only tokens in the string and they should occur in this order. If any of these expectations are broken, an IllegalArgumentException should be thrown.
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.
This method should return the new state value found by the constructor.
This method should return the action value found by the constructor.
Explanation / Answer
Given below is the code for the class specified in the question. 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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.