Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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.
I cannot pass the following multiply matrix test:
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
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