Ok, I will post the code here. My filters don't seem to work and I can't find out what I am doing wrong for the life of me... other than that I beleive it works. If someone sees it before midnight tonight (its due then) plz reply. Thx.
import java.util.Random;
// this interface permits us to accept or // reject an object interface Filter { boolean accept(Object o); }
// this interface permits us to process an // array of objects based on a filter criteria interface Processor { Object process(Object[] arr, Filter f); }
// this implementation of the filter accepts everything class AllFilter implements Filter { public boolean accept(Object o) { return true; } }
// this implementation of the filter should only accept // even numbers class EvenFilter implements Filter { public boolean accept(Object o) { int number = ((Integer)o).intValue(); int numDivByTwo = number % 2;
// this implementation of the filter should only accept // odd numbers class OddFilter implements Filter { public boolean accept(Object o) { int number = ((Integer)o).intValue(); int numDivByTwo = number % 2;
// this implementation f the filter should only accept // every other number that is input to it. class AlternatingFilter implements Filter { public boolean accept(Object o) { boolean everyOther = true;
// this implementation of a processor should take all the // aceptable objects out of the array, and concatenate them // space separated into a string, then return that string. class ToStringProcessor implements Processor {
StringBuffer s1 = new StringBuffer();
public Object process(Object[] arr, Filter f) { for (int i = 0; i < arr.length; i++) { if (f.accept(arr)) { s1.append(arr.toString()); s1.append(" "); }
} return s1; } }
// this implementation of a processor should compute the // average of all the acceptable objects in the array, and // return that average. class AverageProcessor implements Processor { public Object process(Object[] arr, Filter f) { double average; int total = 0; int number;
for (int i = 0; i < arr.length; i++) { if (f.accept(arr)) { number = ((Integer)arr).intValue(); total = total + number; } } average = (double)total / arr.length; return new Double(average); } }
public class Week01 { public static final int MAXSIZE = 100;
public static void main(String[] args) { Random random = new Random(3141592655L);
// create an array of random Integer objects Integer randomInts[] = new Integer[MAXSIZE]; for (int i=0; i<randomInts.length; ++i) randomInts = new Integer(random.nextInt(1000));
Filter all = new AllFilter(); // all elements Filter evens = new EvenFilter(); // only even elements Filter odds = new OddFilter(); // only odd elements Filter alternating = new AlternatingFilter(); // alternating elments
Processor print = new ToStringProcessor(); Processor avg = new AverageProcessor();