Question: Overall goal: with Java, you are asked to read a file, which contains a 2D array. Then, write a recursive method to find the max

Overall goal: with Java, you are asked to read a file, which contains a 2D array. Then, write a recursive method to find the max value of the array. Then, please write a recursive program that outputs all combinations of each subarray in order. For example:

a b

1 2

d

then the method needs to output:

a 1 d

a 2 d

b 1 d

b 2 d

Here are the methods you should create:

method 1. readfile: this method is to read an input file and initialize the 2D array

method 2. findmax: this method is to find the max int number from a 2D array

method 3. printCombinations: this method prints all the combinations of each subarray in order.

Before these steps, first, lets create a Java project. Then, create a Main class and a main method. You also need to make an 2D.txt file with the contents as:

A B C

1 2 3

a b c d e f g h i j k 11

After this setup, you should have a Main class and a main method in your project, and a txt file, 2D.txt.

Step 1. Read the input file We can start from the first method, readfile. The input parameter is String filename, which specifies a file. This method should return a 2D array. In this assignment, we are going to use FileInputStream and Scanner to read the file. These classes throw FileNotFoundExceptionand. we are going to use throws clause in the method signature. So, the method signature should look like:

public static String [] [] readfile(String filename) throws FileNotFoundException

Shortcut-Note: Lets assume the input file has only three lines.

Hint: we can get a line by the nextline() method of class Scanner. To split the string by space, we can use split method. For example, if we have a String variable, str. Suppose str is a b c. Then str.split( ) will return a String array that has {a, b, c}.

After finishing this method, you can call this method in the main method with 2D.txt. The method call should look like the following:

String [] [] matrix = readfile("2D.txt");

Please use System.out.println and System.out.print prints the array (returned by the method call). You need to use try-catch block to catch the FileNotFoundExceptionand that readfile throws. Inside the catch block, System.out.println the message of the exception and call System.exit(1) to exit the program once we catch an IOException from readfile. Run the main method, and the console output should look like the following:

A B C

1 2 3

a b c d e f g h i j k 11

Change the input of the method call to input2.txt, and run the main method. The console output should look like the following picture. In the main method, change the input2.txt back to 2D.txt.

Step 2. findmax The next step is to create a recursive method, findmax. The input for this method is a 2D array, and a row index. The return type is a String. So, the method signature **should** look like this:

public static int findmax(String [] [] array, int row)

In the main method, if we call findmax with the array returned by readfile, and a row index of 0, like the following:

int maxval = findmax(matrix, 0);

The findmax should return an int value of 11. System.out.println the result, and run the main method, then the console should have the following output.

A B C

1 2 3

a b c d e f g h i j k 11

11

Step 3. printCombinations The next step is to create a recursive method, printCombinations. The input for this method is a temporary string, an int index, a 2D array. The return type is a String. So, the method signature looks like this (this is the method signature for the solution, if you can create a recursive method that has the same output, the input parameter list can be different.):

public static String printCombinations(String tmp, int index, String [] [] array)

After finishing the printCombinations method, this method should be called in the main method. Run the main method, the console should have the following output:

A B C

1 2 3

a b c d e f g h i j k 11

11

A1a

A1b

A1c

A1d

A1e

A1f

A1g

A1h

A1i

A1j

A1k

A111

A2a

A2b

A2c

A2d

A2e

A2f

A2g

A2h

A2i

A2j

A2k

A211

and so on with B and C

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!