How to receive by parameter an array of objects of another class in Java?

Asked

Viewed 903 times

1

Hello people my question is that I am trying to receive by parameter an array of objects of another class,for the class I want to put the array by parameter.

Classes are : Route and Costs

I have to receive by parameter an array of objects of the type Route, to be able to calculate. Here is the code :

Class Route :

public class Percurso {

    private double kmPercorrida;
    private double valorCombustivel;
    private double valorPedagio;

    public Percurso() {
        this(0,0,0);
    }

    public Percurso(double kmPercorrida, double valorCombustivel,
            double valorPedagio) {

        this.kmPercorrida = kmPercorrida;
        this.valorCombustivel = valorCombustivel;
        this.valorPedagio = valorPedagio;
    }

Class Costs :

public class Custos {

    public String calcularViagem(Percurso [] p ) {



        return "";


    }

}

In part Route [] p,need to pass the array of the Path object, kmPercorrida,valorCombustivel,valorPedagio.

OBS : In Return I only left so that no problems appear,.

How can I do that? If anyone knows please help me. And excuse me if the format of the post is wrong,I’m new here on the forum. I’ve also read how to make a decent post,but still any mistake,.

  • I did not understand what your doubt, it seems to be all right, although in a real code it would be a mistake to have monetary value as double.

  • My question is that I want through this array to put the variables of the other class (Path) to be able to return later. 'Cause that’s what exercise calls for.

2 answers

1

To declare an array in Java would be Percurso[] p, and not Percurso p[].

  • Thank you friend for correcting me, but even so I’m still doubtful how to receive through the parameter the variables of the other class,.

  • I didn’t understand your question, you want to know how to access the variables of the objects of this array?

  • Yes exactly, I want to put the variables of the other class in this array.

1


public class Custos {

  public String calcularViagem(Percurso[] percursos) {
  return "";
  }

}
  • Thanks also for the help.

Browser other questions tagged

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