Home > database >  Method that returns the location of a number placed in a separate method. In java
Method that returns the location of a number placed in a separate method. In java

Time:12-13

I'm trying to make a method that returns the row and column of a number that was placed in another method. The method that places the new number returns a 2D array, and I think that's a problem because the number placed is going to have the same value as several other numbers in that same 2D array.

so for example:

This will be the original board

1 2 2 1 3 2 
3 3 2 2 1 3
3 2 2 3 3 1
2 3 2 1 2 3
0 0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

After the method that places the new number is going to look like this (The user chooses only what column he wants to put the number, in this case, I chose column B)

1 2 2 1 3 2 
3 3 2 2 1 3
3 2 2 3 3 1
2 3 2 1 2 3
0 2 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 

Notice number 2 in column 2 and row 5 (Technically column 1 and row 4 in Java) That is the number that was placed. The method that I'm trying to create should return the row and column of that number. But there are several numbers that have the value of 2 as well so that's a problem I have.

The number that is placed is always going to be in the row below to the numbers 1 2 or 3 and above 0 and as I said, the column is going to be determined by the user.

This is the code I have so far for the method that puts the new number.

public static int[][] putNumber(int[][] board, String columnInput, int randomNumber) {
        if(columnInput.equals("A") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][0] == 0) {
                    board[row][0] = randomNumber;
                    break;
                }
            }
        }
        if(columnInput.equals("B") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][1] == 0) {
                    board[row][1] = randomNumber;
                    break;
                }
            }        
        }
        if(columnInput.equals("C") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][2] == 0) {
                    board[row][2] = randomNumber;
                    break;
                }
            }     
        }
        if(columnInput.equals("D") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][3] == 0) {
                    board[row][3] = randomNumber;
                    break;
                }
            }    
        }
        if(columnInput.equals("E") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][4] == 0) {
                    board[row][4] = randomNumber;
                    break;
                }
            }       
        }
        if(columnInput.equals("F") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][5] == 0) {
                    board[row][5] = randomNumber;
                    break;
                }
            }        
        }

        return board;
    }

And the code I tried to make is this one. Basically return a new int[] with 2 elements. then replace the first element with the row and the second element with the column. It does not compile but partially shows what I am trying to accomplish:

public static int[] returnLocation(int[][] board, String columnInput, int randomNumber) {
        int[] rowAndColumn = new int[2];
        
        if(columnInput.equals("A") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][0] == 0) {
                    board[row] = rowAndColumn[0];
                    board[0] = rowAndColumn[1];
                }
            }
        }
        if(columnInput.equals("B") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][1] == 0) {
                    board[row] = rowAndColumn[0];
                    board[1] = rowAndColumn[1];
                }
            }        
        }
        if(columnInput.equals("C") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][2] == 0) {
                    board[row] = rowAndColumn[0];
                    board[2] = rowAndColumn[1];
                }
            }     
        }
        if(columnInput.equals("D") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][3] == 0) {
                    board[row] = rowAndColumn[0];
                    board[3] = rowAndColumn[1];
                }
            }    
        }
        if(columnInput.equals("E") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][4] == 0) {
                    board[row] = rowAndColumn[0];
                    board[4] = rowAndColumn[1];
                }
            }       
        }
        if(columnInput.equals("F") ) {
            for(int row = 0; row < board.length; row  ) {
                if(board[row][5] == 0) {
                    board[row] = rowAndColumn[0];
                    board[5] = rowAndColumn[1];
                }
            }        
        }

        return rowAndColumn;
    }

What can I do?

CodePudding user response:

Have you considered creating a class such as "MatrixAndCoordinates" that will hold both the result matrix and the column and row of the number that was placed, an instance of which will be returned by the putNumber method instead?

In this way you can discard the returnLocation method altogether

CodePudding user response:

Is it necessary for putNumber to return the board? I don't know, but it might not be. This answer will show why it might not be necessary, and then assume it isn't necessary.

Consider, in Java, an array is a reference type.

Consider the following:

  int [][] board1 = new int [7][6];
  int [][] board2;
  // some code to put value in elements of board1
  // display the contents of board1        
  board2 = putNumber(board1, "D", rand);
  // display the contents of board1
  board1 [3][4] = 333;
  // display the contents of board2

Go ahead and try that modification. It can be deleted later. Where there is // display ..., substitute code preferred code to display a 2D array. It could be something like this:

  System.out.println (Arrays.deepToString(board1));

So, the upshot is, if your code for putNumber does not return the board after modifying it, you are still going to have access to the modified board.

That allows 'putNumber' to still do its work, and return something else. It could return the coordinates of the location that was changed instead:

  public static int [] putNumber (int [][] board, String column, int randomNumber) { 
     int columnIndex = column.charAt (0) - 'A';
     int saveRow = -1;
     for (int row = 0; row < board.length; row  ) {
        if (board [row][columnIndex] == 0) { 
            board [row][columnIndex] = randomNumber;
            saveRow = row;
            break;
        }
     }
     return new int [] { saveRow, columnIndex };
  }

What's going on here?

Suppose you have a class Foo and code that uses instances of Foo:

  Foo bar, bat;
  bar = bat;

Objects in Java are reference types. With reference types, you can have aliases. In this example, bar and bat are aliases for the same Object. Until and as long as neither is assigned to a different reference, what happens to one happens to the other.

In Java, an array is a reference type. Consider the following line:

 public static int[][] putNumber(int[][] board, String columnInput, int randomNumber)

Within the method putNumber, board is a local variable that is an alias for the corresponding argument in the code that calls putNumber. The code in putNumber doesn't reassign any references. So, when the code alters the value of a primitive in the board array, the corresponding primitive is changed in the calling code.

The same thing happens in the earlier code example: board1 and board2 are aliases of each other.

Off-topic:

Part of your code is duplicated several times, with only a minor difference in copies. When you see that, think of ways to eliminate the duplication.

To reduce the duplication, I chose to convert the first character of columnInput to an int within the code putNumber. An alternative, perhaps better, would be to do that conversion before calling putNumber. Then, instead of a String columnInput parameter, there could be an int columnInput parameter.

One alternative to int columnIndex = column.charAt (0) - 'A'; is int columnIndex = "ABCDEF".indexOf (columnInput.charAt(0));

  • Related