Question: What's wrong with my java code to solve matrix multiplications? Hi, I'm very new to java. I know this question is very basic and has

What's wrong with my java code to solve matrix multiplications?

Hi, I'm very new to java. I know this question is very basic and has been asked here many times, but I still do not know what's wrong with my code after looking at many similar problems and answers. I hope you can spare some time to point out where my code get wrong.

Below is my code to solve this problem.

What's wrong with my java code to solve matrix multiplications? Hi, I'm

I cannot pass the following multiply matrix test:

very new to java. I know this question is very basic and

Can you help me modify my code?

I've attached the code below for you to copy and paste.

public class Matrix { private final int[][] storage; public int rows() {return storage.length;} public int columns() {return storage[0].length;} public Matrix(int[][] matrixArray) { // TODO: please implement the constructor of a matrix. // The constructor will initialize the matrix from an array. Please refer to // the test cases to understand how you can implement the method. //  test = Arrays.asList(matrixArray); if (test.contains(null)){ throw new IllegalArgumentException("Raw matrix contains null row"); } this.storage = new int[matrixArray.length][matrixArray[0].length]; for(int i = 0; i  } public static Matrix multiply(Matrix left, Matrix right) { // TODO: // please implement the method to pass the tests. // Please refer to the test cases to understand how you can implement the method. //  

void should_multiply_matrix() { int[][] left = {{3, 2, 3}, {5, 9, 8}}; int[][] right = {{4, 7}, {9, 3}, {8, 1}}; final Matrix leftMatrix = new Matrix(left); final Matrix rightMatrix = new Matrix(right); // 3 2 3 4 7 // 5 9 8 9 3 // 8 1 final int[][] expected = { {12 + 18 + 24, 21 + 6 + 3}, {20 + 81 + 64, 35 + 27 + 8} }; Matrix result = Matrix.multiply(leftMatrix, rightMatrix); assertEquals(new Matrix(expected), result); }
public class Matrix { private final int[][] storage; public int rows() {return storage.length;} public int columns() {return storage[0].length;} . public Matrix(int[][] matrixArray) {...} public static Matrix multiply(Matrix left, Matrix right) { // TODO: // please implement the method to pass the tests. // Please refer to the test cases to understand how you can implement the method. //

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!