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

One java test run I need help with... Java bug, would love some help... So basic

ID: 3884361 • Letter: O

Question

One java test run I need help with...

Java bug, would love some help...

So basically the program is trying to create a 2d array that is filled with 4 randomly placed fish, which looks like: ><))">, 1 fish hook(J), and 6 fish food (*) and the rest of the characters are filled with ~ or water characters. And every time I run to test that the method placeObjectInTank() method correctly adds a fish at the specified position, I get the test feedback of Failed to call method Main.placeObjectInTank(String, class [[C, Integer, Integer). Similarly, when I test that same method if it correctly adds a fish when it's tail extends beyond the left edge of the screen, I get back the same thing.

I would love to understand how I can fix this.

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)

           placeObjectInTank("><))'>", tilde, fish[i][0], fish[i][1]);

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

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

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

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

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

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

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

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

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

          

       renderTank(tilde);

   }

   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] = water;

           }

          

           System.out.println();

       }

   }

  

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

   {

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

       int k = row;

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

           positionInTank[column][k] = object.charAt(i);

           k--;  

           if (k < 0) {

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

           }

       }

   }

      

  

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 [][] 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;

   }

  

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

   {

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

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

          

       }

      

      

   }

}

Explanation / Answer

Try this , it's fixed


import java.util.*;
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)
placeObjectInTank("><))'>", tilde, fish[i][0], fish[i][1]);
moveAllObjects(fish, 1, 0, 8, 32);
int fishHook [][] = generateRandomPositions(1, 8, 32);
for (int i = 0; i < fishHook.length; ++i)
placeObjectInTank("J", tilde, fishHook[i][0], fishHook[i][1]);
moveAllObjects(fishHook, 0, 1, 8, 32);
int fishFood [][] = generateRandomPositions(6, 8, 32);
for (int i = 0; i < fishFood.length; ++i)
placeObjectInTank("*", tilde, fishFood[i][0], fishFood[i][1]);
moveAllObjects(fishFood, -1, -1, 8, 32);
  
renderTank(tilde);
}
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] = water;
}
  
System.out.println();
}
}
  
public static void placeObjectInTank(String object, char [][] positionInTank, int column, int row)
{
object.charAt(object.length() - 1);
int k = row;
for (int i = object.length() - 1; i >= 0; --i) {
positionInTank[column][k] = object.charAt(i);
k--;
if (k < 0) {
k = positionInTank[0].length-1; //looks at first row and finds first row
}
}
}
  
  
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 [][] randomPositions = new int [number][2]; //number of fish with coordinates
for (int i = 0; i < number; ++i) {

randomPositions [i][0] =(int) (Math.random() * (width-1)) + 0;
randomPositions [i][1] =(int) (Math.random() * (height-1)) + 0;
  
}
  
return randomPositions;
}
  
public static void moveAllObjects(int[][] positions, int dx, int dy, int width, int height)
{
for (int i = 0; i < positions.length; i++) {
positions[i][1] = (positions[i][1] + 1) % 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