Capture variables

Report a typo

There is an interface Returner:

interface Returner {
    
    public String returnString();
    
    public int returnInt();
}

You should create an anonymous class that implements the interface and assign the instance to the variable returner. The anonymous class must override both methods of the interface. The method returnString should capture the string variable str from the context and return it, the second method should capture the integer variable number from the context and return it. These variables will be accessible during testing.

Write a program in Java 17
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

final Scanner scanner = new Scanner(System.in);
final String str = scanner.nextLine();
final int number = Integer.parseInt(scanner.nextLine());

Returner returner = /* create an instance of an anonymous class here,
do not forget ; on the end;
variables str and number will be accessible during testing */

System.out.println(returner.returnString());
System.out.println(returner.returnInt());
}
}

interface Returner {

public String returnString();

public int returnInt();
}
___

Create a free account to access the full topic