In: Computer Science
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
Write a Java program for a matrix class that can add and multiply arbitrary two dimensional arrays of integers.
Code :-
import java.util.Scanner;
public class Matrix {
        public static void main(String[] args) {
        
                while(true)
                {
                int choice;
                Scanner input = new Scanner(System.in);
                
                System.out.println("Press 1 to add two matrix:");
                System.out.println("Press 2 to multiply two matrix:");
                System.out.println("Press 3 to exit:");
                System.out.println("Enter your choice:");
                choice = input.nextInt();
                
                
                switch(choice)
                {
                case 1:
                        int m, n, c, d;
                        
                        Scanner sc = new Scanner(System.in);
                    System.out.println("Enter the number of rows and columns of matrix");
                    m = sc.nextInt();
                    n = sc.nextInt();
                    int first[][] = new int[m][n];
                    int second[][] = new int[m][n];
                    int sum[][] = new int[m][n];
                 
                    System.out.println("Enter the elements of first matrix");
                    for (c = 0; c < m; c++)
                      for (d = 0; d < n; d++)
                        first[c][d] = sc.nextInt();
                    System.out.println("Enter the elements of second matrix");
                    for (c = 0 ; c < m; c++)
                      for (d = 0 ; d < n; d++)
                        second[c][d] = sc.nextInt();
                    for (c = 0; c < m; c++)
                      for (d = 0; d < n; d++)
                        sum[c][d] = first[c][d] + second[c][d];  
                    System.out.println("Sum of the matrices:");
                    for (c = 0; c < m; c++)
                    {
                      for (d = 0; d < n; d++)
                        System.out.print(sum[c][d] + "\t");
                      System.out.println();
                    }
                    break;
                case 2:
                         int m1, n1, p, q, sum1 = 0, c1, d1, k;
                         
                            Scanner in = new Scanner(System.in);
                            System.out.println("Enter the number of rows and columns of first matrix");
                            m1 = in.nextInt();
                            n1 = in.nextInt();
                         
                            int first1[][] = new int[m1][n1];
                         
                            System.out.println("Enter elements of first matrix");
                         
                            for (c1 = 0; c1 < m1; c1++)
                              for (d1 = 0; d1 < n1; d1++)
                                first1[c1][d1] = in.nextInt();
                         
                            System.out.println("Enter the number of rows and columns of second matrix");
                            p = in.nextInt();
                            q = in.nextInt();
                         
                            if (n1 != p)
                              System.out.println("The matrices can't be multiplied with each other.");
                            else
                            {
                              int second1[][] = new int[p][q];
                              int multiply[][] = new int[m1][q];
                         
                              System.out.println("Enter elements of second matrix");
                         
                              for (c1 = 0; c1 < p; c1++)
                                for (d1 = 0; d1 < q; d1++)
                                  second1[c1][d1] = in.nextInt();
                         
                              for (c1 = 0; c1 < m1; c1++) {
                                for (d1 = 0; d1 < q; d1++) {
                                  for (k = 0; k < p; k++)
                                    sum1 = sum1 + first1[c1][k]*second1[k][d1];
                         
                                  multiply[c1][d1] = sum1;
                                  sum1 = 0;
                                }
                              }
                         
                              System.out.println("Product of the matrices:");
                         
                              for (c1 = 0; c1 < m1; c1++) {
                                for (d1 = 0; d1 < q; d1++)
                                  System.out.print(multiply[c1][d1]+"\t");
                         
                                System.out.print("\n");
                              }
                            }
                        
                break;
                case 3:
                        System.out.println("you quit the program");
                         System.exit(0);
            }   
        }
}
}
Output :-

