// applier.java // Apply method f to each element of array A and put the results in B. abstract class ArrayApplier { public abstract int f(int x); public void ApplyToArray(int[] A, int[] B) { for (int i = 0; i < A.length; i++) B[i] = f(A[i]); } } // end Applier // applyIncr overwrites the abstract method "f" with the increment function. class applyIncr extends ArrayApplier { public int f(int i) { return i+1; } } // end applyIncr // applyDouble overwrites the abstract method "f" with the doubling function. class applyDouble extends ArrayApplier { public int f(int i) { return 2*i; } } // end applyDouble // applier applies applyIncr and applyDouble to an array public class applier { public static void main(String args[]) { int A[] = new int[] { 1,2,3 }; int B[] = new int[3]; applyIncr Incr = new applyIncr(); applyDouble Doub = new applyDouble(); Incr.ApplyToArray(A,B); System.out.println(B[0] + " " + B[1] + " " + B[2]); Doub.ApplyToArray(A,B); System.out.println(B[0] + " " + B[1] + " " + B[2]); } } // end applier