Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingOther conceptsNested classes

Anonymous classes properties

Iterations

Report a typo

There are two static methods:

static void performIterationsWithCallback(int numberOfIterations, LoopCallback callback)
static void startIterations(int numberOfIterations)

The first method takes a number of iterations for a loop and a callback that is called on each iteration. The second method takes a number of iterations, creates a callback, and passes them to the first method.

You should finish the second method startIterations. It must create an instance of an anonymous class that implements LoopCallback and passes it to the first method.

The overridden method onNewIteration should output the number of iteration to the standard output on a separate line. For example:

Iteration: 0
Write a program in Java 17
class IteratorExecutor {

static void performIterationsWithCallback(int numberOfIterations, LoopCallback callback) {
for (int i = 0; i < numberOfIterations; i++) {
callback.onNewIteration(i);
}
}

static void startIterations(int numberOfIterations) {
// invoke the method performIterationsWithCallback here
}
}

// Don't change the code below
interface LoopCallback {

void onNewIteration(int iteration);
}
___

Create a free account to access the full topic