my code stock.com

Snippet options

Download: Download snippet as componentecompuesto.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

public class ComponenteCompuesto extends Componente{
    
    protected Componente[] vec;
    protected int num;
    
    public ComponenteCompuesto(){
        vec = new Componente[100];
        num = 0;
    }
    
    public void añade(Componente a){
        if(num < 100){
            vec[num] = a;
            num = num+1;    
        }
    }
    
    public int cardinal(){
        return num;
    }
    
    public void modifica(int pos, Componente c){
            if(pos < num){ // <100 o <num? (Ocupado hasta num -1)
                vec[num] = c.copia();  // c.copia()?  //cardinal() o num?
            }
    }

    public String toString(){
        String componentes = new String();
        for(int i = 0; i < vec.length; i++){
            if (vec[i] != null){
                componentes += vec[i];
            }else{
                break;
            }
        }
    return componentes;
    }
    
    public int nCaracteres(){
        String componentes = toString();
        int count = 0;
        for (int i = 0; i < componentes.length(); i++){
            if (componentes.charAt(i) != ' '){
                count++;
            }
        }
    return count;
    }
    
    public int nPalabras(){
        return num;
    }
    
    public Componente copia(){
        ComponenteCompuesto c = new ComponenteCompuesto();
        c.vec = new Componente[100];
        c.num = num;
        
        for (int i = 0; i < vec.length; i++){
            if(vec[i] != null){
                c.vec[i] = vec[i];
            }else{
                break;
            }
        }
    return c;
    }
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.