adding matrix
import java.util.Scanner;
public class MatrixAddition {
private static Scanner scanner = new Scanner(System.in);
private double [][] arr;
private int columns ;
private int raws ;
private static int numberOfMat = 1;
public void setRawAndColumns(int raws, int columns){
System.out.println("(Enter number of raws.)\n");
raws = scanner.nextInt();
System.out.println("(Enter number of columns).\n");
columns = scanner.nextInt();
SetMatrix(raws,columns);
}
private void SetMatrix(int r , int c){
this.raws = r;
this.columns = c;
this.arr = new double[raws][columns];
enterValues(this.raws,this.columns,this.arr);
double [][] nextMat = new double[this.raws][this.columns];
enterValues(this.raws,this.columns, nextMat);
calculateMatrix(this.arr,nextMat);
}
private double[][] enterValues(int r , int c, double[][]arr){
System.out.println("Enter Values for matrix #"+(this.numberOfMat));
++this.numberOfMat;
for(int i = 0 ; i < arr.length ; ++i ){
for(int j = 0; j < arr[0].length ; ++j ){
System.out.println("raw #"+(i+1)+" Column #"+(j+1));
arr[i][j]= scanner.nextDouble();
}
}
return arr;
}
private double[][] calculateMatrix(double[][] mat,double nextMat[][]){
double [] [] sum = new double[this.raws][this.columns];
for(int i = 0 ; i < mat.length ; ++i){
for(int j = 0 ; j < mat[0].length ; ++j){
sum[i][j]+= (mat[i][j]+nextMat[i][j]);
}
}
printmatrix(sum);
return null;
}
private void printmatrix(double [][] sum){
for(int i = 0 ; i < sum.length; ++i){
System.out.print("[ ");
for (int j = 0 ; j < sum[0].length ; ++j){
System.out.print(sum[i][j]+" ");
}
System.out.println("]");
}
}
}