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

Would love some help on fixing this Java code. So my goal for this code was to c

ID: 3886098 • Letter: W

Question

Would love some help on fixing this Java code.

So my goal for this code was to create a 2D array fish tank that would have 4 fish which would look like: ><))'>, 1 fish hook that would look like: J, and 6 pieces of fish food that would look like: *. I have my code all correct except for the moveAllObjects method at the bottom. The purpose of this method is to move the certain objects within the array. For the fish, it is supposed to move over 1 to the right, the fish hook is supposed to move up one unit, and the fish food is supposed to move over to the left one and down one. I don't need to change anything else in this code except for this method and possibly main. I just want to make sure that each time this code is run, that the tank properly moves all the objects. Thanks in advance for the help! I also wanted to post a scene of what one of the runs would look like:

~~~~~~~~~~~~~~~~~~~~~~~~*~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~J

~~~><(('>~~~~~~~~~~~~~~~*~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~><(('>~*~~~~~~~~~*~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~><(('>~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

       }

   }

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

       }

   }

   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;

   }

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,

I have fixed the issue and highlighted the code changes below

Main.java


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) //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();
}
}

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) //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();
}
}

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

Utility.java

import java.util.Random;

public class Utility {

public static int randomInt(int n) {

Random r = new Random();

return r.nextInt(n);

}

}

Output:

>)'>~~~~~~~~~~~~~~~~~~~~~~~><))'
~~~J~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~><))'>~~~~~~~~~~~~~~
~><))'>~~~~~~~~~~~~~~~~*~~~~~~~~
~~~~~~~~~~~*~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~**~~~~~
~~*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~*~~~~~~~~~~~~~~~~~~

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