How to access an object attribute within a stack of Objects

Asked

Viewed 393 times

2

I have several objects of the Vehicle class with board, model and anoFabri as attributes, inside a stack I created (Stack.java). I need to apply a method in the Stack class, which removes objects from the stack until I find a Vehicle that has the anoFabricated less than 2000.

Stack:

public class Pilha implements TAD_Pilha {
    private int topo;
    private int MAX;
    private Object memo[];

    public int getTopo() {return topo;}
    public void setTopo(int topo) {this.topo = topo;}
    public int getMAX() {return MAX;}
    public void setMAX(int MAX) {this.MAX = MAX;}
    public Object[] getMemo() {return memo;}
    public void setMemo(Object[] memo) {this.memo = memo;}

    public Pilha(int qtde){
        topo = -1;
        MAX = qtde;
        memo = new Object[MAX];
    }        

    @Override
    public boolean isEmpty(){ return(topo==-1);}
    @Override
    public boolean isFull(){ return(topo==MAX-1);}

    @Override
    public Object push(Object x){
        if(!isFull() && x != null){
            memo[++topo] = x;
            return x;
        }
        else
            return null;
    }

    @Override
    public Object pop(){
        if(!isEmpty())
            return memo[topo--];
        else
            return null;
    }

    @Override
    public Object top(){
        if(!isEmpty()){
            return memo[topo];
        }
        else
            return null;
    }

    @Override
    public String toString(){
        if(!isEmpty()){
            String msg="";
            for(int i=0; i<=topo; i++){
                msg += memo[i].toString();  
                if(i != topo) msg += ", ";
            }
            return ("Pilha = [" + msg + " ]");
        }
        else
            return "Pilha vazia!";
    }
}

Vehicle:

public class Veiculo {
    private String placa;
    private String modelo;
    private int anoFabri;    

    public Veiculo() {
    }

    public Veiculo(String placa, String modelo, int anoFabri) {
        this.placa = placa;
        this.modelo = modelo;
        this.anoFabri = anoFabri;
    }

    public String getPlaca() {
        return placa;
    }

    public void setPlaca(String placa) {
        this.placa = placa;
    }

    public String getModelo() {
        return modelo;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public int getAnoFabri() {
        return anoFabri;
    }

    public void setAnoFabri(int anoFabri) {
        this.anoFabri = anoFabri;
    }

    @Override
    public String toString() {
        return placa + " " + modelo + " " + anoFabri; //To change body of generated methods, choose Tools | Templates.
    }
}
  • 1

    You can add the code of these classes mentioned in the question?

1 answer

-1

You could do something like this:

public void RemoveVeiculosAnoFabriMaior1999() {
    while (topo > 0) {
        Veiculo veiculo = (Veiculo) memo[topo];
        if (veiculo.getAnoFabri() < 2000) break;
        memo[topo--] = null;
    }
}
  • Just to know (that the question is old), I voted negative because selective stack is a very unusual TAD, I consider a design error.

Browser other questions tagged

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