JAVA POO - How to call a function from a Class

Asked

Viewed 508 times

1

So I have the following problem , I created a 30x60 matrix in which I will generate a number in a random position, so far so good. But when I took the function that generates the number and put it in the class, I’m having trouble calling it.

My code is like this: I omitted the matrix fill code to make it smaller

int [][] matrix = new int[30][60];
Caminhao caminhoes = new Caminhao();
Carro carros = new Carro();
Moto motos = new Moto();



//Utilizando as funções com objetos

carros.gerarCarro();
motos.gerarMoto();
caminhoes.gerarCaminhao();


//Código para printar a matriz após cada veiculo adicionado

    for(int x = 0; x < matrix.length; x++)
{
    for (int i = 0; i < matrix[x].length; i++) 
    {
        System.out.print(matrix[x][i]);
    }
    System.out.println();
}

And the code in the Class that the function is like this:

/Método para gerar um Carro em um lugar aleatório da matriz
public void gerarCarro(){
          Random r= new Random();
          int [][] matrix = new int[30][60];
          int a =r.nextInt(29);
    int b =r.nextInt(59);
          if(matrix[a][b] == 0 && matrix[a][b] != 2 && matrix[a][b]!=1 && matrix[a][b]!=4 && matrix[a][b]!=5){
            matrix[a][b]=3;

    }

    }
}

What is wrong ?

  • But what is the purpose of what you are trying to do ? that method gerarCarro fill in the matrix you have in main ? Is that you are creating a new one within the method gerarCarro.

  • The generated method has to generate the number 3 at a random position of the matrix that has the value of 0.

1 answer

0


When does int [][] matrix = new int[30][60]; within the method gerarCarro is creating a new matrix and not using the one in the main:

public void gerarCarro(){
    Random r= new Random();
    int [][] matrix = new int[30][60]; // <-- aqui

That is why your next amendment:

matrix[a][b]=3;

In no way affects the matrix that already had in the main. To do what you want you have some alternatives. The most direct and common is to pass this matrix as a parameter to the method.

To do this, start by changing the method to work with the received matrix:

public void gerarCarro(int[][] matrix){
// -------------------------------^ matriz recebida aqui
    Random r= new Random();
    //int [][] matrix = new int[30][60]; //já não cria
    int a =r.nextInt(29);
    int b =r.nextInt(59);
    if(matrix[a][b] == 0){
        matrix[a][b]=3;
    }
}

Right now you might be thinking, What happened to the rest of the conditions if ???

Well they didn’t make sense because if matrix[a][b] for 0 then guaranteed to be different from 1, 2, 4 and 5 making them redundant.

In the main alters the gerarCarro to send the matrix.

//Utilizando as funções com objetos

carros.gerarCarro(matrix);

And to make everything right, you have to apply the same principle to the other methods you generate, assuming you had an identical logic to them.

There would still be other things to improve, such as not creating the object Random every time you enter the method, but these are already steps to the future.

  • Thank you very much, gave it right here , Thank you really.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.