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

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;

}

Write a program containing the main) method that uses your implementation of the Matrix class Add a new method to matrix called getMax which returns the highest value in the 2D array. Create an array of 5 Matrix objects with random dimensions. Print the string representation of each Matrix to System.out with it. You can find an implementation of selection sort in the lecture code. You simply have to modify it to work for Write a method based on selection sort that sorts matrix objects by the result of the getMax method and sort your array Matrices. . Again, print the string representation of each Matrix to System.out. Visually confirm that the matrices are sorted by Again, print the string representation of each Matrix to System.out. Visually confirm that the matrices are sorted by getMax Make sure that it is easy to read your output) The program should not require any user interaction Call your files: SortingMatrices.java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!