JAVAAAAAAA help me write this method!!!!!! /** * This randomly selects, with the
ID: 3920764 • Letter: J
Question
JAVAAAAAAA help me write this method!!!!!!
/**
* This randomly selects, with the Random number generator passed as a parameter,
* one of elements (array of int) in the neighbors list. If the size of neighbors is 0 (empty)
* then null is returned. If neighbors contains 1 element then that element is returned. The
* randGen parameter is only used to select 1 element from a neighbors list containing
* more than 1 element. If neighbors or randGen is null then an error message is
* printed to System.err and null is returned.
*
* @param neighbors A list of potential neighbors to choose from.
* @param randGen The random number generator used throughout the simulation.
* @return A int[] containing the coordinates of a creatures move or null as specified above.
*/
public static int[] chooseMove(ArrayList<int[]> neighbors, Random randGen) {
//TODO Milestone 2
return null; //change in Milestone 2
}
Explanation / Answer
public static int[] chooseMove(ArrayList neighbors, Random randGen) { //If neighbors is empty, or randGen is null, return null if (neighbors == null || neighbors.isEmpty() || randGen == null) { System.err.print("null"); return null; } //If neighbors has a single element, return that element if (neighbors.size() == 1) { return neighbors.get(0); } //randomly return an element return neighbors.get(randGen.nextInt() % neighbors.size()); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.