Page 1 of 1

About Question enthuware.ocpjp.v8.2.1875 :

Posted: Thu Mar 17, 2016 2:28 am
by surzhin
1. There isn’t a field with name "author" in the class Book. ;)
2.
public class TestClass {

public static void main(String[] args) {
List<Book> books = getBooksByAuthor("Ludlum");
books.stream().sorted().forEach(b -> System.out.println(b.getIsbn()));
}

private static List<Book> getBooksByAuthor(String ludlum) {
List<Book> al = new ArrayList<>();
al.add(new Book("123", "Big Bang"));
return al;
}
}

It will no throw a ClassCastException and print "123".

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

Posted: Thu Mar 17, 2016 10:09 am
by admin
surzhin wrote:1. There isn’t a field with name "author" in the class Book. ;)
That is ok. The problem statement asks you to assume that getBooksByAuthor is a valid method that returns a List of Books. This method might return the list of books through some other means (not necessarily by looking at a field of the class).
2.
public class TestClass {

public static void main(String[] args) {
List<Book> books = getBooksByAuthor("Ludlum");
books.stream().sorted().forEach(b -> System.out.println(b.getIsbn()));
}

private static List<Book> getBooksByAuthor(String ludlum) {
List<Book> al = new ArrayList<>();
al.add(new Book("123", "Big Bang"));
return al;
}
}

It will no throw a ClassCastException and print "123".
getBooksByAuthor returns a List of Books (plural). So it could contain more than one book.

HTH,
Paul.