JAVAAAAAAA help me write this method please!!!!!! /** * This attempts to move ea
ID: 3920765 • Letter: J
Question
JAVAAAAAAA help me write this method please!!!!!!
/**
* This attempts to move each fish each chronon.
*
* This is a key method with a number of parameters. Check that the parameters are valid
* prior to writing the code to move a fish. The parameters are checked in the order they
* appear in the parameter list.
* If any of the array parameters are null or not at least 1 element in size then
* a helpful error message is printed out and -1 is returned. An example message for
* an invalid fish array is "fishSwimAndBreed Invalid fish array: Null or not at least 1 in
* each dimension.". Testing will not check the content of the message but will check whether
* the correct number is returned for the situation. Passing this test means we know fish[0]
* exists and so won't cause a runtime error and also that fish[0].length is the width. For this
* project it is safe to assume rectangular arrays (arrays where all the rows are the same
* length).
* If fishBreed is less than zero a helpful error message is printed out
* and -2 is returned.
* If randGen is null then a helpful error message is printed out and -3 is returned.
*
*
* Algorithm:
* for each fish that has not moved this chronon
* get the available unoccupied positions for the fish to move (call unoccupiedPositions)
* choose a move from those positions (call chooseMove)
* Based on the move chosen, either the
* fish stays (call aFishStays)
* fish moves (call aFishMoves)
* or fish moves and breeds (call aFishMovesAndBreeds)
*
*
* @param fish The array containing all the ages of all the fish.
* @param sharks The array containing all the ages of all the sharks.
* @param fishMove The array containing the indicator of whether each fish moved this
* chronon.
* @param fishBreed The age in chronon that a fish must be to breed.
* @param randGen The instance of the Random number generator.
* @return -1, -2, -3 for invalid parameters as specified above. After attempting to move all
* fish 0 is returned indicating success.
*/
public static int fishSwimAndBreed(int[][] fish, int[][] sharks, boolean[][] fishMove,
int fishBreed, Random randGen) {
//TODO Milestone 2
return 0;
}
Explanation / Answer
public static int fishSwimAndBreed(int[][] fish, int[][] sharks, boolean[][] fishMove, int fishBreed,
Random randGen) {
if (fishBreed < 0) {
System.out.println("fishSwimAndBreed Invalid fishBreed int: under the value of 0");
return -2;
} else if (randGen.equals(null)) {
System.out.println("fishSwimAndBreed Invalid random generator: value is null");
return -3;
} else if (fishMove[0].length == 0 || sharks[0].length == 0 || fish[0].length == 0) {
System.out.println("fishSwimAndBreed Invalid fish array: Null or not at least 1 ineach dimension.");
return -1;
} else {
for (int i = 0; i < fishMove.length; i++) {
for (int j = 0; j < fishMove[i].length; j++) {
// System.out.println("i,j" + i + " " + j);
ArrayList<int[]> positions = unoccupiedPositions(fish, sharks, i, j);
if (fishMove[i][j] || positions.size() == 0) {
aFishStays(fish, fishMove, i, j);
} else {
fishBreed = randGen.nextInt(1);
chooseMove(positions, randGen);
if (fishBreed == 0) {
aFishMovesAndBreeds(fish, fishMove, i, j, positions.get(0)[0], positions.get(0)[1]);
} else {
aFishMoves(fish, fishMove, i, j, positions.get(0)[0], positions.get(0)[1]);
}
}
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.