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

Java reusing methods to generate random position. Okay so my program is creating

ID: 3884156 • Letter: J

Question

Java reusing methods to generate random position.

Okay so my program is creating a 2D array fish tank (8 rows, 32 columns) filled with tilde (~) characters with 4 randomly generated positions of fish ( ><))'> ), 6 pieces of fish good (*) and a hook (J). My code so far has the fish, but I am still trying to find out how I can reuse the generateRandomPositions() method to generate myself the fish food and hook. I have // the parts I thought I would need to add in order to do this.

import java.util.Random;

public class Main {

   public static void main(String[] args) {

       char [][] tilde = new char [8][32];

       fillTank(tilde, '~');

       int fish [][] = generateRandomPositions(4, 8, 32);

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

           placeFishInTank(tilde, fish[i][0], fish[i][1]);

       }

       renderTank(tilde);

       System.out.println();

       moveAllFish(fish, tilde[0].length, tilde.length);

       fillTank(tilde, '~');

       System.out.println();

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

           placeFishInTank(tilde, fish[i][0], fish[i][1]);

       }

       renderTank(tilde);

   //   int fishHook [][] = generateRandomPositions(1, 8, 32);

   //   int fishFood [][] = generateRandomPositions(6, 8, 32);

   }

   /**

   * Copies the water character into every position in the tank array. The two-dimensional tank

   * array can have dimensions of any size(s).

   *

   * @param tank will contain all water characters after this method is called.

   * @param water is the character copied into the tank.

   */

   public static void fillTank(char[][] tank, char water)

   {

       for (int row = 0; row < tank.length; ++row) {

           for (int column = 0; column < tank[row].length; ++column) {

               tank [row][column] = '~';

           }

          

           System.out.println();

       }

   }

  

   public static void placeFishInTank(char [][] positionInTank, int x, int y)

   {

       positionInTank[x][y] = '>';

       String fishBody = "><))'";

       char[] fishBodyArray = fishBody.toCharArray();

       int j = 4;

       for (int i = y - 1; i >= 0 && j >= 0; i--) {

           positionInTank[x][i] = fishBodyArray[j];

           j--;

       }

  

   }

  

   /**

   * Prints the contents of the tank into the console in row major order, so that the

   * smallest row indexes are on top and the smallest column indexes are on the left. For

   * example:

   * tank[0][0] tank[0][1] tank[0][2] ...

   * tank[1][0] tank[1][1] tank[1][2] ...

   * ...

   * Each row is on its own line, and this method should work for two-dimensional tanks with

   * dimensions of any size.

   *

   * @param tank contains the characters that will be printed to the console.

   */

   public static void renderTank(char[][] tank)

   {

       for (int row = 0; row < tank.length; ++row) {

           for (int column = 0; column < tank[row].length; ++column) {

               System.out.print(tank[row][column]);

           }

       System.out.println();

       }

   }

  

   public static int[][] generateRandomPositions(int number, int width, int height)

   {

       int [][] fish = new int [number][2];

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

           fish [i][0] = Utility.randomInt(width);

           fish [i][1] = Utility.randomInt(height);

      

       }

      

       return fish;

      

   //   int [][] fishHook = new int [number][2];

   //   for (int i = 0; i < number; ++i) {

   //       fish [i][0] = Utility.randomInt(width);

   //       fish [i][1] = Utility.randomInt(height);

      

   //   }

      

   //   return fishHook;

      

   //   int [][] fishFood = new int [number][2];

   //   for (int i = 0; i < number; ++i) {

   //       fish [i][0] = Utility.randomInt(width);

   //       fish [i][1] = Utility.randomInt(height);

      

   //   }

      

   //   return fishFood;

//

   }

  

   public static void moveAllFish(int[][] fish, int width, int height) {

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

           fish[i][1] = (fish[i][1] + 1) % width;

          

           }

       }

      

}

Explanation / Answer

//   int fishHook [][] = generateRandomPositions(1, 8, 32);

//   int fishFood [][] = generateRandomPositions(6, 8, 32);

The wa you are calling is absolutely fine. For fish-hook, it would return 1 hook co-ordinates, for food, it would return 6 co-ordinates - similar to how it returned 4 co-ordinates for the fish.

You can use the following body of generateRandomPositions() in order to make this work. It is almost same to yours, just that I have changed the random number generation method, and named the variable to generic ones (because the same variable can return fish, food and hook co-ordinates; based on how you are calling the method).

public static int[][] generateRandomPositions(int numberOfPositions, int width, int height)

{

       int [][] randomPositions = new int [number][2];

Random random = new Random();

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

           randomPositions[i][0] = random.nextInt(width); // i.e. return an x co-ordinate from 0 to width-1 range

           randomPositions[i][1] = random.nextInt(width); // i.e. return an y co-ordinate from 0 to height-1 range

}

       return randomPositions;

}

Please let me know in comments if you have any further question/clarification. Thank you.

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