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

Bug, need help I keep getting thrown Exception in thread \"main\" java.lang.Arra

ID: 3885270 • Letter: B

Question

Bug, need help

I keep getting thrown

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

   at Main.placeObjectInTank(Main.java:81)

   at Main.main(Main.java:45)

public class Main {

   public static void main(String[] args)

   {

       //data = new Data();

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

       fillTank(tilde, '~');

       int fish [][] = generateRandomPositions(4, 8, 32); //four fish with 8 rows and 32 columns

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

           placeObjectInTank("><))'>", tilde, fish[i][1], fish[i][0]); //place object in tank

           moveAllObjects(fish, 1, 0, 8, 32); //moveAllObjects and pass it the following

       }

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

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

           placeObjectInTank("J", tilde, fishHook[i][1], fishHook[i][0]);

           moveAllObjects(fishHook, 0, 1, 8, 32);

       }

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

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

           placeObjectInTank("*", tilde, fishFood[i][1], fishFood[i][0]);

           moveAllObjects(fishFood, 1, 1, 8, 32);

       }

       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) //fill the tank up

   {

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

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

               tank [row][column] = water;

           }  

           System.out.println();

       }

   }

  

   /**

   * Places the actual location within the tank. It does this by going through

   * column and row and replacing the tilde with the character we want.

   *

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

   * @param column is x value of the tank

   * @param row is the y value of the tank

   */

   public static void placeObjectInTank(String object, char [][] tank, int column, int row)

   {

       object.charAt(object.length() - 1);

       int k = column;

       for (int i = object.length() - 1; i >= 0; --i) {

           tank[row][k] = object.charAt(i);

           k--;  

           if (k < 0) {

               k = tank[0].length-1; //looks at first row and finds first row  

           }

       }

   }

  

   /**

   * 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) //render our 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();

       }

   }

  

   /**

   * Generates random positions within the tank when called. It will go through the loop

   * to give us values for both coordinates.

   *

   * @param number takes in how many we want to generate

   * @param width is the limit again for the x value

   * @param takes is the limit again for the y value

   * @return returns randomly generated position within the tank to main

   */

   public static int[][] generateRandomPositions(int number, int width, int height) //give us our positions

   {

       int [][] randomPositions = new int [number][2]; //number of fish with coordinates

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

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

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

       }

       return randomPositions;

   }

  

   /**

   * Moves all of the objects within our tank, with fish moving to the right one

   * fish food moving to the left and down one, and the fish hook moving up one.

   * However, this is the method I left on and new I would have to change the array

   * to 'positions' to find the new positions for our objects once this method is called.

   *

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

   * @param dx how much it should move from within the column

   * @param dy how much it should move from within the row

   * @param width the column limit of our tank

   * @param height the row length limit of our tank

   */

   public static void moveAllObjects(int[][] positions, int dx, int dy, int width, int height)

   {

       if (dx == 0 && dy == 1) {

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

               positions[i][0] = positions[i][0] - dy;

           }

       }

      

       if (dx == 1 && dy == 1) {

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

               positions[i][1] = positions[i][1] - dx;

               positions[i][0] = positions[i][0] + dy;

           }

       }

      

       if (dx == 1 && dy == 0) {

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

               positions[i][1] = (positions[i][1] + dx) % width;

           }

       }

      

   }

}

Explanation / Answer

Hi,

Please try with the updated class below:

import com.sun.corba.se.impl.util.Utility;

public class Main {

public static void main(String[] args)

{

//data = new Data();

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

fillTank(tilde, '~');

int fish [][] = generateRandomPositions(4, 8, 32); //four fish with 8 rows and 32 columns

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

placeObjectInTank("><))'>", tilde, fish[i][1], fish[i][0]); //place object in tank

moveAllObjects(fish, 1, 0, 8, 32); //moveAllObjects and pass it the following

}

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

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

placeObjectInTank("J", tilde, fishHook[i][1], fishHook[i][0]);

moveAllObjects(fishHook, 0, 1, 8, 32);

}

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

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

placeObjectInTank("*", tilde, fishFood[i][1], fishFood[i][0]);

moveAllObjects(fishFood, 1, 1, 8, 32);

}

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) //fill the tank up

{

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

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

tank [row][column] = water;

}

System.out.println();

}

}

  

/**

* Places the actual location within the tank. It does this by going through

* column and row and replacing the tilde with the character we want.

*

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

* @param column is x value of the tank

* @param row is the y value of the tank

*/

public static void placeObjectInTank(String object, char [][] tank, int column, int row)

{

object.charAt(object.length() - 1);

int k = column;

for (int i = object.length() - 1; i >= 0; --i) {

tank[row][k] = object.charAt(i);

k--;

if (k < 0) {

k = tank[0].length-1; //looks at first row and finds first row

}

}

}

  

/**

* 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) //render our tank

{

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

if(tank.length > row){

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

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

}

}

System.out.println();

}

}

  

/**

* Generates random positions within the tank when called. It will go through the loop

* to give us values for both coordinates.

*

* @param number takes in how many we want to generate

* @param width is the limit again for the x value

* @param takes is the limit again for the y value

* @return returns randomly generated position within the tank to main

*/

public static int[][] generateRandomPositions(int number, int width, int height) //give us our positions

{

int [][] randomPositions = new int [number][2]; //number of fish with coordinates

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

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

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

}

return randomPositions;

}

  

/**

* Moves all of the objects within our tank, with fish moving to the right one

* fish food moving to the left and down one, and the fish hook moving up one.

* However, this is the method I left on and new I would have to change the array

* to 'positions' to find the new positions for our objects once this method is called.

*

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

* @param dx how much it should move from within the column

* @param dy how much it should move from within the row

* @param width the column limit of our tank

* @param height the row length limit of our tank

*/

public static void moveAllObjects(int[][] positions, int dx, int dy, int width, int height)

{

if (dx == 0 && dy == 1) {

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

positions[i][0] = positions[i][0] - dy;

}

}

  

if (dx == 1 && dy == 1) {

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

positions[i][1] = positions[i][1] - dx;

positions[i][0] = positions[i][0] + dy;

}

}

  

if (dx == 1 && dy == 0) {

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

positions[i][1] = (positions[i][1] + dx) % width;

}

}

  

}

}

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