Page 1 of 1

About Question enthuware.ocpjp.v8.2.1800 :

Posted: Wed Dec 02, 2015 3:30 pm
by kukis13
This code will not compile because Student::debug method is not a valid consumer - it doesn't take any arguments.

Relevant code:

Code: Select all

slist.stream().forEach(Student::debug);
public void debug(){
//sth
}

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Wed Dec 02, 2015 8:46 pm
by admin
Not sure what you mean. Did you try the code? It works fine.
-Paul.

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Thu Dec 17, 2015 7:17 am
by MichaelZett
What he means is, that forEach() takes a Consumer. In another question there is an example with a no argument method that doesn't suffice as Consumer. As explanation the corresponding lambda to the method reference may help:

Code: Select all

slist.stream().forEach(s -> s.debug());
The other question is enthuware.ocpjp.v8.2.1789. There "Strings" but not "Names" are the streamed objects.

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Mon Jan 18, 2016 7:09 pm
by lucasheitich
Now I got confused and I'm 1 week to the exam...

I got the problem, what he means (in code) is this:

Example one (Works fine even the Student::debug has no parameter)

Code: Select all

	static class Student {
		private String name;
		private int marks;

		public Student(String string, int i) {
			this.name = string;
			this.marks = i;
		}

		public void addMarks(int m) {
			this.marks += m;
		}

		public void debug() {
			System.out.println(name + ":" + marks);
		}

		public static void main(String[] args) {
			List<Student> slist = Arrays.asList(new Student("S1", 40), new Student("S2", 35), new Student("S3", 30));
			Consumer<Student> increaseMarks = s -> s.addMarks(10);
			slist.forEach(increaseMarks);
			slist.stream().forEach(Student::debug);
		}
	}
Example two (Compilation error: Names::printNames has no parameter)

Code: Select all


	static class Names {
		private List<String> list;

		public List<String> getList() {
			return list;
		}

		public void setList(List<String> list) {
			this.list = list;
		}

		public void printNames() {
			System.out.println(getList());
		}
		public static void main(String[] args) {
			List<String> list = Arrays.asList("Bob Hope","Bob Dole", "Bob Brown");
			Names n = new Names(); 
			n.setList(list.stream().collect(Collectors.toList()));
			n.getList().forEach(Names::printNames);    
		}
	}
The question is: Why Example 1 works fine when Example 2 doesn't?

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Mon Jan 18, 2016 9:25 pm
by admin
In the first example, (i.e. slist.stream().forEach(Student::debug); ) slist is typed to Student. Thus, each element returned by the stream() method is known to be of type Student. Therefore, Student::debug can be applied on each object because the object is indeed of type Student.

In the second example, (i.e. n.getList().forEach(Names::printNames); ), n.getList() returns a list of Strings, and when you call forEach(Names::printNames); on this list, you are implying that you want to call Names::printNames method on each of those elements of that list. Now, think about it, how will printNames method work on an object of type String? It can work only on an object of type Names.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Tue Jan 19, 2016 8:52 pm
by lucasheitich
Good one, Paul.
Thanks for clearing!

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Fri Feb 19, 2016 7:42 am
by rocoty
In this question, there was no obvious indication as to whether we were to assume that constructors, getters and setters were present. The comment is ambiguous to say the least. I, for one assumed the comment to be of no significance to the question and answered "It will not compile".

Other questions have such comments as well, when we are meant to assume that constructors and getters/setters are present, but these are usually way less ambiguous, in that they explicitly state that constructors, getters and setters are ommitted or not shown. This one, however, did not state any of this, which led me to believe that it didn't serve the same purpose.

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Fri Feb 19, 2016 8:11 am
by admin
The statement has been updated to make it clear.

But please be aware that the real exam questions also may have similar statements.

thank you for your feedback.
Paul.

Re: About Question enthuware.ocpjp.v8.2.1800 :

Posted: Fri Feb 19, 2016 9:27 am
by rocoty
admin wrote:The statement has been updated to make it clear.

But please be aware that the real exam questions also may have similar statements.

thank you for your feedback.
Paul.
Thank you very much for the warning :)