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

How to replace elements in a 2D array in Java? Okay so for my program, I am tryi

ID: 3884049 • Letter: H

Question

How to replace elements in a 2D array in Java? Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~ after I generate random positions for my fish. It should also hide characters of the fishes body if it is more to the left like such )'>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ where there is only three parts of the fish, 2 body characters and the head character.The random position coordinate I have generated for each of the four fishes starts on the head of the fish (>) which means I have been able to successfully generate four > in the tank, which looks like ~~~~~~~~~~~~~~~~>~~~~~~~~~~~~~~~ when ran. But my problem is how I can get the rest of the fishes body after the head. I know I am going to be working backwards from the > character and replace the tilde characters with characters from the fishes body. Some steps that would explain it and some suggestions would be nice. I am just getting back into basic coding so a good explanation would be VERY HELPFUL since some "experts" on Chegg don't always see through the student's eyes. Here is some of the code below.

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

   }

   /**

   * 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) { //HERE IS WHERE I AM GETTING MESSED UP

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

       String fishBody = "><))'";

       char[] fishBodyArray = fishBody.toCharArray();

       int j = 4;

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

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

           j--;

       }

  

   }

   //   for (int i = 0; i < positionInTank.length - 1; i++) {

   //       positionInTank[i] = positionInTank[i - 1];

   //       positionInTank[i] = '(';

  

   //       for (int j = 0; i < positionInTank[x].length-1; j++)

   //           if (positionInTank[][] >= 0) {

   //               String.charAt(i, j);

   //           }

   //   }

      

       //if (positionInTank[row].length - 1 != 0) {

       //add on next character '''

   //   }

      

       //if (!positionInTank[row] - 2 != 0) {

      //add on next character ')'

   //   }

      

       //if (!positionInTank[row] - 3 != 0) {

       //add on next character ')'

   //   }

      

       //if (!positionInTank[row] - 4 != 0) {

       //add on next character '<'

   //   }

      

       //if (!positionInTank[row] - 5 != 0) {

       //add on next character '>'

   //   }

      

  

   /**

   * 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) //width and height different

   {

       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;

   }

  

}

I keep getting back

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

   at Main.placeFishInTank(Main.java:41)

   at Main.main(Main.java:10)

Explanation / Answer

String fishBody = "><))'"; you don't need to escape ' char. Though it should not have any effect.

Second issue in your code is in for loop for putting fish body in tank

for (int i = y - 1; i >= 0 && j >= 0; i--) {
positionInTank[x][i] = fishBodyArray[j];
j--;
}

Here you need to make sure that j is also greater than 0 because y can be very far

say y = 31, now figh will be rendered from 26-31 but with the loop you have you will keep on going beyond hat until i is not -1 but there is no char in figh to access for i = 25 as fish[-1] do not exist.

Hope it helped.

Here is full code (Please note you haven't shared Utility class which you are using for getting random int and I hope that class is returning you values which are making sure fish are not overlapping each other)

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

}
/**
* 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] = '~';
}
}
}
  
public static void placeFishInTank(char [][] positionInTank, int x, int y) { //HERE IS WHERE I AM GETTING MESSED UP
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--;
}
  
}
// for (int i = 0; i < positionInTank.length - 1; i++) {
// positionInTank[i] = positionInTank[i - 1];
// positionInTank[i] = '(';
  
// for (int j = 0; i < positionInTank[x].length-1; j++)
// if (positionInTank[][] >= 0) {
// String.charAt(i, j);
// }
// }
  
//if (positionInTank[row].length - 1 != 0) {
//add on next character '''
// }
  
//if (!positionInTank[row] - 2 != 0) {
//add on next character ')'
// }
  
//if (!positionInTank[row] - 3 != 0) {
//add on next character ')'
// }
  
//if (!positionInTank[row] - 4 != 0) {
//add on next character '<'
// }
  
//if (!positionInTank[row] - 5 != 0) {
//add on next character '>'
// }
  
  
/**
* 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) //width and height different
{
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;
}
  
}

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