Matrix Class Implementation A matrix is a two-dimensional structure of numerical
ID: 3877040 • Letter: M
Question
Matrix Class Implementation
A matrix is a two-dimensional structure of numerical values ai, j , where i is the (zero-origin) row index and j is the (zero-origin) column index. If r is the number of rows and c is the number of columns in a matrix A, then it has the form:
Figure 1: A matrix of r rows and c columns.
You are to design and implement a Java Class named Matrix, here is what you need to do.
Include 3 attributes, rows, columns and a 2D array for the actual matrix.
A constructor that takes the number of rows and columns of the 2D array as parameters from client and instantiates it.
A method called copyMatrix that returns a copy of a Matrix object. (Do not clone!!)
A method called equals that returns true if two Matrix objects are the same, otherwise returns false. Two Matrix objects are the same if they have the same number of rows and columns, and the value Ai, j of Matrix A’s 2D array is equal to value Bi, j of Matrix B’s 2D array.
A toString method that returns a properly formatted String that contains at- tributes of a Matrix object. A toString method in a Java class always returns a String that contains the current state of the Object. The current state is represented by Object’s attributes.
A method called scalarMultiply to multiply a Matrix by scalar in which every value Ai j in Matrix A’s 2D array is multiplied by a scalar value k. See details in Figure 2. You only need to pass value k as a parameter to this method.
A method to multiply called multiply a Matrix by another Matrix. If the number of columns in Matrix A’s 2D array is not equal to the number of rows in Matrix B’s 2D array, the method should throw an Exception, otherwise it should perform the multiplication between the two 2D arrays in the Matrix objects and return a resulting Matrix object. See details in Figure 3, Figure 4 and Figure 5.
Figure 2: A matrix of 2 rows and 3 columns is multiplied by the scalar value 3.
Figure 3: A matrix of 2 rows and 3 columns is multiplied by a matrix of 3 rows and 1 column.
Figure 4: A matrix of 4 rows and 2 columns cannot be multiplied by a matrix of 3 rows and 5 columns.
Figure 5: A matrix of 3 rows and 3 columns can be multiplied by a matrix of 3 rows and 2 columns
Write a client code (a Java class with main method) to test all methods in Matrix class. You must test the following
Instantiate several Matrix objects of different rows/columns.
Make a Matrix copy from an existing Matrix instance
Test the equals method with Matrix objects that are actually equal, and with the Matrix objects that are not equal. Must output properly labeled state- ments.
Display several of the Matrix objects using toString method.
Display a Matrix object that is before and after multiplying by a valid integer scalar value using scalarMultiply
Display a Matrix object that is before and after multiplying by 0 as the scalar value.
Test the method multiply wirh Matrix by Matrix multiplication with at least three sets of different Matrix objects that test different outcomes of the method, including the Exception. Know your answers BEFORE you test!
Turn in properly documented Java programs (actual .java source programs client and class), outputs. Also turn in the java programs and output in a docx document.
Be sure to include file comment block and method comment blocks.
Java please
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//Matrix.java class
import java.util.Scanner;
public class Matrix {
private int rows;
private int cols;
private int[][] matrix;
/**
* Parameterized constructor to initialize variables
*
* @param rows
* - number of rows
* @param cols
* - number of columns
*/
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
matrix = new int[rows][cols];
}
/**
* This is a method to read values to the matrix (Not specified in the
* question, but still its a useful method, there must be some ways to fill
* the matrix
*/
public void readInput() {
Scanner scanner = new Scanner(System.in);
try {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.println("Enter value for [" + i + "," + j
+ "]: ");
int n = Integer.parseInt(scanner.nextLine());
matrix[i][j] = n;
}
}
} catch (Exception e) {
System.out.println("Invalid input");
}
}
/**
* useful getters and setters
*/
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public int[][] getMatrix() {
return matrix;
}
public void setMatrix(int[][] matrix) {
this.matrix = matrix;
}
/**
* mathod to create a copy of this matrix and return it
*/
public Matrix copyMatrix() {
Matrix m = new Matrix(rows, cols);
m.setMatrix(matrix);
return m;
}
/**
* method to compare two Matrix objects to check if they are equal or not
*/
public boolean equals(Matrix m) {
if (rows == m.rows && cols == m.cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != m.matrix[i][j]) {
return false;
}
}
}
return true;
}
return false;
}
/**
* method that returns a properly formatted String that contains attributes
* of a Matrix object
*/
public String toString() {
String str = "";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
str += matrix[i][j] + " ";
}
str += " ";
}
str += "Rows:" + rows + ", Columns: " + cols;
return str;
}
/**
* method to multiply a Matrix by scalar in which every value Ai j in Matrix
* A’s 2D array is multiplied by a scalar value k
*/
public void scalarMultiply(int k) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = matrix[i][j] * k;
}
}
}
/**
* method to multiply two matrices and return the resultant matrix
*
* @throws Exception
* if two matrices cannot be multiplied
*/
public Matrix multiply(Matrix B) throws Exception {
if (cols != B.rows) {
throw new Exception("these two matrices cannot be multiplied");
}
/**
* Resultant matrix will have 'rows' number of rows and 'B.cols' number
* of columns. ie. number of rows= number of rows of first matrix,
* number of columns= number of cols of second matrix
*/
Matrix m = new Matrix(rows, B.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < B.cols; j++) {
for (int k = 0; k < cols; k++) {
m.matrix[i][j] += matrix[i][k] * B.matrix[k][j];
}
}
}
return m;
}
}
//Client.java
public class Client {
public static void main(String[] args) {
int[][] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[][] array2 = { { 2 }, { 5 }, { 7 } };
/**
* Creating a matrix
*/
Matrix m1 = new Matrix(3, 3);
m1.setMatrix(array);
/**
* Creating another matrix using second 2d array
*/
Matrix m2 = new Matrix(3, 1);
m2.setMatrix(array2);
/**
* displaying first matrix
*/
System.out.println("Matrix m1: "+m1.toString());
/**
* Taking a copy of first matrix
*/
Matrix m1copy=m1.copyMatrix();
/**
* displaying copied matrix
*/
System.out.println("Matrix m1copy: "+m1copy);
/**
* checking if first matrix and copied matrix are equal
*/
System.out.println("is m1==m1copy :"+m1.equals(m1copy));
/**
* displaying second matrix
*/
System.out.println("Matrix m2: "+m2);
/**
* performing scalar multiplication on first matrix and displays it
*/
m1.scalarMultiply(2);
System.out.println("Matrix m1 * 2 (scalar multiplied by 2) : "+m1);
/**
* Multiplying matrix 1 with matrix 2
*/
try {
Matrix temp=m1.multiply(m2);
System.out.println("Matrix m1*m2: "+temp);
} catch (Exception e) {
System.err.println(e.getMessage());
}
/**
* Creating one more matrix of 2x1 size
*/
Matrix m3=new Matrix(2, 1);
m3.setMatrix(new int[][]{{3},{5}});
System.out.println("Matrix m3: "+m3);
System.out.println("Matrix m1*m3:");
try {
m1.multiply(m3);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/*OUTPUT*/
Matrix m1:
1 2 3
4 5 6
7 8 9
Rows:3, Columns: 3
Matrix m1copy:
1 2 3
4 5 6
7 8 9
Rows:3, Columns: 3
is m1==m1copy :true
Matrix m2:
2
5
7
Rows:3, Columns: 1
Matrix m1 * 2 (scalar multiplied by 2) :
2 4 6
8 10 12
14 16 18
Rows:3, Columns: 3
Matrix m1*m2:
66
150
234
Rows:3, Columns: 1
Matrix m3:
3
5
Rows:2, Columns: 1
Matrix m1*m3:
these two matrices cannot be multiplied