0

This is my first post here so forgive any formatting errors and the like.

So basically I'm having difficulty implementing a method that takes a given array and rotates it 90 degrees clockwise. We're supposed to implement all given methods in terms of an attribute "imp" like so:

class Matrix {
int[][] imp;
/* All methods in Matrix are implemented in terms of ’imp’ */
}

This is what I have so far in my method implementation:

/**
 * <p> Rotate the matrix by 90 degrees.
 * <p> The old 1st row becomes the new last column, 
 *     the old 2nd row becomes the new 2nd last column, and so on.
 * <p> Hint: attribute 'imp' should be reassigned to a new matrix. 
 * <p> See TestMatrix class for how this method can be used.
 */
public void rotateClockwise() {
    int rows = imp.length;
    int cols = imp[0].length;
    int[][] m2 = new int[cols][rows];
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            m2[i][rows - 1 - i] = imp[i][j];
        }
    }
    imp = m2;
}

This is the test case being used to test my method:

@Test
public void test18_RotateClockwise() {
    Matrix m1 = new Matrix(3, 4); 

    int[] nr0 = {1 , 2 , 3 , 4 };
    int[] nr1 = {5 , 6 , 7 , 8 };
    int[] nr2 = {9 , 10, 11, 12};
    m1.setRow(0, nr0);
    m1.setRow(1, nr1);
    m1.setRow(2, nr2);

    int[] nc0 = {9, 10, 11, 12};
    int[] nc1 = {5, 6 , 7 , 8 };
    int[] nc2 = {1, 2 , 3 , 4 }; 

    int[] nr0_ = {12, 11 , 10 , 9 };
    int[] nr1_ = {8 , 7  , 6  , 5 };
    int[] nr2_ = {4 , 3  , 2  , 1 }; 

    m1.rotateClockwise();
    assertArrayEquals(nc0, m1.getColumn(0));
    assertArrayEquals(nc1, m1.getColumn(1));
    assertArrayEquals(nc2, m1.getColumn(2));

    m1.rotateClockwise();
    assertArrayEquals(nr0_, m1.getRow(0));
    assertArrayEquals(nr1_, m1.getRow(1));
    assertArrayEquals(nr2_, m1.getRow(2));
}

I understand what the test case is trying to do, and my code compiles and doesn't have any exceptions or errors, but I'm getting the wrong output like so:

arrays first differed at element [0]; expected:<9> but was:<0>

I'm assuming this means that I'm basically rotating an empty array. I guess my question is how do I get the array from the test case into my method to rotate it? I tried using getRow and getColumn methods but I get an error telling me I can't call those methods on the attribute imp. Any help is appreciated. Thanks!

1
  • What does your step debugger tell you?. Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. Commented Sep 24, 2018 at 3:35

2 Answers 2

2

I think this line is incorrect, as the position into which the value is being written should definitely depend on j as well:

            m2[i][rows - 1 - i] = imp[i][j];

I haven't tested it but this could be a working version:

            m2[j][rows - 1 - i] = imp[i][j];

Edit: Why should j be the first index instead of i? The inner loop goes through the elements of a row inside the original matrix. These elements should be inserted into the same column of the new matrix, but in different rows. The index of the row into which the current element is inserted should increase as the column index of the element in the original matrix increases. j represents the column index in the original matrix, therefore it should represent the row index in the new/rotated matrix.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow this one simple change made the whole method work. Thank you! Could you explain why that term should be j and not i? I tried writing out the old and new arrays on a piece of paper to visualize what the variables should be, and to me it seemed like the first value would have to be i. Why is it j?
Edited my answer to include explanation
0

Do you have a proper constructor for Matrix class?

My hint for you is to check out matrix transpose implementations in java. It's very similar algorithm to yours.

For example: Transpose matrix on java67

2 Comments

Good link but please include some of the content from the link in your answer, and try to directly respond to the code in the question.
I do have a proper constructor; three, in fact. Here's what they are: link. Changing the first index value from "i" to "j" as per the other comment seems to have fixed my method. I tried turning the imp attribute into a matrix, but I kept getting errors regarding converting from int[][] (which is imp's type) to Matrix, which it said I couldn't do, so I gave up on that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.