//File: Matrix.java //Name: Trai Pham //Date: 01/30/2020 //Course: CSE 8B //This code would allow you to manipulate matrix/matrices, in which you can //set elements of a matrix, add matrices together, multiple matrices, or //transpose a matrix. //#file header: this file contains the Matrix code and Matrixtester //#class header: this class does a lot to an object matrix and it has methods //that are able to manipulate the matrics public class Matrix{ private int row; private int column; private int[][] arr; private static final String ADD_ERROR = "Matrices cannot be added"; private static final String MULTIPLY_ERROR = "Matrices cannot be multiplied"; //Constructor Method public Matrix(){ row = 0; column = 0; arr = null; } //Assigning the row and column to Matrix /** * @param int row and col * @return Matrix object */ public Matrix(int row, int column){ this.row = row; this.column = column; if(row < 0 || column < 0){ this.row = 0; this.column = 0; this.arr = null; } else{ this.arr= new int[this.row][this.column]; } return; } //Create a deep copy of Matrix /** * @param Matrix object * @return Matrix */ public Matrix(Matrix mat){ this.row = mat.row; this.column = mat.column; this.arr = new int[mat.row][mat.column]; //array of matrix has the same number of rows and Columns //as the original for(int i = 0; i < mat.row; i++ ){ for(int j = 0; j < mat.column; j++){ this.arr[i][j] = mat.arr[i][j]; } } } //Set the value for a Matrix at a specific index (r,c) /** * @param int r,c, element * @return boolean */ public boolean setElement(int r, int c, int element){ //invalid input/matrices if(r >= this.row){ return false; } else if(c >= this.column){ return false; } else if(r< 0){ return false; } else if(c < 0){ return false; } //assigning value to specific index else{ this.arr[r][c] = element; return true; } } //gets the Element/value of a Matrix at a specific index /** * @param int r,c * @return Integer */ public Integer getElement(int r, int c) { //invalid inputs/matrices if(r >= this.row || c >= this.column || r < 0 || c < 0){ return null; } else{ return this.arr[r][c]; } } //Gets the row of the matrix public int getRows(){ return this.row; } //Gets the column of the matrix public int getColumns(){ return this.column; } //Convert a matrix that contains integers into a string //This would help you print the matrix /** * @param void * @return string */ @Override public String toString(){ String str = "";//declares and initialize a String variable for(int i = 0; i < this.row; i++){ for( int j = 0; j < this.column; j++){ str += (this.arr[i][j] + " " ); //this would print a new line when you reach the end of column if( j >= this.column - 1){ str += "\n"; } } } return str; } //This would let you add two matrices together /** * @param Matrix object * @return Matrix */ public Matrix add(Matrix y){ // invalid inputs/matrices if( y == null){ return null; } else if( this.arr == null){ return null; } Matrix a = new Matrix(y); //create a new Matrix to get the sum of values at specific index if( y.row == this.row && y.column == this.column){ for( int i = 0; i < this.row; i++){ for( int j = 0; j < this.column; j++){ a.arr[i][j] = y.arr[i][j] + this.arr[i][j]; } } return a; } else{ System.out.println(ADD_ERROR); return null; } } //This would let you multiply two matrices based on the multiplication //rules of matrices, and you'll get the product of those two matrices /** * @param Matrix object * @return Matrix */ public Matrix multiply(Matrix y){ //null matrices if(y == null){//Null Matrix return null; } else if (this.column != y.getRows()){//Error message System.out.println(MULTIPLY_ERROR); return null; } //the new matrix should have the row of the first matrix, and //the column numbers of the called matrix Matrix newMat = new Matrix(this.row, y.getColumns()); if(this.column == y.row){ for( int i = 0; i < newMat.row; i++){//Get row of new Matrix for(int j = 0; j < newMat.column; j++){ //Get the column of new Matrix int num = 0; for(int a = 0; a < this.column; a++){ num += (this.arr[i][a] * y.arr[a][j]); // this would get you //the sum when you multiply column of first matrix to row of //second matrix } newMat.arr[i][j] = num; } } return newMat; } else{ return null; } } //This would flip the rows and columns of a Matrix, though keeping //the same value /** * @param void * @return Matrix */ public Matrix transpose (){ Matrix tranMat = new Matrix(this.column, this.row); for(int i = 0; i < this.column; i++){ for(int j = 0; j < this.row; j++){ //flip the rows and the columns in order to efficiently transpose tranMat.arr[i][j] = this.arr[j][i]; } } return tranMat; } }