import utils.Filter; import utils.Predicate; import java.util.List; import java.util.Arrays; import java.util.Iterator; /** Exercises for Lab 6. * @author You. */ public class Iterating { /** A couple of test cases. */ private static final Integer[][] TESTS = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { 1, 2, 3, 0, 7, 8, 6, 9, 10, 1 } }; /** Print out the items returned by L. */ static void printAll(Filter L) { System.out.print("["); String sep; sep = ""; for (Integer i: L) { System.out.print(sep + i); sep = ", "; } System.out.println("]"); } /** Returns a filter that delivers every fourth item of INPUT, * starting with the first. You should not need to define a new * class. */ static Filter everyFourth(Iterator input) { return null; // FIXME } /** Test the Filter classes. */ public static void main(String... unused) { for (Integer[] data: TESTS) { List test = Arrays.asList(data); Filter f1 = new TrivialFilter(test.iterator()); printAll(f1); Filter f2 = new PredicateFilter(new Even(), test.iterator()); printAll(f2); Filter f3 = new AlternatingFilter(test.iterator()); printAll(f3); Filter f4 = everyFourth(test.iterator()); printAll(f4); } } /** A class whose instances represent the test for eveness. */ static class Even implements Predicate { @Override public boolean test(Integer x) { return x % 2 == 0; } // FIXME } }