Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class with methods:
- ------------------------------------------------------------------------------------------------------
- public class Matrix {
- public int size1, size2, size3, size4;
- public int[][] matrix1; int[][] matrix2; int[][] matrixendMulti; int[][] matrixendSum;
- Matrix()
- {
- size1=0;
- size2=0;
- size3=0;
- size4=0;
- matrix1=new int [size1][size2];
- matrix2=new int [size3][size4];
- matrixendMulti= new int [size1][size4];
- matrixendSum= new int[size1][size2];
- }
- Matrix(int size1, int size2, int size3, int size4, int K)
- {
- this.size1 = size1;
- this.size2 = size2;
- this.size3 = size3;
- this.size4 = size4;
- matrix1= new int [size1][size2];
- matrix2= new int [size3][size4];
- matrixendMulti= new int [size1][size4];
- matrixendSum = new int [size1][size2];
- for(int i=0; i< matrix1.length; i++)
- {
- for(int j=0; j<matrix1[0].length; j++)
- matrix1[i][j] = (int) (Math.random()*K)+0;
- }
- for(int i=0; i< matrix2.length; i++)
- {
- for(int j=0; j<matrix2[0].length; j++)
- matrix2[i][j] = (int) (Math.random()*K)+0;
- }
- }
- boolean checkMultiplication(int size2, int size3)
- {
- if(size2==size3)
- return true;
- return false;
- }
- boolean checkSum(int size1, int size2, int size3, int size4)
- {
- if(size1==size3 && size2==size4)
- return true;
- return false;
- }
- private void multiplication()
- {
- for (int i = 0; i < matrix1.length; i++)
- {
- for (int j = 0; j < matrix1.length; j++)
- {
- matrixendMulti[i][j]=0;
- for (int k = 0; k < matrix1[0].length; k++)
- matrixendMulti[i][j] += matrix1[i][k] * matrix2[k][j];
- }
- }
- }
- void displayMultiplication()
- {
- if(checkMultiplication(size2, size3))
- {
- multiplication();
- System.out.println("Multiplication:");
- for (int i = 0; i < matrixendMulti.length; i++)
- {
- System.out.println();
- for (int j = 0; j < matrixendMulti[0].length; j++)
- System.out.print(matrixendMulti[i][j]+" ");
- }
- }
- else
- System.out.println("The multiplication is not possible");
- }
- private void sum()
- {
- for(int i=0; i<matrixendSum.length; i++)
- {
- for(int j=0; j<matrixendSum[0].length; j++)
- matrixendSum[i][j]+=matrix1[i][j] + matrix2[i][j];
- }
- }
- void displaySum()
- {
- if(checkSum(size1, size2, size3, size4))
- {
- sum();
- System.out.println("Sum:");
- for(int i=0; i<matrixendSum.length; i++)
- {
- System.out.println();
- for(int j=0; j<matrixendSum[0].length; j++)
- System.out.print(matrixendSum[i][j]+" ");
- }
- }
- else
- System.out.println("Addition is not possible");
- }
- }
- ----------------------------------------------------------------------------------------------------------------
- Class with tester:
- ------------------------------------------------------------------------------------------------------------
- import java.util.Scanner;
- public class MatrixTester {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- int size1, size2, size3, size4, K;
- System.out.println("Enter the dimensions of the first matrix");
- size1=input.nextInt();
- size2=input.nextInt();
- System.out.println("Enter the dimensions of the second matrix");
- size3=input.nextInt();
- size4=input.nextInt();
- System.out.println("Enter the maximum numbers on the matrix");
- K=input.nextInt();
- Matrix test = new Matrix(size1, size2, size3, size4, K);
- test.displayMultiplication();
- test.displaySum();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment