
class Matrix {

    private int a[][];

    public Matrix(int n, int m) {
        a = new int[n][m];
    }

    public Matrix(int a[][]) {
        this.a = a;
    }

    public int getVerticalSize() {
        return a.length;
    }

    public int getHorizontalSize() {
        return a[0].length;
    }

    public int getElement(int i, int j) {
        return a[i][j];
    }

    public void setElement(int i, int j, int value) {
        a[i][j] = value;
    }

    @Override
    public String toString() {
        String s = "\n";
        for (int[] vector : a) {
            for (int value : vector)
                s += value + " ";
            s += "\n";
        }
        return s;
    }

    public static Matrix add(Matrix a, Matrix b) {
        int v = a.getVerticalSize();
        int h = a.getHorizontalSize();
        Matrix result = new Matrix(v, h);

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < v; j++) {
                int value = 0;
                value = a.getElement(i, j) + b.getElement(i, j);
                result.setElement(i, j, value);
            }
        }
        return result;
    }

    public static Matrix subtract(Matrix a, Matrix b) {
        int v = a.getVerticalSize();
        int h = a.getHorizontalSize();
        Matrix result = new Matrix(v, h);

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < v; j++) {
                int value = 0;
                value = a.getElement(i, j) - b.getElement(i, j);
                result.setElement(i, j, value);
            }
        }
        return result;
    }

    public static Matrix multiply(Matrix a, Matrix b) {
        int v = a.getVerticalSize();
        int h = b.getHorizontalSize();
        int temp = a.getHorizontalSize();

        Matrix result = new Matrix(v, h);

        for (int i = 0; i < v; i++)
            for (int j = 0; j < h; j++) {
                int value = 0;
                for (int k = 0; k < temp; k++) {
                    value += a.getElement(i, k) * b.getElement(k, j);
                }
                result.setElement(i, j, value);
            }
        return result;
    }

    public void transMatrix() {
        int x = a.length;
        int retA[][] = new int[x][x];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < x; j++) {
                retA[j][i] = a[i][j];
            }
        }
        for (int i = 0; i < a.length; i++) {
            System.arraycopy(retA[i], 0, a[i], 0, a.length);
        }
    }

    public static void main(String[] args) {

        int[][] a = {{-9, 1, 0}, {4, 1, 1}, {-2, 2, -1}};
        int[][] b = {{0, 0, 0}, {0, 2, 0}, {0, 0, 1}};

        Matrix A = new Matrix(a);
        System.out.println("A" + A);
        System.out.println("Element [0,0] : " + A.getElement(0, 0));

        Matrix B = new Matrix(b);
        B.setElement(0, 0, 1);
        System.out.println("Size of matrix B : " + B.getHorizontalSize() + " " + B.getVerticalSize() + B);

        Matrix C;
        C = add(A, B);
        System.out.println("add " + C);

        C = subtract(A, B);
        System.out.println("subtract " + C);

        C = multiply(A, B);
        System.out.println("multiply " + C);

        A.transMatrix();
        System.out.println("Transposed A : " + A);

    }
}
