A developer tests a proposed natural ordering for three distinct objects. The following calls return the recorded integers:
Call
Result
X.compareTo(Y)
-3
Y.compareTo(X)
3
Y.compareTo(Z)
-2
Z.compareTo(Y)
2
X.compareTo(Z)
4
Z.compareTo(X)
-4
Every self-comparison returns zero. Select every statement supported by these observations.
The recorded results violate the contract because valid nonzero results must have magnitude one.
Replacing each nonzero result with its sign would preserve the observed transitivity violation.
The recorded results satisfy the comparison contract because all reversed pairs have opposite signs.
The recorded results violate transitivity, even though each reversed pair has the opposite sign.
Unlock full course notes by signing up
Count representatives after comparison ties
A scheduling application inserts five distinct Job objects into an initially empty ordered set. The set keeps one representative whenever two objects compare as zero. No objects are removed or modified.
Each job's compareTo method compares only its integer duration in minutes, using ascending order. The jobs have durations 11 min, 7 min, 11 min, 16 min, and 7 min. Their identifiers are all different, and equals compares identifiers only.
How many objects remain in the set after all insertions? Answer in objects, as an exact integer with zero decimal places.
Compare package labels as strings
A warehouse stores package labels as Java strings. Their lexicographic order compares corresponding characters from left to right. For String.compareTo, the first differing characters determine the result: subtract the argument character's value from the receiver character's value. If one string is a prefix of the other, subtract the argument's length from the receiver's length.
Use these character values: '1' is 49, '8' is 56, 'R' is 82 and 'r' is 114. Comparisons are case sensitive. Complete the table with exact integer return values, rather than just their signs. No units apply.
Repair subtraction-based comparison
An accounting program orders Balance objects by ascending integer balance in cents. A programmer writes:
Object A stores 2000000000 cents, and object B stores -2000000000 cents. Both values fit in a Java signed 32-bit int, whose range is -2147483648 through 2147483647. Integer subtraction wraps on overflow: subtract 4294967296 from an excessively large result, or add 4294967296 to an excessively small result, to obtain its signed 32-bit value.
Find three outputs: the buggy result of A.compareTo(B), the buggy result of B.compareTo(A), and the safe result of Integer.compare(A.cents, B.cents). Enter each as an exact unitless integer with zero decimal places; comparison outputs are not monetary amounts.
Infer outcomes across comparison ties
A developer tests five distinct, non-null Ticket objects named a, b, c, d and e. Their comparison method obeys the full Java Comparable contract, and its outcomes remain unchanged throughout the test. Four observed results are:
The developer then uses each object as the receiver and calls compareTo once with each of the other four objects. No object is compared with itself. Thus, reversing receiver and argument counts as a separate call.
How many of these calls return a negative integer, how many return zero, and how many return a positive integer? Enter each number of calls as an exact whole number.