According to the compareTo method for Circle objects as defined in the diagram b
ID: 3803144 • Letter: A
Question
According to the compareTo method for Circle objects as defined in the diagram below, what is the output of the following code sequence ?
Circle c1 = new Circle(5);
Circle c2 = new Circle(5);
Circle c3 = new Circle(15);
System.out.println(c1.compareTo(c1));
System.out.println(c1.compareTo(c2));
System.out.println(c2.compareTo(c3));
System.out.println(c3.compareTo(c2));
intA "int A intB" evaluates to true intB "cl c2" evaluate to fala "el c2" evaluatog to t Figure 6.1 Comparing primitive and nonprimitive variablesExplanation / Answer
Output:
0
0
-1
1
The compareTo() method has return type int so it returns 0, -1 or 1. If both the elements compared are same then it returns 0. If first is greater then it returns 1 otherwise -1 is returned. So the values printed says relation of the radius of circle on left side with the radius of circle in right side.
The figure says something like it returns true if radius is same otherwise false, you can consider 0 means true here 1 or -1 false here. This should not return boolean value because the Java documentation says this method returns integer. So here the compareTo() is considered and should be implemented by radius comparison.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.