[ Team LiB ] Page 652

[ Team LiB ] 7 Nested Classes and Interfaces 7.1 // Filename: Exercise3.java interface Function { public int evaluate(int arg); } class Half implements Function { public int evaluate(int arg) { return arg/2; } } class Print implements Function { public int evaluate(int arg) { System.out.println(arg); return arg; } } public class Exercise3 { /* Inner class that applies the function, prints the value, and returns the result. */ static class PrintFunc extends Print { PrintFunc(Function f) { func = f; } Function func; public int evaluate(int arg) { return super.evaluate(func.evaluate(arg)); } } // Inner class that just returns the argument unchanged. /* Use this when you want a PrintFunc object to printthe argument as-is. */ static class NoOpFunc implements Function { public int evaluate(int arg) { return arg; } } public static void main(String[] args) { // Create array with values 1 .. 10 int[] myArr = new int[10]; for (int i=0; i<10;) myArr[i] = ++i; // Print array without modificationapplyFunctionToArray(myArr, new PrintFunc(new Page 653
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services

Comments are closed.