Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello i have JAVA PROGRAMMING assignment could you help me We write a generic ma

ID: 3911921 • Letter: H

Question

Hello i have JAVA PROGRAMMING assignment could you help me
We write a generic matrix multiplication class (named GenericMatrix). The class must be able to multiply any matrix containing the elements of the number type. The call will be made as if

Double[][] M = new Double[3][5];

Double[][] N = new Double[5][7];

Double[][] A = GenericMatrix.multiply(M,N);

Integer[][] K = new Integer[2][3];

Integer[][] L = new Integer[3][5];

Double[][] R = GenericMatrix.multiply(K,L);

Notice that the multiply method always returns Double. (Hint: java.lang.Number).

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// GenericMatrix.java

public class GenericMatrix {

                /**

                * static generic method to multiply two matrices

                *

                * @param <T>

                *            any type extending Number class

                * @param first

                *            - 2D array of type T

                * @param second

                *            - 2D array of type T

                * @return resultant 2D array (Double type), or null if two matrices cannot

                *         be multiplied

                */

                public static <T extends Number> Double[][] multiply(T[][] first,

                                                T[][] second) {

                                /**

                                * finding rows and columns count for both matrices

                                */

                                int row1 = first.length;

                                int col1 = first[0].length;

                                int row2 = second.length;

                                int col2 = second[0].length;

                                if (col1 == row2) {

                                                /**

                                                * Resultant matrix will have 'row1' number of rows and 'col2'

                                                * number of columns. ie. number of rows= number of rows of first

                                                * matrix, number of columns= number of cols of second matrix.

                                                * performing matrix multiplication and storing the result in a 2D

                                                * double array

                                                */

                                                Double m[][] = new Double[row1][col2];

                                                for (int i = 0; i < row1; i++) {

                                                                for (int j = 0; j < col2; j++) {

                                                                                for (int k = 0; k < col1; k++) {

                                                                                                if (m[i][j] == null) {

                                                                                                                m[i][j] = 0.0;

                                                                                                }

                                                                                                m[i][j] += first[i][k].doubleValue()

                                                                                                                                * second[k][j].doubleValue();

                                                                                }

                                                                }

                                                }

                                                return m;

                                }

                                // multiplication is not possible for the given two matrices

                                return null;

                }

}

// Main.java (for testing)

import java.util.Random;

public class Main {

                static Random random = new Random();

                /**

                * a method to generate random number to fill the matrices

                */

                static int randomNumber() {

                                return random.nextInt(10);

                }

                public static void main(String[] args) {

                                /**

                                * testing the multiply method using two Double matrices and two Integer

                                * matrices

                                */

                                Double[][] M = new Double[3][5];

                                System.out.println("Double matrix 1: ");

                                /**

                                * filling the matrix with random values, also displaying it on the go

                                */

                                for (int i = 0; i < M.length; i++) {

                                                for (int j = 0; j < M[0].length; j++) {

                                                                M[i][j] = (double) randomNumber();

                                                                System.out.print(M[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                Double[][] N = new Double[5][7];

                                System.out.println(" Double matrix 2: ");

                                for (int i = 0; i < N.length; i++) {

                                                for (int j = 0; j < N[0].length; j++) {

                                                                N[i][j] = (double) randomNumber();

                                                                System.out.print(N[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                /**

                                * performing multiplication and displaying the resultant matrix

                                */

                                Double[][] A = GenericMatrix.multiply(M, N);

                                System.out.println(" Double matrix 1 * Double matrix 2: ");

                                for (int i = 0; i < A.length; i++) {

                                                for (int j = 0; j < A[0].length; j++) {

                                                                System.out.print(A[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                /**

                                * Now doing same for two Integer matrices

                                */

                                Integer[][] K = new Integer[2][3];

                                System.out.println(" Integer matrix 1: ");

                                for (int i = 0; i < K.length; i++) {

                                                for (int j = 0; j < K[0].length; j++) {

                                                                K[i][j] = randomNumber();

                                                                System.out.print(K[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                Integer[][] L = new Integer[3][5];

                                System.out.println(" Integer matrix 2: ");

                                for (int i = 0; i < L.length; i++) {

                                                for (int j = 0; j < L[0].length; j++) {

                                                                L[i][j] = randomNumber();

                                                                System.out.print(L[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                Double[][] R = GenericMatrix.multiply(K, L);

                                System.out.println(" Integer matrix 1 * Integer matrix 2: ");

                                for (int i = 0; i < R.length; i++) {

                                                for (int j = 0; j < R[0].length; j++) {

                                                                System.out.print(R[i][j] + " ");

                                                }

                                                System.out.println();

                                }

                                /**

                                * Always make sure that you multiply compatible matrices only,

                                * otherwise the multiply method will return null

                                */

                }

}

/*OUTPUT*/

Double matrix 1:

9.0 1.0 3.0 0.0 4.0

9.0 4.0 8.0 4.0 4.0

4.0 2.0 0.0 7.0 7.0

Double matrix 2:

8.0 8.0 5.0 2.0 5.0 1.0 1.0

4.0 9.0 2.0 4.0 3.0 2.0 8.0

9.0 6.0 1.0 1.0 3.0 9.0 6.0

3.0 3.0 5.0 8.0 5.0 9.0 9.0

9.0 3.0 9.0 9.0 8.0 2.0 1.0

Double matrix 1 * Double matrix 2:

139.0 111.0 86.0 61.0 89.0 46.0 39.0

208.0 180.0 117.0 110.0 133.0 133.0 129.0

124.0 92.0 122.0 135.0 117.0 85.0 90.0

Integer matrix 1:

9 4 0

2 8 0

Integer matrix 2:

7 3 7 1 5

2 9 2 8 3

0 6 5 6 4

Integer matrix 1 * Integer matrix 2:

71.0 63.0 71.0 41.0 57.0

30.0 78.0 30.0 66.0 34.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote