earlution

Stage 1/6: Addition - Matrix

Mar 30th, 2021 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. package processor;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Matrix {
  6.     Scanner scanner = new Scanner(System.in);
  7.     int[][] matrix;
  8.     int rows;
  9.     int columns;
  10.  
  11.     public Matrix(int n, int m) {
  12.         this.matrix = new int[n][m];
  13.         this.rows = n;
  14.         this.columns = m;
  15.     }
  16.  
  17.     /**
  18.      * This method will read int values from System.in to initialise matrix class attribute.
  19.      */
  20.     public void readMatrix() {
  21.         for (int i = 0; i < this.rows; ++i) {
  22.             for (int j = 0; j < this.columns; ++j) {
  23.                 this.matrix[i][j] = scanner.nextInt();
  24.             }
  25.         }
  26.     }
  27.  
  28.     /**
  29.      * Take a matrix and adds it to its own matrix class attribute.
  30.      * @param matrixToAdd The matrix to be added to its own matrix class attribute.
  31.      * @return addedMatrix The sum of matrixToAdd and own matrix class attribute.
  32.      */
  33.     public Matrix addMatrix(Matrix matrixToAdd) {
  34.         Matrix addedMatrix = new Matrix(this.rows, this.columns);
  35.         for (int n = 0; n < this.rows; n++) {
  36.             for (int m = 0; m < this.columns; m++) {
  37.                 addedMatrix.matrix[n][m] = this.matrix[n][m] + matrixToAdd.matrix[n][m];
  38.             }
  39.         }
  40.         return addedMatrix;
  41.     }
  42.  
  43.     /**
  44.      * Outputs matrix class attribute on System.out, space delimited.
  45.      */
  46.     public void printMatrix() {
  47.         for (int n = 0; n < this.rows; n++) {
  48.             for (int m = 0; m < this.columns; m++) {
  49.                 System.out.print(this.matrix[n][m] + " ");
  50.             }
  51.             System.out.println();
  52.         }
  53.     }
  54. }
  55.  
Add Comment
Please, Sign In to add comment