• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Sybex Wrong answer for review Question #11 for chapter 6

 
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe there is an error in Chapter 6 Review Question 11.
In my opinion correct answer should be B,
since the list is sorted in ascending order and would result in [Olivia, leo].  


List<String> cats = new ArrayList<>();
cats.add("leo");
cats.add("Olivia");

cats.sort((c1, c2) -> c1.compareTo(c2));

System.out.println(cats);

Output: [Olivia, leo]

which would be answer B, and not A.


cats.sort((c1, c2) -> c2.compareTo(c1)); would reverse the order or better: would sort in descending order.
 
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always supply full details for such questions, including book name and authors.
There is no such thing in Java® as sorting in descending order; what you are doing by writing a reversing Comparator is redefining 1 to be greater than 2 (!!)
What you have is in normal ascending order because the String#compareTo() method compares the Unicode/ASCII values of individual chars. If you look at an ASCII table (one example here), you will find that Olivia does in fact come before leo. Please check very carefully that you have copied the question exactly.
 
Veit Ullmann
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book's name is
OCP Oracle Certified Professional Java SE 11 Programmer 1
Study Guide Exam 1Z0-815
by Jeanne Boyarsky and Scott Selikoff.

Question: what is the output of the mentioned code.

A: [leo, Olivia]
B: [Olivia, leo]
and some other possible answers.

In the appendix on page 510 answer A is marked as correct, and I don't agree.

It should be answer B.


>> If you look at an ASCII table (one example here), you will find that Olivia does in fact come before leo.

That's what I wanted to say!

 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it yesterday and got Olivia first. Please check very carefully that you have copied the c1 and c2 the right way round from the book. Please check here for the errata; I didn't find what you have found, so you may have found a new erratum.
 
Veit Ullmann
Greenhorn
Posts: 4
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did check it very carefully and also checked the errata and that's the reason I posted it here.
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please wait; Jeanne Boyarsky checks this forum at regular intervals and you can get the credit for being the first person to find that erratum
 
author & internet detective
Posts: 39997
807
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veit,
You missed a really important character in your first post

This is what is in the book (note the minus sign)


So it is sorting descendingly, not ascendingly and A is correct.

On page 510, the answer explanation says the list is being sorted in descending order. Which is good as it matches the code in the question in the book.

The importance of a single character!
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah! I don't have your books, so I couldn't check that.
Do you say anywhere thatis potentially unreliable, but in the case of String#compareTo(), the error I am thinking about won't occur?
 
Jeanne Boyarsky
author & internet detective
Posts: 39997
807
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah! I don't have your books, so I couldn't check that.
Do you say anywhere thatis potentially unreliable, but in the case of String#compareTo(), the error I am thinking about won't occur?


We do not. What is the problem with the negative sign?
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in the Java™ Tutorials (at least I think it is). If the compareTo() method returns −2147483648, you will get an overflow error if you try to negate it. Such overflow won't happen with String comparisons because the comparison uses the differences between successive chars and chars are never negative (and they have a restricted range).

[edit] Add text underlines in first line.
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If the compareTo() method returns −2147483648, you will get an overflow error if you try to negate it.



No, I just tried it. You get -2147483648 as the result of calculating -2147483648, but that does indeed qualify as what you call "unreliable".
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several problems. including that subtraction is error‑prone. If one account has $20,000,000/£20,000,000/€20,000,000 and the other is overdrawn by $2,000,000/£2,000,000/€2,000,000, you will get an overflow error and the two will sort the wrong way round. If the overdrawn account is down by $1,474,836.48/£1,474,836.48/€1,474,836.48, (and if I got the figures right), your first comparator will record the difference as Integer.MIN_VALUE but the negation/sign change operator in line 4 will cause an overflow error and you will get exactly the same result irrespective of which order the two arguments go. Negating Integer.MIN_VALUE is a corner case where you get an overflow error.

Actually, I think the same error will occur with or without the negation/sign change operator. You can get correct behaviour with Integer#compare() and Comparator#comparingInt().
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic