Question
Java: determine the number of organisms in an image (2d array), and label each organism. Signature: public static int howManyOrganisms(char[][] image) Description : Given a
Java: determine the number of organisms in an image (2d array), and label each organism.
Signature: public static int howManyOrganisms(char[][] image)
Description: Given a 2d array of characters, where an asterisk (*) marks a non-empty cell. An organism is defined as all cells connected directly to other cells in the up/down/left/right (not diagonal) directions.
Details:
The method signature above is not a recursive method. Instead, it iterates through the image and as soon as it encounters a new organism, it calls a recursive method (that you define) that labels the newly found organism.
Look at the provided driver code to see how the organism is stored and compare that with the output of the labeled organisms below. In the original image, the organisms are identified by asterisks (*), but the resulting image has each organism labeled with a different character in the alphabet.
The image is NOT guaranteed to be rectangular (i.e. it may be ragged).
char image[][] = { {'*','*',' ',' ',' ',' ',' ',' ','*',' '}, {' ','*',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ',' ',' ',' ','*','*',' ',' '}, {' ','*',' ',' ','*','*','*',' ',' ',' '}, {' ','*','*',' ','*',' ','*',' ','*',' '}, {' ','*','*',' ','*','*','*','*','*','*'}, {' ',' ',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ',' ',' ',' ',' ',' ','*',' '}, {' ',' ',' ','*','*','*',' ',' ','*',' '}, {' ',' ',' ',' ',' ','*',' ',' ','*',' '} };
Driver code:
int howMany = howManyOrganisms(image); System.out.println(); System.out.println("--- Labeled Organism Image ---"); for (char[] line : image) { for (char item : line) { System.out.print(item); } System.out.println(); } System.out.printf("There are %d organisms in the image. ", howMany);
Example Output:
--- Labeled Organism Image --- aa b a b cc d ccc dd c c c dd cccccc c c eee c e c There are 5 organisms in the image.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started