1

I am supposed to write a method that accepts 3 2-D arrays of This method should determine whether one of the matrices is the result of matrix addition of the other two.

public class Matrix {
    public static void main(String[]args){
        int [][] a = {{5,2,3},{4,1,6},{0,7,2}};
        int [][] b = {{1,2,3},{4,5,6},{0,1,2}};
        int [][] t = {{6,4,6},{8,6,12},{0,8,4}};
        System.out.println(add(a,b));
        System.out.println(check(a,b,t));
    }

    public static int [][] add(int[][]a,int[][]b){
        int i=0;
        int j=0;
        int[][] r = new int [3][3];
        while (i<a.length){
            r[i][j] = a[i][j] + b[i][j];
                i++;
                j++;
        }
        return r;
    }
    public static boolean check(int[][]a,int[][]b,int[][]t){
        int i = 0;
        int j = 0;
        while(i<t.length){
            if(t==add(a,b))
                return true;
        }
        return false;
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Zeyad
  • 13
  • 4
  • The printing statement prints a reference rather than the return types of both methods. So why is this? TIA. – Zeyad Jun 03 '15 at 17:26
  • possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Dan Getz Jun 03 '15 at 17:30
  • Are you sure you read that other question? It explains that when you print an array object in Java directly, you get something like (their example) `[I@3343c8b3`. If that's not what your problem is, then please clarify. – Dan Getz Jun 03 '15 at 17:34
  • Trivial edit: "... accepts 3 2-D arrays of *type `int`*. This method..." – OhBeWise Jun 03 '15 at 17:37
  • Oh, I guess you've got 2 different questions in there, and my proposed duplicate only answers one of them. Give me a moment, I'll try to find a duplicate for the other. – Dan Getz Jun 03 '15 at 17:39
  • [This question](http://stackoverflow.com/q/8051084/3004881) is a duplicate of your second question (by that I mean `check()`, with the printing of the result of `add()` being your "first question") – Dan Getz Jun 03 '15 at 17:43
  • It's my problem. My printing is rather different. I am invoking a method in the print statement. and the Arrays.deepToString results in an error : can't find symbol – Zeyad Jun 03 '15 at 17:44

1 Answers1

1

add returns an array. Arrays in Java are objects, but they do not override the toString() method. When printing, you'd print their default toString() call, which is implemented by Object as return getClass().getName() + "@" + Integer.toHexString(hashCode());.

Luckily, Java provides a utility in the form of java.util.Arrays.deepToString(Ojbect[]) to generate a more readable string output:

System.out.println(Arrays.deepToString(add(a,b)));

EDIT:
Your add method is also wrong. Your code iterates i and j together, so it only sums the elements along the matrix's diagonal instead of adding all of them. You should use a nested loop instead:

public static int [][] add(int[][]a, int[][]b) {
    int[][] r = new int [3][3];
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            r[i][j] = a[i][j] + b[i][j];
        }
    }
    return r;
}

Your check method, by the way, is also wrong - it attempts to compare the array itself instead of is elements:

public static boolean check(int[][]a, int[][]b, int[][]t) {
    int[][] r = add(a, b);
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            if (r[i][j] != t[i][j]) {
                return false;
            }
        }
    }

    return true;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350