Advertisement
kocev

The Matrix

Feb 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1.  
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class TheMatrix {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         System.out.println("Enter a size of matrix < 10");
  9.         String input = scanner.nextLine().trim();
  10.         int n;
  11.         while (!input.matches("\\d+") || Integer.parseInt(input) >= 10) {
  12.             System.out.println("The size must be numeric and less than 10. Try again: ");
  13.             input = scanner.nextLine().trim();
  14.         }
  15.         n = Integer.parseInt(input);
  16.         int[][] matrix1 = new int[n][n];
  17.         int[][] matrix2 = new int[n][n];
  18.         System.out.println("Enter the first matrix row by row. The elements must be eparated by ',': ");
  19.         enterMatrix(matrix1);
  20.         System.out.println("Enter the second matrix row by row. The elements must be eparated by ',': ");
  21.         enterMatrix(matrix2);
  22.         printTwoMatrixInParallel(matrix1, matrix2);
  23.         printSumOFElements(matrix1, matrix2);
  24.         printDirectSum(matrix1, matrix2);
  25.  
  26.     }
  27.  
  28.     public static void enterMatrix(int[][] matrix) {
  29.         Scanner scanner = new Scanner(System.in);
  30.         for (int i = 0; i < matrix.length; i++) {
  31.             String[] inputArr = scanner.nextLine().split(",".trim());
  32.             boolean isDigit = true;
  33.             while (inputArr.length != matrix.length || Arrays.asList(inputArr).stream().anyMatch(element -> element.matches("\\D+"))) {
  34.                 System.out.printf("Try again! You should enter %d numeric elements: %n", matrix.length);
  35.                 inputArr = scanner.nextLine().split(",".trim());
  36.             }
  37.             for (int j = 0; j < inputArr.length; j++) {
  38.                 matrix[i][j] = Integer.parseInt(inputArr[j]);
  39.             }
  40.         }
  41.     }
  42.  
  43.     public static void printTwoMatrixInParallel(int[][] firstMatrix, int[][] secondMatrix) {
  44.         System.out.println("The both matrices are:");
  45.         for (int i = 0; i < firstMatrix.length; i++) {
  46.             for (int j = 0; j < firstMatrix.length; j++) {
  47.                 System.out.printf("%5d ", firstMatrix[i][j]);
  48.             }
  49.             System.out.print(" | ");
  50.             for (int k = 0; k < firstMatrix.length; k++) {
  51.                 System.out.printf("%5d ", secondMatrix[i][k]);
  52.             }
  53.             System.out.println();
  54.         }
  55.         System.out.println();
  56.     }
  57.  
  58.     public static void printSumOFElements(int[][] firstMatrix, int[][] secondMatrix) {
  59.         System.out.println("The sum of elements is:");
  60.         for (int i = 0; i < firstMatrix.length; i++) {
  61.             for (int j = 0; j < firstMatrix.length; j++) {
  62.                 System.out.printf("%5d ", firstMatrix[i][j] + secondMatrix[i][j]);
  63.             }
  64.             System.out.println();
  65.         }
  66.         System.out.println();
  67.     }
  68.  
  69.     public static void printDirectSum(int[][] firstMatrix, int[][] secondMatrix) {
  70.         int n = firstMatrix.length;
  71.         System.out.println("The direct sum is: ");
  72.         for (int i = 0; i < n + n; i++) {
  73.             for (int j = 0; j < n + n; j++) {
  74.                 if (i < n && j < n) {
  75.                     System.out.printf("%5d ", firstMatrix[i][j]);
  76.                 }
  77.                 if (i < n && j >= n) {
  78.                     System.out.printf("%5d ", 0);
  79.                 }
  80.                 if (i >= n && j < n) {
  81.                     System.out.printf("%5d ", 0);
  82.                 }
  83.                 if (i >= n && j >= n) {
  84.                     System.out.printf("%5d ", secondMatrix[i - n][j - n]);
  85.                 }
  86.             }
  87.  
  88.             System.out.println();
  89.         }
  90.         System.out.println();
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement