MAB 240 - Computação II

Gabarito P2 2012.2 - 08/03/2013

1.

a)

public class Explode implements Funcao<String,List<Character>> {
	@Override
	public List<Character> aplica(String s) {
		ArrayList<Character> res =
				new ArrayList<Character>();
		for(int i = 0; i < s.length(); i++)
			res.add(s.charAt(i));
		return res;
	}
}

b)

public class ContaCaractere implements Funcao<List<Character>,Integer> {
	private Character c;
	
	public ContaCaractere(Character c) {
		this.c = c;
	}
	
	@Override
	public Integer aplica(List<Character> l) {
		int n = 0;
		for(Character ch: l)
			if(c.equals(ch))
				n++;
		return n;
	}
}

c)

public class Composicao<X,Y,Z> implements Funcao<X,Z> {
	private Funcao<X,Y> f;
	private Funcao<Y,Z> g;
	
	public Composicao(Funcao<X,Y> f, Funcao<Y,Z> g) {
		this.f = f;
		this.g = g;
	}
	
	@Override
	public Z aplica(X x) {
		return g.aplica(f.aplica(x));
	}
}

2.

a)

public class FiltroAcumulador extends Filtro {
	private int total = 0;
	
	@Override
	protected String processa(String linha) {
		total += Double.parseDouble(linha);
		return "" + total;
	}
}

b)

public abstract class FiltroRecupera extends Filtro {
	public void filtra() throws IOException {
		try {
			super.filtra();
		} catch(IOException e) {
			throw e;
		} catch(Exception e) {
			System.out.println(e.getMessage());
			this.filtra();
		}
	}
}