(Solved Homework): 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;

Don't use plagiarized sources. Get Your Custom Essay on
(Solved Homework): How to replace elements in a 2D array in Java? Okay so for my program, I am trying to create a fish…
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

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

}
}
}

Homework Ocean
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

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.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents