> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud4-solucions/polimorfismo-e-coleccions.md).

# Polimorfismo e coleccións

1\.

```java
public class Banda {
    private String nome;
    private Instrumento[] instrumentos;
    
    public Banda(String nome) {
        this.nome = nome;
        this.instrumentos = new Instrumento[4];
    }
    
    public void engadirInstrumento(Instrumento instrumento) {
        int pos = -1;
        for (int i = 0; i< this.instrumentos.length; i++){
            if (instrumentos[i]==null){
                pos = i;
                break;
            }        
        }
        if (pos == -1 ) {
            throw new Exception("A banda xa está completa. Non hai máis espazo para instrumentos.");
        }
        instrumentos[pos] = instrumento;
        System.out.println("Instrumento engadido á banda '" + nome + "'");
    }
    
    public void afinarInstrumentos() {
        System.out.println("=== Afinando instrumentos da banda '" + nome + "' ===");
        for (int i = 0; i < this.instrumentos.length; i++) {
            if (instrumentos[i] != null) {
                instrumentos[i].afinar();
            }
        }
    }
    
    public void tocar() {
        System.out.println("=== A banda '" + nome + "' está a tocar ===");
        for (int i = 0; i < this.instrumentos.length; i++) {
            if (instrumentos[i] != null) {
                instrumentos[i].tocar();
            }
        }
    }
    
    public String getNome() {
        return nome;
    }
    
    public int getNumeroInstrumentos() {
        return contador;
    }
    
    public boolean estaCompleta() {
        return contador >= instrumentos.length;
    }
}
```

```java
package Instrumentos;

public class App {
    public static void main(String[] args) {
        Banda orquesta = new Banda("Panorama");
        try {
            orquesta.engadirInstrumento(new Instrumento());
            orquesta.engadirInstrumento(new Guitarra());
            orquesta.engadirInstrumento(new Violin());
            orquesta.engadirInstrumento(new Piano());
        } catch (Exception e){
            System.out.println("Error engadindo instrumentos: " + e.getMessage());
        }
        orquesta.afinarInstrumentos();
        orquesta.tocar();

    }
}
```

2. Empresa

```java
public class Empresa {
    private String cif;
    private String direccion;
    private Empregado[] empregados;

    public Empresa(String cif, String direccion) {
        this.cif = cif;
        this.direccion = direccion;
        this.empregados = new Empregado[4];
    }

    public void engadirEmpregado(Empregado empregado) throws Exception {
        int pos = -1;
        for (int i = 0; i < this.empregados.length; i++) {
            if (empregados[i] == null) {
                pos = i;
                break;
            }
        }
        if (pos == -1) {
            throw new Exception("A empresa xa está completa. Non hai máis espazo para empregados.");
        }
        empregados[pos] = empregado;
        System.out.println("Empregado '" + empregado.nome + "' engadido á empresa '" + cif + "'");
    }

    public void presentarEmpregados() {
        System.out.println("=== Presentando empregados da empresa '" + cif + "' ===");
        for (int i = 0; i < this.empregados.length; i++) {
            if (empregados[i] != null) {
                System.out.println(empregados[i].toString());
                empregados[i].mostrarFuncion();
                System.out.println("Salario: " + empregados[i].calcularSalario() + "€");
                System.out.println("------------------------");
            }
        }
    }

    public String getCif() {
        return cif;
    }

    public String getDireccion() {
        return direccion;
    }
}
```

```java
public class App{
    
    public static void main(String[] args){
        
        Empresa miEmpresa = new Empresa("A12345678", "Rúa do Progreso, 123 - Lugo");
        
        Empregado empregadoXen = new Empregado("Carlos Méndez");
        EmpregadoXornadaCompleta empJornada = new EmpregadoXornadaCompleta("Ana López", 2000.0);
        EmpregadoSustituto empSustituto = new EmpregadoSustituto("Pedro Gómez", 1800.0);
        Freelance freelance = new Freelance("María Rodríguez", 80, 25.0);
        
         try {
            miEmpresa.engadirEmpregado(empregadoXen);
            miEmpresa.engadirEmpregado(empJornada);
            miEmpresa.engadirEmpregado(empSustituto);
            miEmpresa.engadirEmpregado(freelance);
            
        } catch (Exception e) {
            System.out.println("Excepción capturada: " + e.getMessage());
        }
        
        System.out.println("\n=== Presentación de empregados ===");
        miEmpresa.presentarEmpregados();
        
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud4-solucions/polimorfismo-e-coleccions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
