MAB 240 - Computação II

Gabarito P3 2012.2 - 13/03/2013

1.

a)

public class Naturais implements Seq<Integer> {
  private int n;
 
  public Naturais(int n) {
    this.n = n;
  }

  @Override
  public Integer proximo() {
    Integer res = n;
    n = n + 1;
    return res;
  }
}

b)

public class FiltroSeq<T> implements Seq<T> {
  private Seq<T> seq;
  private Funcao<T,Boolean> filtro;

  public FiltroSeq(Seq<T> seq, Funcao<T,Boolean> filtro) {
    this.seq = seq;
    this.filtro = filtro;
  }

  @Override
  public T proximo() {
    T prox = seq.proximo();
    while(prox != null) {
      if(filtro.aplica(prox))
        return prox;
      else
        prox = seq.proximo();
    }
    return prox;
  }
}

2.

a)

public class RegraE<T> extends Regra<T> {
  private Regra<T> r1;
  private Regra<T> r2;

  public RegraE(Regra<T> r1, Regra<T> r2) {
    this.r1 = r1;
    this.r2 = r2;
  }

  @Override
  public boolean seAplica(T obj) {
    return r1.seAplica(obj) && r2.seAplica(obj);
  }
}

public class RegraOu<T> extends Regra<T> {
  private Regra<T> r1;
  private Regra<T> r2;

  public RegraOu(Regra<T> r1, Regra<T> r2) {
    this.r1 = r1;
    this.r2 = r2;
  }

  @Override
  public boolean seAplica(T obj) {
    return r1.seAplica(obj) || r2.seAplica(obj);
  }
}

public class RegraNao<T> extends Regra<T> {
  private Regra<T> r;

  public RegraNao(Regra<T> r) {
    this.r = r;
  }

  @Override
  public boolean seAplica(T obj) {
    return !r.seAplica(obj);
  }
}

b)

public class Atrasado extends Regra<Pagamento> {
  @Override
  public boolean seAplica(Pagamento pg) {
    return pg.atrasado;
  }
}

public class Notificado extends Regra<Pagamento> {
  private int notifs;

  public Notificado(int notifs) {
    this.notifs = notifs;
  }

  @Override
  public boolean seAplica(Pagamento pg) {
    return pg.notificacoes >= notifs;
  }
}

public class NoSPC extends Regra<Pagamento> {
  @Override
  public boolean seAplica(Pagamento pg) {
    return pg.noSPC;
  }
}