// ApplierGenericAbs.java is a generalization of ApplierAbs.java that uses // generic types. // Apply method f to each element of array A of elements of class T // and put the results in an array B of elements of class S abstract class ArrayApplier { public abstract S f(T x); public void applyToArray(T[] a, S[] b) { for (int i = 0; i < a.length; i++) b[i] = f(a[i]); } } // end Applier // applyIncr extends the instantiation of ArrayApplier with // and overwrites the abstract method "f" with the increment function. class ApplyIncr extends ArrayApplier { public Integer f(Integer i) { return new Integer(i+1); } } // end applyIncr // applyTimesPi extends the instantiation of ArrayApplier with // and overwrites the abstract method "f" with the function. // that multiplies its argument by 3.14. class ApplyTimesPi extends ArrayApplier { public Double f(Double x) { return new Double(3.14*x); } } // end applyTimesPi // ApplierAbs applies applyIncr and applyTimesPito an array public class ApplierGenericAbs { public static void main(String args[]) { Integer a[] = new Integer[] { 1,2,3 }; Integer b[] = new Integer[3]; Double c[] = new Double[] { 1.0, 2.0, 3.0 }; Double d[] = new Double[3]; ApplyIncr incr = new applyIncr(); ApplyTimesPi tp = new applyTimesPi(); incr.applyToArray(a,b); System.out.println(b[0] + " " + b[1] + " " + b[2]); tp.applyToArray(c,d); System.out.println(d[0] + " " + d[1] + " " + d[2]); } } // end ApplierGenericAbs