Question: Below is Matrix class. import java.util.Random; public class Matrix { private int[][] matrix; private int row; private int col; public Matrix(int r, int c) {

Below is Matrix class.
import java.util.Random;
public class Matrix {
private int[][] matrix;
private int row;
private int col;
public Matrix(int r, int c) {
row = value(r);
col = value(c);
// creating matrix
matrix = new int[row][col];
// calling function to fill matrix with random number in range [-10 10]
createMatrixAndFill();
}
public Matrix(int dimension) {
row = value(dimension);
col = row;
// creating matrix
matrix = new int[row][col];
createMatrixAndFill();
}
// function that takes row or column value and return after applying constraint
private int value(int num){
if(num
num=1;
else if(num>5)
num = 5;
return num;
}
// function to fill matrix with random value in range [-10 10]
private void createMatrixAndFill(){
Random random = new Random();
for(int i=0; i for(int j=0; j matrix[i][j] = random.nextInt(21) -10; } public int returnNum(int x, int y){ return matrix[x][y]; } public int getNumOfRows(){ return row; } public int getNumOfColumns(){ return col; } public int [][] getMatrxi(){ return matrix; } // transpose function public void transpose(){ int transpose[][] = new int[col][row]; for ( int c = 0 ; c { for ( int d = 0 ; d transpose[d][c] = matrix[c][d]; } // now assigning transpose to matrix matrix = transpose; // changing row and col int temp = row; row = col; col = temp; } public boolean add(Matrix m){ // if matrix can not be added if(row != m.getNumOfRows() && col!=m.getNumOfColumns()) return false; // getting matrix of m int temp[][] = m.getMatrxi(); // adding corresponding elements of two matrix and storing matrix for(int i=0; i for(int j=0; j matrix[i][j] = matrix[i][j] + temp[i][j]; return true; } public void multiply(int num){ for(int i=0; i for(int j=0; j matrix[i][j] = num*matrix[i][j]; } @Override public String toString() { String s = ""; for(int i=0; i for(int j=0; j s = s+matrix[i][j]+" "; } s = s+" "; } return s; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
