This in-class assignment has 2 points in total. Every bug costs 0.1-0.3 based on its severity. The deadline for this assignment is Tuesday, Oct. 30, NOON, 12 pm. In addition to the 2 points, we have an extra credit of 0.5 to implement a recursive method, printcombinations. Note: Make sure your console output is exactly the same as the output in this instruction. Otherwise, you may get 0.1 point off. Overall goal: in this assignment, 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 (optional, extra credit), please write a recursive program that outputs all combinations of each subarray in order. For example, a b 1 2 d if the 2D array is: Then, the extra credit method needs to output: a 1 d a 2 d b 1 d b 2 d We will split this assignment into three parts. Each part is accomplished by a method. 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, let s create a Java project. Then, create a Main class and a main method (just like what you did in the beginning of assignment13, assignment14, and assignment15 J). Go to the GitHub repository for assignment17, there is an input file, called: 2D.txt You can download the file OR create a file in your project and copy the content of input.txt to your local file. After this setup, you should have a Main class and a main method in your project, and a txt file, input.txt 1
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: Shortcut-Note: Let s 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. 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 picture. 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: 2
In the main method, if we call findmax with the array returned by readfile, and a row index of 0, like the following picture: 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. [Extra credit: 0.5] 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.): After finishing the printcombinations method, this method should be called in the main method, like the following picture. Run the main method, the console should have the following output. 3
4
5
<End> 6