Java >> Tutoriel Java >  >> Java

Programme piloté par menu pour les opérations matricielles en Java

Programme piloté par menus pour les opérations matricielles en Java | Description du programme :- Écrivez un programme Java pour le programme piloté par menu pour les opérations matricielles. Effectuez des additions, des soustractions, des multiplications et des transpositions matricielles à l'aide du boîtier de commutation. Aidez-vous des méthodes.

Auparavant, nous avions développé plusieurs programmes Java sur des matrices telles que

  1. Programme pour imprimer une matrice 3×3
  2. Somme des éléments de matrice en Java 
  3. Somme des éléments diagonaux en Java
  4. Découvrez chaque somme de ligne et somme de colonne d'une matrice
  5. Ajout de deux Matrix en Java
  6. Soustraction de deux matrices en Java
  7. Multiplication de deux matrices en Java
  8. Transposition d'une matrice en Java

Maintenant, développons un programme pour effectuer diverses opérations matricielles d'addition, de soustraction, de multiplication, de transposition en utilisant l'instruction switch-case et le concept de méthode.

Matrix est un tableau à deux dimensions. Et pour représenter le tableau à deux dimensions, il devrait y avoir deux boucles, où les boucles externes représentent les lignes de la matrice et la boucle interne représente la colonne de la matrice. Voir plus :- Matrice en 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;
  }

}

Sortie :-

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

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :1
Somme de la matrice :
[[ 6, 8, 10], [ 12, 14, 16], [ 10, 9, 11]]

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :2
Soustraction de matrice :
[[ 4, 4, 4], [ 4, 4, 4], [ -4, -7, -7]]

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :3
Multiplication de matrice :
[[ 78, 96, 114], [ 114, 141, 168], [ 21, 27, 33]]

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :4
Transposition de la première matrice :
[[ 5, 8, 3], [ 6, 9, 1], [ 7, 10, 2]]
Transposition de la deuxième matrice :
[[ 1, 4, 7], [ 2, 5, 8], [ 3, 6, 9]]

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :6
Saisie invalide.
Veuillez saisir la bonne saisie.

Choisir l'opération matricielle,
—————————-
1. Ajout
2. Soustraction
3. Multiplication
4. Transposer
5. Quitter
—————————-
Entrez votre choix :5
Merci.


Balise Java