Question
You will write a Java class called Matrix that has the following variables, constructors, and methods: 1. Declare two-dimensional matrix int [][] matrix, numRows, numCols
You will write a Java class called Matrix that has the following variables, constructors, and methods:
1. Declare two-dimensional matrix int [][] matrix, numRows, numCols to denote the number of rows and columns.
2. Non-default constructor public Matrix (int n, int m) that initializes matrix with number of rows equal to n and number of columns equal to m.
3. Method public Matrix add(Matrix mat) that adds the corresponding integer entries on this.matrix with values in mat and returns the resulting Matrix object. This operation succeeds only when the number of rows and columns of both matrices are equal, so make sure to check that before adding. If not, handle it via an appropriate exception. The result matrix is of the same dimensions as the ones added. (see Part A of attached pdf). What is the time-complexity of your method?
4. Method public Matrix transpose() OR public static Matrix transpose(Matrix mat) that returns the transpose of matrix mat. The transpose of a matrix M is a matrix M with number of rows and columns and the values interchanged as shown in the example in the pdf (Part E). What is the time-complexity of your method?
5. Method public Matrix subtract(Matrix mat) that subtracts the corresponding integer entries from this.matrix with values in mat and returns the resulting Matrix object. This operation succeeds only when the number of rows and columns of both matrices are equal, so make sure to check that before adding. If not, handle it via an appropriate exception. The result matrix is of the same dimensions as the ones in the subtraction operation. (see Part B of pdf). There is an easier way to do this, so think about it. What is the time-complexity of your method?
6. Method public Matrix scalarMult(int const) that returns a new Matrix object where each entry of this.matrix is multiplied by the value const. Again, see the example in the pdf to understand how it is done (Part C). What is the time-complexity of your method? What is the timecomplexity of your method?
7. Method public void printMatrix() that prints out the contents of this.matrix to a file matrix_output.txt . This file has on the first line Matrix is of dimension m rows by n columns, followed by the contents of the matrix starting from row 0, and then row 1, etc., each row of values on a separate line.
8. Method public static Matrix genMatrix(int m, int n) that creates a new matrix of size m x n whose [i][j]th entry is set equal to i+j. This can be used for your own testing purposes.
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