Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question manipulates one-dimensional and two-dimensional arrays. In part (a) you will write a method to reverse elements of a one-dimensional array. In parts (b)
This question manipulates one-dimensional and two-dimensional arrays. In part (a) you will write a method to reverse elements of a one-dimensional array. In parts (b) and (c) you will write methods to reverse elements of a twodimensional array. (a) Consider the following incomplete ArrayUtil class, which contains a static reverseArray method. public class ArrayUtil \{ /** Reverses elements of array arr. * Precondition: arr. length >0. * Postcondition: The elements of arr have been reversed. * @param arr the array to manipulate */ public static void reverseArray(int [] arr) {/ to be implemented in part (a) /} //0ther methods are not shown. \} Write the ArrayUtil method reverseArray. For example, if arr is the array {2,7,5,1,0}, the call to reverseArray changes arr to be {0,1,5,7,2}. Complete method reverseArray below. /** Reverses elements of array arr. * Precondition: arr.length >0. * Postcondition: The elements of arr have been reversed. * Oparam arr the array to manipulate */ public static void reverseArray(int [] arr) Consider the following incomplete Matrix class, which represents a twolimensional matrix of integers. Assume that the matrix contains at least e integer. lic class Matrix private int [] [] mat; /** Constructs a matrix of integers. */ public Matrix (int [] [] m) { mat =m;} /** Reverses the elements in each row of mat. * Postcondition: The elements in each row have been reversed. */ public void reverseAllRows() \{/* to be implemented in part (b) */ } /** Reverses the elements of mat. * Postcondition: * - The final elements of mat, when read in row-major order, * are the same as the original elements of mat when read * from the bottom corner, right to left, going upward. * - mat [0] [0] contains what was originally the last element. * - mat [mat. length-1] [mat [0].length-1] contains what was * originally the first element. */ public void reverseMatrix() \{/* to be implemented in part (c)*/ } //0ther instance variables, constructors and methods are not shown. (c) Write the Matrix method reverseMatrix. This method reverses the elements of a matrix such that the final elements of the matrix, when read in row-major order, are the same as the original elements when read from the bottom corner, right to left, going upward. Again let mat 1 be a reference to a Matrix object. The the call mat1.reverseMatrix() will change the matrix as shown below. In writing reverseMatrix, you must call the reverseAllRows method in part (b). Assume that reverseAllRows works correctly regardless of what you wrote in part (b). Complete method reverseMatrix below. /** Reverses the elements of mat. * Postcondition: * - The final elements of mat, when read in row-major order, * are the same as the original elements of mat when read * from the bottom corner, right to left, going upward. * - mat[0][0] contains what was originally the last element. * - mat [mat.length-1] [mat [0]. length-1] contains what was * originally the first element. */ public void reverseMatrix()
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