Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package processor;
- import java.util.Scanner;
- public class Matrix {
- Scanner scanner = new Scanner(System.in);
- int[][] matrix;
- int rows;
- int columns;
- public Matrix(int n, int m) {
- this.matrix = new int[n][m];
- this.rows = n;
- this.columns = m;
- }
- /**
- * This method will read int values from System.in to initialise matrix class attribute.
- */
- public void readMatrix() {
- for (int i = 0; i < this.rows; ++i) {
- for (int j = 0; j < this.columns; ++j) {
- this.matrix[i][j] = scanner.nextInt();
- }
- }
- }
- /**
- * Take a matrix and adds it to its own matrix class attribute.
- * @param matrixToAdd The matrix to be added to its own matrix class attribute.
- * @return addedMatrix The sum of matrixToAdd and own matrix class attribute.
- */
- public Matrix addMatrix(Matrix matrixToAdd) {
- Matrix addedMatrix = new Matrix(this.rows, this.columns);
- for (int n = 0; n < this.rows; n++) {
- for (int m = 0; m < this.columns; m++) {
- addedMatrix.matrix[n][m] = this.matrix[n][m] + matrixToAdd.matrix[n][m];
- }
- }
- return addedMatrix;
- }
- /**
- * Outputs matrix class attribute on System.out, space delimited.
- */
- public void printMatrix() {
- for (int n = 0; n < this.rows; n++) {
- for (int m = 0; m < this.columns; m++) {
- System.out.print(this.matrix[n][m] + " ");
- }
- System.out.println();
- }
- }
- }
Add Comment
Please, Sign In to add comment