August 09, 2012

How to print matrix in java

Matrix Manipulation in java


Description : To print sum of all values of matrix in java


 import java.util.Scanner;

/**
* +belazy 
*/
/**
* @author BlackBery
*
*/
public class MatrixDiagonal {
private int [][]matrix = null;
private Scanner sc = null;
public MatrixDiagonal() {
matrix = new int[3][3];
}
public static void main(String[] args) {
MatrixDiagonal md = new MatrixDiagonal();
md.getMatrixValues();
md.showMatrix();
md.computeValue();
}
private void computeValue() {
int sum = 0;
for(int row = 0; row < 3 ; row ++){
for(int col =0; col col){
sum = sum + matrix[row][col];
}
}
}
System.out.println(sum);
}
private void showMatrix() {
for(int row = 0; row < 3 ; row ++){
for(int col =0; col < 3; col ++ ){
System.out.print(matrix[row][col]+"\t");
}
System.out.println();
}
}
private void getMatrixValues() {
sc = new Scanner(System.in);
System.out.println(" Enter values ");
for(int row = 0; row < 3 ; row ++){
for(int col =0; col < 3; col ++ ){
matrix[row][col] = sc.nextInt();
}
}
}
}




Facebook comments