Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class TheMatrix {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter a size of matrix < 10");
- String input = scanner.nextLine().trim();
- int n;
- while (!input.matches("\\d+") || Integer.parseInt(input) >= 10) {
- System.out.println("The size must be numeric and less than 10. Try again: ");
- input = scanner.nextLine().trim();
- }
- n = Integer.parseInt(input);
- int[][] matrix1 = new int[n][n];
- int[][] matrix2 = new int[n][n];
- System.out.println("Enter the first matrix row by row. The elements must be eparated by ',': ");
- enterMatrix(matrix1);
- System.out.println("Enter the second matrix row by row. The elements must be eparated by ',': ");
- enterMatrix(matrix2);
- printTwoMatrixInParallel(matrix1, matrix2);
- printSumOFElements(matrix1, matrix2);
- printDirectSum(matrix1, matrix2);
- }
- public static void enterMatrix(int[][] matrix) {
- Scanner scanner = new Scanner(System.in);
- for (int i = 0; i < matrix.length; i++) {
- String[] inputArr = scanner.nextLine().split(",".trim());
- boolean isDigit = true;
- while (inputArr.length != matrix.length || Arrays.asList(inputArr).stream().anyMatch(element -> element.matches("\\D+"))) {
- System.out.printf("Try again! You should enter %d numeric elements: %n", matrix.length);
- inputArr = scanner.nextLine().split(",".trim());
- }
- for (int j = 0; j < inputArr.length; j++) {
- matrix[i][j] = Integer.parseInt(inputArr[j]);
- }
- }
- }
- public static void printTwoMatrixInParallel(int[][] firstMatrix, int[][] secondMatrix) {
- System.out.println("The both matrices are:");
- for (int i = 0; i < firstMatrix.length; i++) {
- for (int j = 0; j < firstMatrix.length; j++) {
- System.out.printf("%5d ", firstMatrix[i][j]);
- }
- System.out.print(" | ");
- for (int k = 0; k < firstMatrix.length; k++) {
- System.out.printf("%5d ", secondMatrix[i][k]);
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void printSumOFElements(int[][] firstMatrix, int[][] secondMatrix) {
- System.out.println("The sum of elements is:");
- for (int i = 0; i < firstMatrix.length; i++) {
- for (int j = 0; j < firstMatrix.length; j++) {
- System.out.printf("%5d ", firstMatrix[i][j] + secondMatrix[i][j]);
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void printDirectSum(int[][] firstMatrix, int[][] secondMatrix) {
- int n = firstMatrix.length;
- System.out.println("The direct sum is: ");
- for (int i = 0; i < n + n; i++) {
- for (int j = 0; j < n + n; j++) {
- if (i < n && j < n) {
- System.out.printf("%5d ", firstMatrix[i][j]);
- }
- if (i < n && j >= n) {
- System.out.printf("%5d ", 0);
- }
- if (i >= n && j < n) {
- System.out.printf("%5d ", 0);
- }
- if (i >= n && j >= n) {
- System.out.printf("%5d ", secondMatrix[i - n][j - n]);
- }
- }
- System.out.println();
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement