Answer to Question #244906 in Java | JSP | JSF for sundar

Question #244906
Write a program in Java to perform the following operations on Matrix using
multithreading. Get the required input matrix from the user and after that run
three threads to perform the following operation.
a. Addition
b. Subtraction
c. Multiplication
1
Expert's answer
2021-10-01T01:31:58-0400
package com.company;
import java.util.*;
class matrices
{
    Scanner sc = new Scanner(System.in);
    //setting matrix 1
    int matrix_1[][]  = set_matrix_1();
    public int[][] set_matrix_1()
    {
        System.out.println("Enter m * n for the matrix 1");
        int m1 = sc.nextInt();
        int n1 = sc.nextInt();

        System.out.println("Enter the first matrix");
        int arr1[][] = new int[m1][n1];
        for(int i = 0; i<m1; i++)
        {
            for(int j = 0; j<n1; j++)
            {
                arr1[i][j] = sc.nextInt();
            }
        }
        return  arr1;
    }

    //setting matrix 2
    int matrix_2[][]  = set_matrix_2();
    public int[][] set_matrix_2()
    {
        System.out.println("Enter m * n for the matrix 2");
        int m2 = sc.nextInt();
        int n2 = sc.nextInt();
        System.out.println("Enter the second matrix");
        int arr2[][] = new int[m2][n2];
        for(int i = 0; i<m2; i++)
        {
            for(int j = 0; j<n2; j++)
            {
                arr2[i][j] = sc.nextInt();
            }
        }
        return  arr2;
    }
    //define method for matrix addition
    public int[][] matrix_add()
    {
        int a1 = matrix_1.length;
        int b1 = matrix_1[0].length;
        int a2 = matrix_2.length;
        int b2 = matrix_2[0].length;
        int c[][] = new int[a2][b2];
        if(a1==a2 && b1==b2)
        {
            for(int i = 0; i < a1; i++ )
            {
                for (int j = 0; j< b1; j++)
                {
                    c[i][j] = matrix_1[i][j] + matrix_2[i][j];
                }
            }
        }
        else
        {
            System.out.println("The given two matrices are incompatible for addition");
        }
        return c;
    }

    //define method for subtraction
    //define method for matrix addition
    public int[][] matrix_sub()
    {
        int a1 = matrix_1.length;
        int b1 = matrix_1[0].length;
        int a2 = matrix_2.length;
        int b2 = matrix_2[0].length;
        int c[][] = new int[a2][b2];
        if(a1==a2 && b1==b2)
        {
            for(int i = 0; i < a1; i++ )
            {
                for (int j = 0; j< b1; j++)
                {
                    c[i][j] = matrix_1[i][j] - matrix_2[i][j];
                }
            }
        }
        else
        {
            System.out.println("The given two matrices are incompatible for subraction");
        }
        return c;
    }
    //method for matrix multiplication
    //define method for matrix addition
    public int[][] matrix_mult()
    {
        int a1 = matrix_1.length;
        int b1 = matrix_1[0].length;
        int a2 = matrix_2.length;
        int b2 = matrix_2[0].length;
        int c[][] = new int[a2][b2];
        if(b1==a2)
        {
            for(int i = 0; i < a1; i++ )
            {
                for (int j = 0; j< b1; j++)
                {
                    for(int k = 0; k<b1; k++)
                    {
                        c[i][j] = c[i][j]+(matrix_1[i][j] * matrix_2[i][j]);
                    }

                }
            }
        }
        else
        {
            System.out.println("The given two matrices are incompatible for multiplication");
        }
        return c;
    }

}

public class Main {
    static Scanner sc = new Scanner((System.in));

    public static void main(String[] args) throws InterruptedException {
        //create an object of the class
        matrices my_object = new matrices();
        int choice;
        System.out.println("Enter your option :");
        System.out.println("1.ADDITION");
        System.out.println("2.SUBTRACTION");
        System.out.println("3.MULTIPLICATION");
        System.out.println("4.EXIT");
        choice = sc.nextInt();
        switch (choice)
        {
            case 1: Thread add = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (my_object)
                    {
                        int a[][] = my_object.matrix_add();
                        int a1 = a.length;
                        int b1 = a[0].length;
                        System.out.println("\nYour result matrix is");
                        for(int i = 0; i < a1; i++ )
                        {
                            for (int j = 0; j< b1; j++)
                                System.out.println(a[i][j] + "\t");
                        }
                    }

                }
            });
            add.start();
            add.join();
                break;
            case 2: Thread sub = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (my_object)
                    {
                        int ab[][] = my_object.matrix_sub();
                        int a2 = ab.length;
                        int b2 = ab[0].length;
                        System.out.println("\nYour result matrix is");
                        for(int i = 0; i < a2; i++ )
                        {
                            for (int j = 0; b2 < j; j++)
                                System.out.println(ab[i][j] + "\t");
                        }
                    }

                }
            });
                sub.start();
                sub.join();
                break;
            case 3: Thread mult = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (my_object)
                    {
                        int mul[][] = my_object.matrix_mult();
                        int a2 = mul.length;
                        int b2 = mul[0].length;
                        System.out.println("\nYour result matrix is");
                        for(int i = 0; i < a2; i++ )
                        {
                            for (int j = 0; b2 < j; j++)
                                System.out.println(mul[i][j] + "\t");
                        }
                    }

                }
            });
                mult.start();
                mult.join();
                break;
            case 4:
                System.out.println("Program exited successfully");

                break;
            default:
                throw new IllegalStateException("Unexpected value: " + choice);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS