O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6. Object-oriented Programming

6.1
// Filename: Exercise1.java
interface Function {
    public int evaluate(int arg);
}

class Half implements Function {
    public int evaluate(int arg) {
        return arg/2;
    }
}

public class Exercise1 {
    public static int[] applyFunctionToArray(int[] arrIn) {
        int length = arrIn.length;
        int[] arrOut = new int[length];

        Function func = new Half();

        for (int i=0; i< length; i++)
            arrOut[i] = func.evaluate(arrIn[i]);

        return arrOut;
    }
}
6.2
 // Filename: Exercise2.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 Exercise2 { public ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required