Blog

Two dimensional Array in Java

Two Dimensional Array in Java

Two dimensional Array in Java | Two dimensional Array | Two dimensional Array in Java| Two dimensional Array in Java

Two dimensional Array in Java | Create a class with static methods to calculate the sum, difference and product of two matrices (represented by 2D arrays). The methods must return the resulting matrices.

Program of Two dimensional Array in Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignments;
import java.util.*;
/**
 *
 * @author Achyut1324
 */
public class Assignment8 {
    public static void main(String[] args)
    {
        int [][] firstMatrix= new int[3][3];
        int [][] secondMatrix = new int[3][3];
        int [][] result = new int [3][3];

        Scanner in = new Scanner(System.in);
        //Taking input for 1st Matrix
        System.out.println("Enter values of first 3 by 3 matrix");
        for (int i =0 ; i<3; i++)
        {
            for (int j =0; j<3; j++)
            {
                firstMatrix[i][j] = in.nextInt();
            }
        }

        //Taking input for second Matrix
        System.out.println("Enter Values of second 3 by 3 matrix");
        for (int i =0 ; i<3; i++)
        {
            for (int j =0; j<3; j++)
            {
                secondMatrix[i][j] = in.nextInt();
            }
        }

        // Calling calculateSum, calculateDifference and calculateMultiply function.
         result = calculateSum(firstMatrix,secondMatrix);
         System.out.println("The result of Matrix addition is:");
         displayMatrix(result);

         result = calculateDifference(firstMatrix,secondMatrix);
         System.out.println("The result of difference between two matrices is:");
         displayMatrix(result);

         result=calculateProduct(firstMatrix,secondMatrix);
         System.out.println("The result of Matrix Multiplication is:");
         displayMatrix(result);
    }

    //calculates the sum between two matrices
    public static int[][] calculateSum(int [][]firstMatrix, int [][]secondMatrix)
    {
        int [][] sum = new int[3][3];

        for(int i = 0 ; i<3; i++)
        {
            for(int j =0; j<3; j++)
            {
                sum[i][j] = firstMatrix[i][j]+ secondMatrix[i][j];
            }
        }
        return sum;
    }

    //calculates the difference between two matrices
    public static int[][] calculateDifference(int [][]firstMatrix,int [][]secondMatrix)
    {
        int [][] difference = new int[3][3];

        for(int i = 0 ; i<3; i++)
        {
            for(int j =0; j<3; j++)
            {
                difference[i][j] = firstMatrix[i][j]-secondMatrix[i][j];
            }
        }
        return difference;
    }

    //calculates the product between two matrices
    public static int[][] calculateProduct(int[][] firstMatrix, int[][]secondMatrix)
    {
        int mul[][]=new int[3][3];

        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                mul[i][j]=0;
                for(int k=0;k<3;k++)
                {
                    mul[i][j]+=firstMatrix[i][k]*secondMatrix[k][j];
                }
            }
        }
        return mul;
    }

    //Display the result in a  Matrix form
    public static void displayMatrix(int [][] result)
    {
        for(int i = 0; i<3; i++)
        {
            for (int j = 0; j<3; j++)
            {
                System.out.print(result[i][j]+" ");
            }
            System.out.println();
        }
    }
}

Output of the program

run:
Enter values of first 3 by 3 matrix
2
3
4
5
6
7
8
9
1
Enter Values of second 3 by 3 matrix
5
4
3
2
1
9
8
7
6
The result of Matrix addition is:
7 7 7
7 7 16
16 16 7
The result of difference between two matrices is:
-3 -1 1
3 5 -2
0 2 -5
The result of Matrix Multiplication is:
48 39 57
93 75 111
66 48 111
BUILD SUCCESSFUL (total time: 16 seconds)