Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4 2D Arrays In this section of the lab we will introduce you to 2D arrays, which will be used in your second project. A
4 2D Arrays In this section of the lab we will introduce you to 2D arrays, which will be used in your second project. A 2D array is just an array of arrays. One way to declare a 2D array and initialize a 2D array can be seen in the following example: int[] [] tdArr = new int [5] [7]; Here, tdArr is a two dimensional array of integers, with 5 rows and 7 columns. Accessing an index of tdArr like tdArr[i] allows us to access the ith row of of tdArr (this value would have type int()). Doing something like tdArr[i][j] accesses the integer at the ith row and jth column of tdArr. One important thing to keep in mind is that not all 2D arrays have equally sized rows; however, for this problem, you may assume this to be the case. Your goal is to write a Matrix class with the following methods and class variables: private int nrows //the number of rows of the matrix private int ncols //the number of columns of the matrix private int [] [] matrix; //2D array which acts as the actual matrix for the class public Matrix(int nrows, int ncols) CSCI 1933 LAB 5 5. HONORS public Matrix(int[] [] arr) public Matrix transpose() The first constructor takes in two arguments representing the number of rows and columns of the matrix. It initializes the matrix based on these values (java will automatically set every value in the 2D array to be zero) The second constructor takes in a 2D array and sets matrix to be equal to this 2D array. It also sets nrows and ncols according to the size of the input array. The transpose() method essentially returns a mirror image of your Matrix. The ith row of your matrix becomes the ith column of the return Matrix. This method should essentially work as follows: create a new Matrix that has nrows equal to the number of columns of your matrix and ncols equal to the number of rows of your matrix. Then, iterate through all the rows and columns of your matrix, setting each entry in the new matrix to be the same entry in your matrix, but with the order of the indices flipped. For example: newMatrix.matrix[j][i] = this.matrix[i][j] Milestone 4: Write a main method where you create a Matrix, print out its contents, and then print out the contents of its transpose by calling the transpose() method (a toString() method in your Matrix class may be helpful when printing the contents of a matrix (remember, your work must be done in an IDE)
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