Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matrix Class In this assignment, you will revisit the matrix from the last assig

ID: 3681404 • Letter: M

Question

Matrix Class In this assignment, you will revisit the matrix from the last assignment. In this version you will encapsulate the data (i.e. two dimensional array) that represents the matrix and the operations that can be performed on it into a single class called Matrix. Your class should be able to handle matrices for which the number of rows and number of columns are between 1 and 5 (inclusive). If the client of the class attempts to create a matrix with fewer rows/columns, the number should default to 1 (do not print any error message). If the client of the class attempts to create a matrix with more rows/columns, the number should default to 5 (do not print any error message). These rules should be enforced by the constructors. You will have two constructors. The first constructor will take rows and columns as parameters and create the matrix filled with randomly generated integers in the range from -10 to 10 (inclusive). The row and column parameters should obey the criteria specified earlier in this assignment. If they are violated use the defaults as specified above. The second constructor will take a single value called 'dimensions' that is used for both the rows and the columns. It should be validated in the same manner as specified above. The class should provide the methods that provide information about number of rows and number of columns of the matrix called getNumOfRows() and getNumOfColumns(). The class should provide methods for row max, column sum and transpose operations (see original matrix assignment), but with one difference - the transpose method should create a new modified matrix that replaces the matrix on which the operation is performed. The class should provide a toString() method that returns a String object containing the representation of the matrix. The string should be multi-lined and contain the data organized into aligned columns. The class should provide an add(...) method. The method should take another matrix object as a parameter and modify the current object by adding to it the parameter object. Two matrices can be added only if they have the same shape (same number of rows and same number of columns). The method should return true, if the addition can be carried out and false, if it cannot. Two matrices that have the same shape are added by adding their corresponding entries together. For example: if matrix m_1 is and matrix m_2 is then a call to ml. add (m_2) should return true and change m_1 to be If then we try to add matrix m_3 to m_1 by calling m_1. add (m_3), the method should return false and ml should not be changed. The class should provide a multiply method. The method should take an integer as its parameter and modify the current object by multiplying each matrix entry by the value of the parameter.

Explanation / 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();
   }
  
   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 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;
   }
}

###################### Test.java #################

public class Test {
  
   public static void main(String[] args) {
      
       // creating a two matrix Object
       Matrix m1 = new Matrix(6);
       System.out.println("Row: "+m1.getNumOfRows()+", and Columns: "+m1.getNumOfColumns());
       System.out.println(m1.toString());
      
       System.out.println();
       // Creating other Object
       Matrix m2 = new Matrix(5, 5);
       System.out.println("Row: "+m2.getNumOfRows()+", and Columns: "+m2.getNumOfColumns());
       System.out.println(m2.toString());
       System.out.println();
       System.out.println("Addition of m1 + m2");
       m1.add(m2);
       System.out.println(m1.toString());
   }

}

/*

Output:

Row: 5, and Columns: 5
9   9   8   -8   -1  
5   -9   4   9   -5  
-9   -2   -4   -3   2  
-3   -9   1   5   -7  
-6   10   -3   -8   4  


Row: 5, and Columns: 5
3   -10   -8   -4   5  
-7   -10   8   -7   -5  
7   -9   5   -7   -3  
3   -4   3   3   5  
2   6   2   -7   -10  


Addition of m1 + m2
12   -1   0   -12   4  
-2   -19   12   2   -10  
-2   -11   1   -10   -1  
0   -13   4   8   -2  
-4   16   -1   -15   -6  

*/