Question: Write the add and toString methods in the Matrix class. The add method returns a new Matrix object whose values are the sums of the
Write the add and toString methods in the Matrix class. The add method returns a new Matrix object whose values are the sums of the elements of the two matrices. However, if the parameter object does not have the same number of rows and columns as the object executing the method, return null. The add method should not change the state of the parameter or the state of the object executing the method.
The toString method returns a string representation of the matrix. Use \t and .
When you've written the two methods, the main method should print something like the figure below.
2 -1 11
8 4 5
7 6 -21
0 -2 -7
9 5 -10
8 2 -2
*********************
12 13 14 15
4 3
2 1
null
Matrix Class:
public Matrix( int [][] n ) { nums = new int[ n.length ][ n[0].length ]; for ( int r = 0; r < n.length; r++ ) { for ( int c = 0; c < n[r].length; c++ ) { nums[r][c] = n[r][c]; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
