karol_dziachan

matrix multiplication and sum

Nov 12th, 2019
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. Class with methods:
  2. ------------------------------------------------------------------------------------------------------
  3. public class Matrix {
  4.  
  5.     public int size1, size2, size3, size4;
  6.     public int[][] matrix1; int[][] matrix2; int[][] matrixendMulti; int[][] matrixendSum;
  7.  
  8.     Matrix()
  9.     {
  10.         size1=0;
  11.         size2=0;
  12.         size3=0;
  13.         size4=0;
  14.  
  15.         matrix1=new int [size1][size2];
  16.         matrix2=new int [size3][size4];
  17.         matrixendMulti= new int [size1][size4];
  18.         matrixendSum= new int[size1][size2];
  19.  
  20.     }
  21.  
  22.     Matrix(int size1, int size2, int size3, int size4, int K)
  23.     {
  24.  
  25.             this.size1 = size1;
  26.             this.size2 = size2;
  27.             this.size3 = size3;
  28.             this.size4 = size4;
  29.  
  30.             matrix1= new int [size1][size2];
  31.             matrix2= new int [size3][size4];
  32.             matrixendMulti= new int [size1][size4];
  33.             matrixendSum = new int [size1][size2];
  34.  
  35.             for(int i=0; i< matrix1.length; i++)
  36.             {
  37.  
  38.                 for(int j=0; j<matrix1[0].length; j++)
  39.                     matrix1[i][j] = (int) (Math.random()*K)+0;
  40.  
  41.             }
  42.  
  43.             for(int i=0; i< matrix2.length; i++)
  44.             {
  45.                 for(int j=0; j<matrix2[0].length; j++)
  46.                     matrix2[i][j] = (int) (Math.random()*K)+0;
  47.             }
  48.         }
  49.  
  50.     boolean checkMultiplication(int size2, int size3)
  51.     {
  52.  
  53.         if(size2==size3)
  54.             return true;
  55.  
  56.         return false;
  57.     }
  58.  
  59.     boolean checkSum(int size1, int size2, int size3, int size4)
  60.     {
  61.  
  62.         if(size1==size3 && size2==size4)
  63.             return true;
  64.  
  65.         return false;
  66.  
  67.     }
  68.  
  69.  
  70.   private void multiplication()
  71.    {
  72.  
  73.            for (int i = 0; i < matrix1.length; i++)
  74.            {
  75.                for (int j = 0; j < matrix1.length; j++)
  76.                {
  77.                    matrixendMulti[i][j]=0;
  78.  
  79.                    for (int k = 0; k < matrix1[0].length; k++)
  80.  
  81.                        matrixendMulti[i][j] += matrix1[i][k] * matrix2[k][j];
  82.                }
  83.  
  84.            }
  85.    }
  86.  
  87.     void displayMultiplication()
  88.     {
  89.         if(checkMultiplication(size2, size3))
  90.         {
  91.  
  92.         multiplication();
  93.  
  94.             System.out.println("Multiplication:");
  95.  
  96.             for (int i = 0; i < matrixendMulti.length; i++)
  97.             {
  98.                 System.out.println();
  99.  
  100.                 for (int j = 0; j < matrixendMulti[0].length; j++)
  101.                     System.out.print(matrixendMulti[i][j]+" ");
  102.             }
  103.         }
  104.               else
  105.         System.out.println("The multiplication is not possible");
  106.  
  107.     }
  108.  
  109.     private void sum()
  110.     {
  111.  
  112.         for(int i=0; i<matrixendSum.length; i++)
  113.         {
  114.             for(int j=0; j<matrixendSum[0].length; j++)
  115.                 matrixendSum[i][j]+=matrix1[i][j] + matrix2[i][j];
  116.  
  117.         }
  118.  
  119.     }
  120.  
  121.     void displaySum()
  122.     {
  123.         if(checkSum(size1, size2, size3, size4))
  124.         {
  125.             sum();
  126.  
  127.             System.out.println("Sum:");
  128.  
  129.             for(int i=0; i<matrixendSum.length; i++)
  130.             {
  131.                 System.out.println();
  132.  
  133.                 for(int j=0; j<matrixendSum[0].length; j++)
  134.                     System.out.print(matrixendSum[i][j]+" ");
  135.  
  136.             }
  137.         }
  138.         else
  139.             System.out.println("Addition is not possible");
  140.     }
  141. }
  142. ----------------------------------------------------------------------------------------------------------------
  143. Class with tester:
  144. ------------------------------------------------------------------------------------------------------------
  145. import java.util.Scanner;
  146.  
  147. public class MatrixTester {
  148.     public static void main(String[] args)
  149.     {
  150.         Scanner input = new Scanner(System.in);
  151.  
  152.         int size1, size2, size3, size4, K;
  153.  
  154.         System.out.println("Enter the dimensions of the first matrix");
  155.         size1=input.nextInt();
  156.         size2=input.nextInt();
  157.  
  158.         System.out.println("Enter the dimensions of the second matrix");
  159.         size3=input.nextInt();
  160.         size4=input.nextInt();
  161.  
  162.         System.out.println("Enter the maximum numbers on the matrix");
  163.         K=input.nextInt();
  164.  
  165.         Matrix test = new Matrix(size1, size2, size3, size4, K);
  166.         test.displayMultiplication();
  167.         test.displaySum();
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment