How to call a method that belongs to another class? JAVA POO

2

Hi, I'm doing a program on the weight of people and I have to do several methods one of them is: averageWe're in the class Statistics of People but my problem is that I do not know how to call a variable or the method getPeso () which is of the Person class to calculate it, because if I put only getPeso () I get error in eclipse and it tells me that I must create that method within the class Statistics of People but I want to call it from the Person class because as I did it there , so that I am going to create again the same one in another class ... for now it arrives until here:

public class Persona {

private String nombre ;
private int edad;
private double peso;
private String genero ;
private double altura;

public Persona(String nombre , int edad , double peso , String genero , double altura) {

    this.nombre = nombre ;
    this.edad = edad ;
    this.peso = peso;
    this.genero = genero;
    this.altura = altura;

}

public String toString() {
    return "EstadisticasDePersonas [nombre=" + nombre + ", edad=" + edad + ", peso=" + peso + ", genero=" + genero
            + ", altura=" + altura + "]";
}



public String getNombre () {

    return this.nombre;
}

public void setNombre (String nombre) {

    this.nombre = nombre;
}

public int getEdad () {

    return this.edad;
}

public void setEdad(int edad) {

    this.edad = edad;
}

public double getPeso() {

    return this.peso ;
}

public void setPeso() {

    this.peso = peso;
}

public String getGenero() {

    return this.genero;
}

public void setGenero (String genero) {

    this.genero = genero;
}

public double getAltura() {

    return this.altura;
}

public void setAltura() {

    this.altura = altura;
}

}

public class EstadisticasDePersonas {

private String nombre ;


public EstadisticasDePersonas (String nombre) {
    this.nombre =nombre;

}


public double pesoPromedio() {

    int contadorPeso=0;
    int sumatoriaPeso =0;
    double pesoPromedio=0.0;

    if (getPeso()>0) {

        contadorPeso++; 
    }

    sumatoriaPeso+=contadorPeso;
    pesoPromedio= sumatoriaPeso/50;

    return pesoPromedio;

}
    
asked by computer96 07.12.2018 в 04:50
source

2 answers

3

A regular method is not called in the class Person, you call it on an object of the person class, technically speaking, on an instance of the class 1 .

This, if you think about it, makes a lot of sense. The class is like the mold with which a person is represented and constructed . But there is no weight of all people, but each person has its own weight.

Then, if you need the weight of a particular person, you must first have built an object-an instance-of that class, and then you can establish and obtain its weight.

Let's make a small example:

{
  //declaramos una _variable_ para una persona
  Persona juan;
  //ahora, creamos el objeto y lo inicializamos...
  juan = new Persona();
  juan.setNombre("Juan Antonio");
  juan.setEdad(43);
  juan.setPeso(125.12);
  //.... hacemos otras cosas
  //y ahora, recuperamos el peso
  double peso;
  peso = juan.getPeso();
}

Now, assuming you have this class EstadisticasDePersonas , I am not very clear about its use or its purpose, but it could be that this class has a list of people who are adding and then we can get the weight of any of these people. Here I will assume, that internally the class has a list or an arrangement of people and that it has a agregarPersona(Persona persona) method that is adding people to this arrangement or list, for example, I can do something like this:

{
  Persona juan;
  Persona mario;
  EstadisticaPersonas estadistica;
  estadistica = new EstadisticaPersonas();

  juan = new Persona();
  juan.setNombre("Juan Antonio");
  juan.setEdad(43);
  juan.setPeso(125.12);
  estadistica.agregarPersona(juan);

  mario = new Persona();
  mario.setNombre("Mario Alberto");
  mario.setEdad(44);
  mario.setPeso(132.15);
  estadistica.agregarPersona(mario);
  }

Then, within this class, you could implement the getPesoPedia method, using a foreach doing something like:

public double getPesoPromedio() {

    int cantidadPersonas = 0;
    double sumatoriaPeso = 0;

    for (Persona persona : listaPersonas) {
      cantidadPersonas++;
      sumatoriaPeso += persona.getPeso();
    }

    return sumatoriaPeso / cantidadPersonas;
}

Of course, here we are in the field of assumptions and I will stop ... I hope that the information provided helps clarify your doubts.

1 : I have said that it is usually called on an object, but java (and many other object-oriented languages) if they allow to call a method on a class, but this is a special type , or less common, method, which is called static method or class method . This method is called about the class, but as such, it is in a different context and does not have access to the members or regular methods of the class, which always require an instance to be accessed / invoked.

    
answered by 07.12.2018 в 05:12
0

So you can access the methods and properties that exist in the .java file but from the .java file; you should consider the following

  • Both must be within the same package
  • You need to perform the instantiation process on the .java.B file to give an object access to the methods of the .java file
  • Consider that in Persona.java you have a constructor that initializes a series of properties when the class is created; then at the moment of realization, you need to assign the corresponding values for each property passed to the constructor
  • Note that I put both files in the same package called stackover , so from one file when trying to access the values of another, it will be done as long as they are inside the same one mentioned
  • Persona.java

    package stackover;
    
    public class Persona {
    
    private String nombre ;
    private int edad;
    private double peso;
    private String genero ;
    private double altura;
    
    public Persona(String nombre , int edad , double peso , String genero , double altura) {
    
        this.nombre = nombre ;
        this.edad = edad ;
        this.peso = peso;
        this.genero = genero;
        this.altura = altura;
    
    }
    
    public String toString() {
        return "EstadisticasDePersonas [nombre=" + nombre + ", edad=" + edad + ", peso=" + peso + ", genero=" + genero
                + ", altura=" + altura + "]";
    }
    
    
    
    public String getNombre () {
    
        return this.nombre;
    }
    
    public void setNombre (String nombre) {
    
        this.nombre = nombre;
    }
    
    public int getEdad () {
    
        return this.edad;
    }
    
    public void setEdad(int edad) {
    
        this.edad = edad;
    }
    
    public double getPeso() {
    
        return this.peso ;
    }
    
    public void setPeso() {
    
        this.peso = peso;
    }
    
    public String getGenero() {
    
        return this.genero;
    }
    
    public void setGenero (String genero) {
    
        this.genero = genero;
    }
    
    public double getAltura() {
    
        return this.altura;
    }
    
    public void setAltura() {
    
        this.altura = altura;
    }
    }
    

    Person Statistics.java

    package stackover;
    
    public class EstadisticasDePersonas
    {
        public static void main(String args[])
        {
            Persona personita = new Persona("Alfredo", 23, 23.2, "masculino", 12.3);
            personita.setGenero("Femenino");
            personita.getGenero();
    
        }
    }
    
        
    answered by 07.12.2018 в 05:15