How to replace elements in a 2D array in Java? Okay so for my program, I am tryi
ID: 3883837 • 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)
{
positionInTank[x][y] = '>';
//THE REST IS POTENTIAL IDEAS OF HOW I WAS THINKING TO DO THIS.
//FIRST THOUGHTS
// fish [] = {>,<, ), ), ', >};
// for (int i = 0; i < positionInTank.length; i++) {
// for (int j = 0; i < positionInTank[x].length; j++)
// if (positionInTank[][] >= 0) {
// String.charAt(i, j);
// }
// }
//MY SECOND THOUGHT
//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 '>'
// }
}
Explanation / Answer
Assuming that you already have the code of generating the random position of the fish's head using the method generateRandomPositions(4, 8, 32), I am going to help you with filling up the rest of the fish's body in the tank.
Hence, I will only write the following method. Rest of your code remains intact.
The tip here is, you implement the solution using a character array. You go backwards and replace tilde with the Fish's body character. However, at any moment, if you find that you have reached the left end of the tank, you should stop executing the loop at that point. Because, the rest of the fish's body part (at the left side) will not fit since we have already reached the left most end, and can only go left position from there.
public static void placeFishInTank(char [][] positionInTank, int x, int y)
{
positionInTank[x][y] = '>';
String fishBodyExceptHead = "><))'";
// Note the escape character before single-quote above, since we are inside double-quote.
char[] fishBodyExceptHeadArray = fishBodyExceptHead.toCharArray();
int j = 4;
// Size of the fish's body except head is 5. The last index of the array is 4, hence. Using this counter for tracking it.
for(int i=y-1;i>=0 && j>=0;i--){
positionInTank[x][i] = fishBodyExceptHeadArray[j];
j--;
}
}
Sample code which runs using the above block (Just copy paste and run this code)
public class MyClass {
public static void main(String args[]) {
char arr[][] = {{'a','a','a','a','a','a','a','a','a'},{'a','a','a','a','a','a','a','a','a'},{'a','a','a','a','a','a','a','a','a'}};
placeFishInTank(arr, 1, 3); // try passing various co-ordinates here.
renderTank(arr);
}
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 void placeFishInTank(char [][] positionInTank, int x, int y)
{
positionInTank[x][y] = '>';
String fishBodyExceptHead = "><))'";
// Note the escape character before single-quote above, since we are inside double-quote.
char[] fishBodyExceptHeadArray = fishBodyExceptHead.toCharArray();
int j = 4;
// Size of the fish's body except head is 5. The last index of the array is 4, hence. Using this counter for tracking it.
for(int i=y-1;i>=0 && j>=0;i--){
positionInTank[x][i] = fishBodyExceptHeadArray[j];
j--;
}
}
}
Please let me know in comments if you have any further question/clarification. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.