Answered Essay: How to replace elements in a 2D array in Java? Okay so for my program, I am trying to create a fish

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 ‘>’

//   }

 

}

 

Expert 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–;

}
}
}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.