About Question enthuware.ocpjp.v8.2.1800 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
kukis13
Posts: 1
Joined: Wed Dec 02, 2015 4:51 am
Contact:

About Question enthuware.ocpjp.v8.2.1800 :

Post 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
}

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Not sure what you mean. Did you try the code? It works fine.
-Paul.
If you like our products and services, please help us by posting your review here.

MichaelZett
Posts: 22
Joined: Wed Dec 10, 2014 8:28 am
Contact:

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

Post 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.

lucasheitich
Posts: 4
Joined: Tue Feb 17, 2015 9:54 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

lucasheitich
Posts: 4
Joined: Tue Feb 17, 2015 9:54 am
Contact:

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

Post by lucasheitich »

Good one, Paul.
Thanks for clearing!

rocoty
Posts: 10
Joined: Tue Dec 01, 2015 11:22 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

rocoty
Posts: 10
Joined: Tue Dec 01, 2015 11:22 am
Contact:

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

Post 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 :)

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests