Java >> Java tutorial >  >> Java

Menudrevet program til matrixoperationer i Java

Menudrevet program til matrixoperationer i Java | Programbeskrivelse:- Skriv et Java-program til det menudrevne program til matrixoperationer. Udfør matrixaddition, subtraktion, multiplikation og transponering ved hjælp af switch case. Tag hjælp af metoderne.

Tidligere havde vi udviklet flere Java-programmer på matricer som

  1. Programmer til at udskrive 3×3 Matrix
  2. Summen af ​​matrixelementer i Java 
  3. Summen af ​​diagonale elementer i Java
  4. Find ud af hver rækkesum og kolonnesum af en matrix
  5. Tilføjelse af to Matrix i Java
  6. Subtraktion af to matricer i Java
  7. Multiplikation af to Matrix i Java
  8. Transponering af en matrix i Java

Lad os nu udvikle et program til at udføre forskellige matrixoperationer addition, subtraktion, multiplikation, transponering ved hjælp af switch-case-sætning og metodekoncept.

Matrix er et todimensionelt array. Og for at repræsentere det todimensionelle array skal der være to sløjfer, hvor ydre sløjfer repræsenterer rækker af matricen, og den indre sløjfe repræsenterer matrixens søjle. Se mere:- Matrix i Java

import java.util.Arrays;
import java.util.Scanner;

public class Matrix {

  // main method
  public static void main(String[] args) {

    // Scanner class object
    Scanner scan = new Scanner(System.in);

    // declare two matrix
    int a[][] = { { 5, 6, 7 }, { 8, 9, 10 }, { 3, 1, 2 } };
    int b[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    // create third matrix
    int c[][] = new int[3][3];

    // display both matrix
    System.out.println("A = " + Arrays.deepToString(a));
    System.out.println("B = " + Arrays.deepToString(b));

    // variable to take choice
    int choice;

    // menu-driven
    do {
      // menu to choose the operation
      System.out.println("\nChoose the matrix operation,");
      System.out.println("----------------------------");
      System.out.println("1. Addition");
      System.out.println("2. Subtraction");
      System.out.println("3. Multiplication");
      System.out.println("4. Transpose");
      System.out.println("5. Exit");
      System.out.println("----------------------------");
      System.out.print("Enter your choice: ");
      choice = scan.nextInt();

      switch (choice) {
      case 1:
        c = add(a, b);
        System.out.println("Sum of matrix: ");
        System.out.println(Arrays.deepToString(c));
        break;
      case 2:
        c = subtract(a, b);
        System.out.println("Subtraction of matrix: ");
        System.out.println(Arrays.deepToString(c));
        break;
      case 3:
        c = multiply(a, b);
        System.out.println("Multiplication of matrix: ");
        System.out.println(Arrays.deepToString(c));
        break;
      case 4:
        System.out.println("Transpose of the first matrix: ");
        c = transpose(a);
        System.out.println(Arrays.deepToString(c));
        System.out.println("Transpose of the second matrix: ");
        c = transpose(b);
        System.out.println(Arrays.deepToString(c));
        break;
      case 5:
        System.out.println("Thank You.");
        return;
      default:
        System.out.println("Invalid input.");
        System.out.println("Please enter the correct input.");
      }
    } while (true);
  }

  // method to perform matrix addition and
  // return resultant matrix
  public static int[][] add(int[][] a, int[][] b) {

    // calculate row and column size of anyone matrix
    int row = a.length;
    int column = a[0].length;

    // declare a matrix to store resultant value
    int sum[][] = new int[row][column];

    // calculate sum of two matrices
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        sum[i][j] = a[i][j] + b[i][j];
      }
    }

    // return resultant matrix
    return sum;
  }

  // method to perform matrix subtraction and
  // return resultant matrix
  public static int[][] subtract(int[][] a, int[][] b) {

    // calculate row and column size of anyone matrix
    int row = a.length;
    int column = a[0].length;

    // declare a matrix to store resultant value
    int sub[][] = new int[row][column];

    // calculate sum of two matrices
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        sub[i][j] = a[i][j] - b[i][j];
      }
    }

    // return resultant matrix
    return sub;
  }

  // method to perform matrix multiplication and
  // return resultant matrix
  // passed matrices can be square or non-square matrix
  public static int[][] multiply(int[][] a, int[][] b) {

    // find row size of first matrix
    int row = a.length;
    // find column size of second matrix
    int column = b[0].length;

    // declare new matrix to store result
    int product[][] = new int[row][column];

    // find product of both matrices
    // outer loop up to row of A
    for (int i = 0; i < row; i++) {
      // inner-1 loop utp0 column of B
      for (int j = 0; j < column; j++) {
        // assign 0 to the current element
        product[i][j] = 0;

        // inner-2 loop up to A[0].length
        for (int k = 0; k < a[0].length; k++) {
          product[i][j] += a[i][k] * b[k][j];
        }
      }
    }
    return product;
  }

  // method to find transpose of a matrix
  public static int[][] transpose(int[][] a) {

    // calculate row and column size
    int row = a.length;
    int column = a[0].length;

    // declare a matrix to store resultant
    int temp[][] = new int[row][column];

    // calculate transpose of matrix
    // outer loop for row
    for (int i = 0; i < row; i++) {
      // inner loop for column
      for (int j = 0; j < column; j++) {
        // formula
        temp[i][j] = a[j][i];
      }
    }

    // return resultant matrix
    return temp;
  }

}

Output:-

A =[[ 5, 6, 7], [ 8, 9, 10], [ 3, 1, 2]]
B =[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]]

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:1
Sum af matrix:
[[ 6, 8, 10], [ 12, 14, 16], [ 10, 9, 11]]

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:2
Subtraktion af matrix:
[[ 4, 4, 4], [ 4, 4, 4], [ -4, -7, -7]]

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:3
Multiplikation af matrix:
[[ 78, 96, 114], [ 114, 141, 168], [ 21, 27, 33]]

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:4
Transponer den første matrix:
[[ 5, 8, 3], [ 6, 9, 1], [ 7, 10, 2]]
Transponering af den anden matrix:
[[ 1, 4, 7], [ 2, 5, 8], [ 3, 6, 9]]

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:6
Ugyldigt input.
Indtast venligst det korrekte input.

Vælg matrixoperationen,
——————————
1. Tilføjelse
2. Subtraktion
3. Multiplikation
4. Transponer
5. Afslut
——————————-
Indtast dit valg:5
Tak.


Java tag