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

Can anyone help me with the start method? its not work. import java.util.ArrayLi

ID: 3606904 • Letter: C

Question

Can anyone help me with the start method? its not work.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class SimpleBotController {
public void readWorld(Scanner in) {
List> charArrList = new ArrayList<>();
int lineLength = 0;
int countB = 0;
int countW = 0;
int countT = 0;
boolean firstLine = true;
while (in.hasNextLine()) { // check for next line
String text = in.nextLine(); // read next line
char[] arr = text.toCharArray();
List carr = new ArrayList<>();
int loopCountR = 0;
int loopCountB = 0;
int loopCountW = 0;
int loopCountT = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'r' || arr[i] == 'b' || arr[i] == 'w' || arr[i] == 't' || arr[i] == ' ' || arr[i] == ' ') {
if (arr[i] == 'r' && loopCountR == 1) {
throw new IllegalArgumentException("Found more than 1 r");
}
if (arr[i] != ' ' && arr[i] != ' ') {
if (arr[i] == 'b') loopCountB++;
if (arr[i] == 'w') loopCountW++;
if (arr[i] == 't') loopCountT++;
if (arr[i] == 'r') loopCountR++;
carr.add(arr[i]);
}
}
else {
throw new IllegalArgumentException("Char other than r, b, w, t found");
}
}
charArrList.add(carr);
if (!firstLine && (countB != loopCountB || countW != loopCountW || countT != loopCountT)) {
throw new IllegalArgumentException("Different length line found");
}
else {
firstLine = false;
countB = loopCountB;
countW = loopCountW;
countT = loopCountT;
}
}
int size = charArrList.size();
lineLength = 1 + countB + countT + countW;
char[][] array = new char[size][lineLength];
for (int i = 0; i < size; i++) {
List temp = charArrList.get(i);
for (int j = 0; j < lineLength; j++) {
array[i][j] = temp.get(j);
}
}
}
  
public void readProgram(Scanner in) {

while (in.hasNextLine()) {
String statement = in.nextLine();
int index = statement.indexOf('#');
if (index != -1)
statement = statement.substring(0, index);
statement = statement.trim();
if (!statement.isEmpty()) {
try {
SimpleBotStatement sb = new SimpleBotStatement(statement);
}
catch(IllegalArgumentException e) {
System.out.println("IllegalArgumentException is thrown as a program is illegally formatted");
throw e;
}
}
}

}
  

public void start() {

if(world==null||program==null){
throw new IllegalStatement("must load legal world and state");}
SimpleBotModel sbm= new SimpleBotModel(world,program);

}

-----------------------------------------------------------------------------

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);
}
}

-------------------------------------------------------------------

import java.util.Scanner;
import java.util.Arrays;

public class SimpleBotControllerTester {

public static void main(String[] args) {
boolean pass = true;
SimpleBotController c = new SimpleBotController();

try { c.start(); pass = false; }
catch(IllegalStateException e) { }
catch(Exception e) { pass = false; }

try { c.readWorld(null); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

try { c.readWorld(new Scanner("bb wt ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

try { c.readWorld(new Scanner("rb wr ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

try { c.readWorld(new Scanner("rb wx ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

try { c.readWorld(new Scanner("rb www ")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

c.readWorld(new Scanner(" t b w t w r "));
c.readProgram(new Scanner("# 0 **** -> x 0 0 **** -> x 0 # Hello # Hello"));
c.start();
pass = pass && Arrays.deepToString(SimpleBotModel.world).equals("[[t, b], [w, t], [w, r]]");
pass = pass && SimpleBotModel.program.length==2;

try { c.readProgram(new Scanner("dkfjhsdkjfh")); pass = false; }
catch(IllegalArgumentException e) { }
catch(Exception e) { pass = false; }

System.out.println(pass);
}

}

-------------------------------------------------------------

public class SimpleBotModel {

public static char[][] world;
public static SimpleBotStatement[] program;

public SimpleBotModel(char[][] world, SimpleBotStatement[] program) {
SimpleBotModel.world = world;
SimpleBotModel.program = program;
}

}

more information is here

http://krovetz.net/20/project2/SimpleBotController.html

Explanation / Answer

Hi,

Your problem with resolved for start method.

import java.util.Scanner;

import java.util.Arrays;

public class SimpleBotControllerTester {

public static void main(String[] args) {

boolean pass = true;

SimpleBotController c = new SimpleBotController();

try {

c.start();

pass = false;

} catch (IllegalStateException e) {

} catch (Exception e) {

pass = false;

}

try {

c.readWorld(null);

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

try {

c.readWorld(new Scanner("bb wt "));

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

try {

c.readWorld(new Scanner("rb wr "));

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

try {

c.readWorld(new Scanner("rb wx "));

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

try {

c.readWorld(new Scanner("rb www "));

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

Scanner scan = new Scanner("t b w t w r ");

c.readWorld(scan);

c.readProgram(new Scanner("# 0 **** -> x 0 0 **** -> x 0 # Hello # Hello"));

c.start();

pass = pass && Arrays.deepToString(SimpleBotModel.world).equals("[[t, b], [w, t], [w, r]]");

pass = pass && SimpleBotModel.program.length == 2;

try {

c.readProgram(new Scanner("dkfjhsdkjfh"));

pass = false;

} catch (IllegalArgumentException e) {

} catch (Exception e) {

pass = false;

}

System.out.println(pass);

}

}

////////////////////////

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class SimpleBotController {

List<List> charArrList = new ArrayList<>();

public void readWorld(Scanner in) {

int lineLength = 0;

int countB = 0;

int countW = 0;

int countT = 0;

boolean firstLine = true;

while (in.hasNextLine()) { // check for next line

String text = in.nextLine(); // read next line

char[] arr = text.toCharArray();

List carr = new ArrayList<>();

int loopCountR = 0;

int loopCountB = 0;

int loopCountW = 0;

int loopCountT = 0;

for (int i = 0; i < arr.length; i++) {

if (arr[i] == 'r' || arr[i] == 'b' || arr[i] == 'w' || arr[i] == 't' || arr[i] == ' '

|| arr[i] == ' ') {

if (arr[i] == 'r' && loopCountR == 1) {

throw new IllegalArgumentException("Found more than 1 r");

}

System.out.println("SimpleBotController.readWorld()"+arr[i]);

if (arr[i] != ' ' && arr[i] != ' ') {

if (arr[i] == 'b')

loopCountB++;

if (arr[i] == 'w')

loopCountW++;

if (arr[i] == 't')

loopCountT++;

if (arr[i] == 'r')

loopCountR++;

carr.add(arr[i]);

}

} else {

throw new IllegalArgumentException("Char other than r, b, w, t found");

}

}

charArrList.add(carr);

if (!firstLine && (countB != loopCountB || countW != loopCountW || countT != loopCountT)) {

throw new IllegalArgumentException("Different length line found");

} else {

firstLine = false;

countB = loopCountB;

countW = loopCountW;

countT = loopCountT;

}

}

int size = charArrList.size();

lineLength = 1 + countB + countT + countW;

arrayworld = new char[size][lineLength];

for (int i = 0; i < size; i++) {

List temp = charArrList.get(i);

for (int j = 0; j < lineLength; j++) {

arrayworld[i][j] = (char) temp.get(j);

}

}

}

char[][] arrayworld;

List<SimpleBotStatement> program=new ArrayList<>();

public void readProgram(Scanner in) {

while (in.hasNextLine()) {

String statement = in.nextLine();

int index = statement.indexOf('#');

if (index != -1)

statement = statement.substring(0, index);

statement = statement.trim();

if (!statement.isEmpty()) {

try {

SimpleBotStatement sb = new SimpleBotStatement(statement);

program.add(sb);

} catch (IllegalArgumentException e) {

System.out.println("IllegalArgumentException is thrown as a program is illegally formatted");

throw e;

}

}

}

}

public void start() {

if(arrayworld==null||program==null)

throw new IllegalArgumentException(("must load legal world and state"));

SimpleBotModel sbm= new SimpleBotModel(arrayworld,program.toArray(

new SimpleBotStatement[program.size()]));

}

}

\\\\\\\\\\\\\\\\\

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);

}

}

/////////////////////////////////////////////////

public class SimpleBotModel {

public static char[][] world;

public static SimpleBotStatement[] program;

public SimpleBotModel(char[][] world, SimpleBotStatement[] program) {

SimpleBotModel.world = world;

SimpleBotModel.program = program;

}

}

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