Fill an array with objects from other classes

0

I need a method of name "submitBanda" that will receive a Band type object with 5 musicians and print a message. How can I fill an array with objects from other classes, and use it as a method?

My code is the following.

package presentarbanda;

public class PresentarBanda {

    public static void main(String[] args) {

        //AQUI IRIA EL METODO    
    }

Estas son mis clases.

    package presentarbanda;

/**
 *
 * @author ricar
 */
public class Instrumento {

    //ATRIBUTOS
    private String NombreInstrumento;
    private String TipoInstrumento;

    //CONSTRUCTOR
    public Instrumento (String NombreInstrumento, String TipoInstrumento)
    {
        this.NombreInstrumento = NombreInstrumento;
        this.TipoInstrumento = TipoInstrumento;
    }

    public String getNombreInstrumento()
    {
        return NombreInstrumento;
    }

    public void setNombreInstrumento (String NombreInstrumento)
    {
        this.NombreInstrumento = NombreInstrumento;
    }

    public String getTipoInstrumento()
    {
        return TipoInstrumento;
    }

    public void setTipoInstrumento(String TipoInstrumento)
    {
        this.TipoInstrumento = TipoInstrumento;
    }


}

    package presentarbanda;

/**
 *
 * @author ricar
 */
public class Musico {

    //ATRIBUTOS
    private String NombreMusico;
    private String PosicionMusico;
    private Instrumento instrumento;

    //CONSTRUCTOR
    public Musico (String NombreMusico, String PosicionMusico, Instrumento instrumento)
    {
        this.NombreMusico = NombreMusico;
        this.PosicionMusico = PosicionMusico;
        this.instrumento = instrumento;
    }

    public String getNombreMusico ()
    {
        return NombreMusico;
    }

    public void setNombreMusico(String NombreMusico)
    {
        this.NombreMusico = NombreMusico;
    }

    public String getPosicionMusico()
    {
        return PosicionMusico;
    }

    public void setPosiconMusico(String PosiconMusico)
    {
        this.PosicionMusico = PosicionMusico;
    }

    public Instrumento getInstrumento ()
    {
        return instrumento;
    }

    public void setInstrumento (Instrumento instrumento){
        this.instrumento = instrumento;
    }


}

    package presentarbanda;

/**
 *
 * @author ricar
 */
public class Banda {

    Musico musico[] = new Musico[5];

}
    
asked by Erick 01.08.2018 в 23:19
source

2 answers

2

It can also be done by creating the band class:

public class Banda {

    private Musico[] band = new Musico[5];

    public Banda(Musico[] musicos) {
        for (int i = 0; i < 5; i++) {
            band[i] = musicos[i];
        }
    }

    public Musico[] getBand() {
        return band;

    }
    public void setBand(Musico[] band) {
        this.band = band;
    }


}

And then in PresentarBanda show its members:

public class PresentarBanda {

    public static void main(String[] args) {
        Instrumento i = new Instrumento("trompeta", "viento");
        Instrumento i2 = new Instrumento("Guitarra", "Cuerdas");
        Musico m = new Musico("erick", "jefe", i);
        Musico m2 = new Musico("erick2", "jefe", i2);
        Musico m3 = new Musico("erick3", "jefe", i);
        Musico m4 = new Musico("erick4", "jefe", i2);
        Musico m5 = new Musico("erick5", "jefe", i);

        Banda banda = new Banda(new Musico[] {m,m2,m3,m4,m5})  ;

        presentarBanda(banda);
    }

    private static void presentarBanda(Banda banda) {
        Musico[] musicos = banda.getBand();
        for (int i = 0; i < musicos.length; i++) {
            System.out.println("Hola soy "+ musicos[i].getNombreMusico() + " y toco: "+  musicos[i].getInstrumento().getNombreInstrumento());

        }
    }

}

In the given example, the output is:

Hola soy erick y toco: trompeta
Hola soy erick2 y toco: Guitarra
Hola soy erick3 y toco: trompeta
Hola soy erick4 y toco: Guitarra
Hola soy erick5 y toco: trompeta
    
answered by 02.08.2018 / 09:34
source
0

One of the simplest forms maybe and this is, without the need to create the Band class, in the same class of your main you could create your method or good is at your discretion anyway, but the idea would be the following:

public static void main(String args[]){
       Instrumento i = new Instrumento("trompeta", "viento");
       Musico m = new Musico("erick", "jefe", i);

       presentarBanda(m, i);  
}

public static void presentarBanda(Musico musico, Instrumento instrumento){
            musico.setInstrumento(instrumento);
            List<Musico> listaMusicos = new ArrayList<>();
            listaMusicos.add(musico);            

            //Imprimes a tus musicos
            for(Musico m: listaMusico){
                System.out.println(m.getNombreMusico() + " "
                          + m.getInstrumento().getNombreInstrumento());
            }
    }

Greetings !!

    
answered by 02.08.2018 в 00:02