Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please read carefully! Copy the program Flatten.java and implement the TBI (To Be Implemented) methods flatten(int[][]) and flatten(String[][]) . You will need to study the
Please read carefully!
Copy the program Flatten.java and implement the TBI (To Be Implemented) methods flatten(int[][]) andflatten(String[][]).
You will need to study the code in the print(int[]) and print(String[][]) methods to figure out how theflatten() methods "flatten" their 2-dimensional array parameter into their 1-dimensional array return value.
Your program cannot contain any import statements.
The output of your program must match the following. {output file}
{ } { 1, } { 1, 2, } { 1, 2, 3, } { 1, 2, 3, 4, } { 1, 2, 3, 4, 5, } { 1, 2, 3, 4, 5, 6, } { "if", "else", "switch", } { "while", "do while", "for", } { "break", "continue", } { "*", "/", "%", "+", "-", } { ">", ">=", "<=", "<", "==", "!=", } { "&&", "||", "!", } { "<<", ">>", ">>>", "&", "|", "^", "~", }
The Code: /* * This Java application "flattens" a 2d-array * into a 1d-array. The details of how this is * done is left to the reader. * * * */
public class Flatten { public static void main(String[] argv) { int[][] x = { { }, { 1, }, { 1, 2, }, { 1, 2, 3, }, { 1, 2, 3, 4, }, { 1, 2, 3, 4, 5, }, { 1, 2, 3, 4, 5, 6, }, }; print(flatten(x)); String[][] y = { { "if", "else", "switch", }, { "while", "do while", "for", }, { "break", "continue", }, { "*", "/", "%", "+", "-", }, { ">", ">=", "<=", "<", "==", "!=", }, { "&&", "||", "!", }, { "<<", ">>", ">>>", "&", "|", "^", "~", }, }; print(flatten(y)); } /** * Prints a "flattened" 1d-array of ints. * * @param a the flattened array to print */ static void print(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print("{ "); for (int j = 0, k = a[i]; j < k; j++) System.out.print(a[++i] + ", "); System.out.println("}"); } } /** * Prints a "flattened" 1d-array of String objects. * * @param a the flattened array to print */ static void print(String[] a) { for (int i = 0; i < a.length; i++) { System.out.print("{ "); for (int j = 0, k = Integer.parseInt(a[i]); j < k; j++) System.out.print("\"" + a[++i] + "\", "); System.out.println("}"); } } /** * TBI (To Be Implemented)... * * This method "flattens" a 2-dimensional int-array * into a 1-dimensional int-array. The algorithm * for flattening the 2d array is defined by code * in the print(int[]) method. * * @param x 2-dimensional int-array to "flatten" * @return the "flattened" 1-dimensional int-array */ static int[] flatten(int[][] x) { } /** * TBI (To Be Implemented)... * * This method "flattens" a 2-dimensional String-array * into a 1-dimensional String-array. The algorithm * for flattening the 2d array is defined by code * in the print(String[]) method. * * @param x 2-dimensional String-array to "flatten" * @return the "flattened" 1-dimensional String-array */ static String[] flatten(String[][] x) { } }
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