Matrix Multiplication Write a method called multiply in the Matrix class that takes an instance of Matrix as a parameter and returns the result of matrix multiplication. Complete the definition of the class Matrix and method definition multiply shown here and in the starter code. public class Matrix { public Matrix multiply(Matrix other) { // fill in code here } } For purposes of this exercise, you should assume (without checking) that the matrices to be multiplied have the correct shapes, ie, that this.numCols() is equivalent to other.numRows(). To multiply matrices, the number of columns in the first matrix must equal the number of rows in the second matrix. In general, if we multiply an m x n matrix A (ie, having m rows and n columns) by a n x p matrix B (ie, having n rows and p columns), the product will have size m x p (ie, m rows and p columns). Here is a simple illustration of the computation for two 2 x 2 matrices: Examples Example 1 If A and B were both instances of the Matrix class with the following data, respectively, 0.0 1.0 2.0 2.0 1.0 2.0 2.0 2.0 2.0 2.0 0.0 2.0 the product a.multiply(b) would be the Matrix containing the following data: 2.0 6.0 6.0 10.0 Example 2 If A and B were both instances of the Matrix class with the following data, respectively, 1.0 3.0 1.0 2.0 3.0 1.0 0.0 3.0 1.0 0.0 3.0 1.0 1.0 1.0 2.0 3.0 2.0 3.0 0.0 1.0 the product a.multiply(b) would be the Matrix containing the following data: 9.0 15.0 5.0 9.0 7.0 11.0 Matrix.java code is attached import java.util.Random; public class Matrix { // Largest random value in Matrix will be MAX_VALUE-1 privatestaticfinalintMAX_VALUE = 3; privatedouble[][] matrix; // Constructor initializes an empty matrix publicMatrix(intnumRows, intnumCols) { matrix = newdouble[numRows][numCols]; } // Fill Matrix with random small integers for testing publicvoidfillMatrix() { Randomrand = newRandom(); for (intr = 0; r < numRows(); r++) { for (intc = 0; c < numCols(); c++) { matrix[r][c] = rand.nextInt(MAX_VALUE); } } } // Return number of rows in Matrix object publicintnumRows() { returnmatrix.length; } // Return number of columns in Matrix object publicintnumCols() { return (matrix.length == 0) ?0:matrix[0].length; } // Return String representation of Matrix object publicStringtoString() { Stringm = ""; for (intr = 0; r < numRows(); r++) { for (intc = 0; c < numCols(); c++) { m += matrix[r][c] + " "; } m += "\n"; } returnm; } // Get value at row r and column c publicdoublegetValue(intr, intc) { returnmatrix[r][c]; } // Set value at row r and column c publicvoidsetValue(intr, intc, doublevalue) { matrix[r][c] = value; } // Return product of this and other publicMatrixmultiply(Matrixother) { // TODO: complete method definition, replace this placeholder code Matrixproduct = newMatrix(0,0); returnproduct; } // main method to test Matrix class publicstaticvoidmain(String[] args) { Matrixa = newMatrix(2, 3); Matrixb = newMatrix(3, 2); a.fillMatrix(); b.fillMatrix(); Matrixproduct = a.multiply(b); System.out.println(a); System.out.println(b); System.out.println(product); } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Matrix Multiplication
Write a method called multiply in the Matrix class that takes an instance of Matrix as a parameter and returns the result of matrix multiplication. Complete the definition of the class Matrix and method definition multiply shown here and in the starter code.
public class Matrix { public Matrix multiply(Matrix other) { // fill in code here } }
For purposes of this exercise, you should assume (without checking) that the matrices to be multiplied have the correct shapes, ie, that this.numCols() is equivalent to other.numRows().
To multiply matrices, the number of columns in the first matrix must equal the number of rows in the second matrix. In general, if we multiply an m x n matrix A (ie, having m rows and n columns) by a n x p matrix B (ie, having n rows and p columns), the product will have size m x p (ie, m rows and p columns).
Here is a simple illustration of the computation for two 2 x 2 matrices:
Examples
Example 1
If A and B were both instances of the Matrix class with the following data, respectively,
0.0 1.0 2.0 2.0 1.0 2.0 2.0 2.0 2.0 2.0 0.0 2.0
the product a.multiply(b) would be the Matrix containing the following data:
2.0 6.0 6.0 10.0
Example 2
If A and B were both instances of the Matrix class with the following data, respectively,
1.0 3.0 1.0 2.0 3.0 1.0 0.0 3.0 1.0 0.0 3.0 1.0 1.0 1.0 2.0 3.0 2.0 3.0 0.0 1.0
the product a.multiply(b) would be the Matrix containing the following data:
9.0 15.0 5.0 9.0 7.0 11.0
Matrix.java code is attached

Step by step
Solved in 4 steps with 4 images









