Below is Matrix class. import java.util.Random; public class Matrix { private in
ID: 3684487 • Letter: B
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) {
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<1)
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<row; i++)
for(int j=0; j<col; 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 < row ; c++ )
{
for ( int d = 0 ; d < col ; 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<row; i++)
for(int j=0; j<col; j++)
matrix[i][j] = matrix[i][j] + temp[i][j];
return true;
}
public void multiply(int num){
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
matrix[i][j] = num*matrix[i][j];
}
@Override
public String toString() {
String s = "";
for(int i=0; i<row; i++){
for(int j=0; j<col; 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.javaExplanation / Answer
Matrix.java
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();
String s = toString();
System.out.println("The Input Matrix is : ");
System.out.println(s);
matrix = getMax(matrix);
s = toString();
System.out.println("The Final Matrix after sorting is : ");
System.out.println(s);
}
public Matrix(int dimension) {
row = value(dimension);
col = row;
// creating matrix
matrix = new int[row][col];
createMatrixAndFill();
String s = toString();
System.out.println(s);
getMax(matrix);
}
// function that takes row or column value and return after applying constraint
private int value(int num){
if(num<1)
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<row; i++)
for(int j=0; j<col; 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 < row ; c++ )
{
for ( int d = 0 ; d < col ; 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<row; i++)
for(int j=0; j<col; j++)
matrix[i][j] = matrix[i][j] + temp[i][j];
return true;
}
public void multiply(int num){
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
matrix[i][j] = num*matrix[i][j];
}
@Override
public String toString() {
String s = "";
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
s = s+matrix[i][j]+" ";
}
s = s+" ";
}
return s;
}
public static int[][] getMax(int[][] list) {
int t=0;
for(int x=0;x<list.length;x++)
{
for(int y=0;y<list[x].length;y++)
{
for(int i=0;i<list.length;i++)
{
for(int j=0;j<list[i].length;j++)
{
if(list[i][j]>list[x][y])
{
t=list[x][y];
list[x][y]=list[i][j];
list[i][j]=t;
}
}
}
}
}
return list;
}
}
SortingMatrices.java
public class SortingMatrices {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Matrix m = new Matrix(5, 5);
}
}
Output:
The Input Matrix is :
-4 -4 -9 1 7
2 -9 -1 10 9
-3 -3 9 -3 -8
-10 -3 8 -9 -6
8 7 -6 -2 -9
The Final Matrix after sorting is :
-10 -9 -9 -9 -9
-8 -6 -6 -4 -4
-3 -3 -3 -3 -2
-1 1 2 7 7
8 8 9 9 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.