Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.3339 :

Posted: Thu Jan 07, 2021 8:28 am
by teodorj
Given the code...

Code: Select all

		Stream<Integer> sin = Stream.of(1, 2, 3 );
		Consumer<Integer> c1 = System.out::print;
		Consumer<Integer> c2 = x->{ System.out.println(" * "+number+" = "+x*number); };
		INSERT CODE HERE
This will actually not compile because unknown number variable

The given variable must be changed to x like below :

Code: Select all

   public static void main(String[] args) throws IOException {
		Stream<Integer> sin = Stream.of(1, 2, 3 );
		Consumer<Integer> c1 = System.out::print;
		Consumer<Integer> c2 = x->{ System.out.println(" * "+x+" = "+x*x); };
		sin.forEach(c1.andThen(c2));
   }

Re: About Question enthuware.ocpjp.ii.v11.2.3339 :

Posted: Thu Jan 07, 2021 8:41 am
by admin
Tried it just now. Works fine!

Re: About Question enthuware.ocpjp.ii.v11.2.3339 :

Posted: Thu Jan 07, 2021 8:54 am
by teodorj
From where did Consumer body reference the "number" variable?

Re: About Question enthuware.ocpjp.ii.v11.2.3339 :

Posted: Thu Jan 07, 2021 9:00 am
by admin
It is the method parameter. You have to try the code exactly as given in the question:
public static void generateMultiplicationTable(int number){
Stream<Integer> sin = Stream.of(1, 2, 3 );
Consumer<Integer> c1 = System.out::print;
Consumer<Integer> c2 = x->{ System.out.println(" * "+number+" = "+x*number); };

INSERT CODE HERE
}
public static void main(String[] args) throws Exception{
generateMultiplicationTable(2);
}

Re: About Question enthuware.ocpjp.ii.v11.2.3339 :

Posted: Thu Jan 07, 2021 9:02 am
by teodorj
I see. I wrap the code in main method and missed the variable :lol:
Thanks for the confirmation :cheers: