
i need to make a function in java where i will do addition of two matrices then i need to call this function through an object.is it possible to do,i tried everyway but couldnt get the result.

import java.util.*;
class MatrixAddition{
public static void matrixSum(int A[][],int B[][]){
int[][] C = new int[3][3];
System.out.println("Matrix A: ");
for (int i=0 ; i < A.length ; i++){
System.out.println();
for (int j=0 ; j < A[i].length ; j++){
System.out.print(A[i][j]+" ");
}
}
System.out.println();
System.out.println();
System.out.println("Matrix B: ");
for (int i=0 ; i < B.length ; i++){
System.out.println();
for (int j=0 ; j < B[i].length ; j++){
System.out.print(B[i][j]+" ");
}
}
System.out.println();
System.out.println();
System.out.println("Addition Of Matrices: ");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
C[i][j]=A[i][j]+B[i][j];
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[][] A = new int[3][3];
int[][] B = new int[3][3];
System.out.println("Enter elements for matrix A : ");
for (int i=0 ; i < A.length ; i++)
for (int j=0 ; j < A[i].length ; j++){
A[i][j] = input.nextInt();
}
System.out.println("Enter elements for matrix B : ");
for (int i=0 ; i < B.length ; i++)
for (int j=0 ; j < B[i].length ; j++){
B[i][j] = input.nextInt();
}
MatrixAddition mx=new MatrixAddition();
mx.matrixSum(A,B);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.