You are to write a class called Matrix that implements a constructor, a method t
ID: 3695704 • Letter: Y
Question
You are to write a class called Matrix that implements a constructor, a method to returns an array with the dimensions of the stored matrix, a method that returns a reference to the internal array (Not an element-by-element copy of the array), a method that performs matrix multiplication, and one that performs scalar addition. For simplicity of this writeup, the code for the constructor is already provided for you. Constructor public Matrix(double [] []) Provided. Do NOT change. You will also not need to add any instance variables to complete the assignment public int [] getDim() return a one-dimensional array where index 0 contains the row dimension of the internal array mat and index 1 contains the column dimension of the internal array mat public double [][] getMatrix() return a reference to the internal array mat. Do not return an element-by-element copy. public Matrix multiply(Matrix rhs) If rhs is of compatible dimension, return a new Matrix object that contains the result of multiplying mat x rhs where multiplication is standard matrix multiplication. If mat is N x M, then rhs must M x Q (where Q is any positive integer). The result matrix in this case has dimension N x Q Error cases: if rhs is null, return a null Matrix object if rhs is not of compatible dimension, return a null Matrix object public Matrix add (double scalar, Matrix rhs) return a new Matrix object that is the result of mat + (scalar)*rhs. rhs is for "right hand side". For matrix addition, this is element-by-element addition. That is, the (i,j) element of the result of adding A + c*B is computed as A[i][j]+ c*B[i][j]. Error cases: if rhs is null, return a reference to the Matrix object of the left-hand side of the addition if rhs is not of compatible dimension, return a reference to the Matrix object of the left-hand side of the addition Notes Why do you think the assignment asks getMatrix() to return a reference to double[][] mat instead of an element-by-element copy? You can use System.out.println() in your code to help you debug. You can print out the variables passed to your methods, for example. Read the specifications carefully, When asked to create a new object, do exactly that. When asked to return a reference to the original, return the proper reference.
Explanation / Answer
class Matrix { double [][] arr; public Matrix(double[][] arr) { this.arr = arr; } public int [] getDim(){ int[] a = new int[2]; a[0] = arr.length; a[1] = arr[0].length; return a; } public double [][] getMatrix(){ return arr; } public Matrix multiply(Matrix rhs){ int[] d1 = getDim(); int[] d2 = rhs.getDim(); int m = d1[0]; int q = d2[1]; int p = d2[0]; double multiply[][] = new double[m][q]; if(m!=q){ return new Matrix(null); }else { double sum = 0; for (int c = 0 ; cRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.