简体   繁体   中英

Composition of void method used in void method? (Java)

I started learning Java about 2 weeks ago, so please, don't be judgy. I'm doing this program with a two-dimensional array (a picture), which I want to rotate on 90 degrees (already done, tested, it works) and 180. My methods are void and I want to use the 90 degrees one twice (composed?) in the 180 degrees one, but it doesn't work.

This is my 90 method:

public void rotate90(){
        for (int r = 0; r < w; r++) {
             for (int c = 0; c < h; c++) {
                 imageMatrix[c][w-r-1] = imageMatrix[r][c];
             }
        }

public void rotate180(){ 
        rotate90(rotate90()); // my idea was to rotate again the already rotated matrix, but since rotate90 is void it doesn't work
}

Is there a way I can do this? With void functions?

Thanks in advance!

The method rotate90() has no parameter for that. Actually this is not the correct way.

The first way is to write it out.

rotate90();
rotate90();

Or use the for-cycle

for (int i=0; i<2; i++) {
    rotate90();
}

However here is a way to rotate it how many times you want with just a one method:

public void rotate90(int n) {
    for (int i=0; i<n; i++) {
        for (int r=0; r<w; r++) {
            for (int c=0; c<h; c++) {
                imageMatrix[c][w-r-1] = imageMatrix[r][c];
            }
        }
    }

And then the rotate180() method:

public void rotate180(){ 
    rotate90(2); // rotate by 90 two times
}

You just need to call the method twice. What you can't do is call rotate90() with the return value of rotate90 which is what your proposed code is doing, since the method doesn't take a parameter or return a value.

Your rotate90() is working directly on a global variable, so your rotate180() would too.

public void rotate180(){ 
    rotate90();
    rotate90();
}

However, I would suggest you to use some params and return values, only use global variables if strictly necesary. Also, I am not sure if your algorithm is correct, I would do it like this.

public static int[][] rotate90(int[][] matrix){
    int [][] newMatrix = new int[matrix[0].length][matrix.lenght];

    for (int r = 0; r < w; r++) {
         for (int c = 0; c < h; c++) {
             newMatrix[c][w-r-1] = matrix[r][c];
         }
    }
    return newMatrix;
}

public static int[][] rotate180(){ 
    return rotate90(rotate90()); 
}

There is no need to set them as static but since they don't need an object to work, you can move them to a Utils class or something.

If you want to call it only once, you can pass it as a parameter

public void rotate90nTimes(int n){
    for (int times = 0; times < n; times++) {
        for (int r = 0; r < w; r++) {
             for (int c = 0; c < h; c++) {
                 imageMatrix[c][w-r-1] = imageMatrix[r][c];
             }
        }
    }
}

ps: If you do want to use it as rotate90(rotate90) you need to return the matrix and not making the funcion as void.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM