176k views
4 votes
Which of the following is correct implementation code for the compareTo method in the ExpertPlayer class?

I.
ExpertPlayer rhs = (ExpertPlayer) obj;
if(myRating == rhs.myRating) return 0;
else if (myRating < rhs.myRating) return –1;
else return 1;

II.
ExpertPLayer rhs = (ExpertPlayer) obj;
return myRating – rhs.myRating;

III.
ExpertPlayer rhs = (ExpertPlayer) obj;
if(getName().equals(rhs.getName())) return 0;
else if (getName().compareTo(rhs.getName()) < 0) return –1;
else return 1;

A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III

User Akrohit
by
5.5k points

1 Answer

5 votes

Complete Question:

The complete question is shown in uploaded image one and two.

Answer:

Answer is E

Step-by-step explanation:

In order to gain understanding of this answer let look at each code line by line

Considering Method one :

public int compareTo(Object obj)

{

This is the deceleration of the method.

ExpertPlayer rhs = (ExpertPlayer) obj;

On this line of code above an object (obj) that is passed into the method ( compareTo)is converted to type ExpertPlayer and is being assigned to a variable(rhs) of type ExpertPlayer

if(myRating == rhs.myRating) return 0;

else if(myRating < rhs.myRating) return 1;

else return 1;

}

This if conditional statement above will comparing the private variable myRating defined on ExpertPlayer with the private variable on the instance of the ExpertPlayer(i.e rhs.myRating)

some might ask why is compareTo method accessing myRating which is private variable without getters or setters method. This is because the method (compareTo )and the variable(myRating) are defined in the same class.

Looking at this conditional statement we see that

if myRating == rhs.Rating it will return 0

if myRating < rhs.Rating it will return -1 a negative value

if myRating > rhs.Rating it will return 1 a positive value

Considering Method two

public int compareTo(Object obj)

{

As usual this code above is the deceleration of the method.

ExpertPLayer rhs = (ExpertPlayer) obj;

On this line of code above an object (obj) that is passed into the method ( compareTo)is converted to type ExpertPlayer and is being assigned to a variable(rhs) of type ExpertPlayer

return myRating – rhs.myRating;

}

On this line of code above a variable of an instance of ExpertPlayer class (i.e rhs.myRating) is subtracted from a private variable ( myrating) in ExpertPlayer class

Now looking at this code we see that if they are equal(i.e if myrating is equal to rhs.myRating) 0 will be returned

if myRating is less than rhs.myRating a negative value is returned

and if myRating is greater than rhs.myRating a positive value is return

comparing this with the first method we see that they are the same.

Considering Method Three

public int compareTo(Object obj)

{

As usual this code above is the deceleration of the method.

ExpertPLayer rhs = (ExpertPlayer) obj;

On this line of code above an object (obj) that is passed into the method ( compareTo)is converted to type ExpertPlayer and is being assigned to a variable(rhs) of type ExpertPlayer

if (getName().equals(rhs.getName()))

return 0;

else if (getName().compareTo(rhs.getName()) < 0)

return -1;

else

return 1;

}

On the above snippet of code myName a private variable gotten using getName() method from the HumanPlayer class which extended by ExpertPlayer class is compared with the myName variable of the instance of ExpertPlayer (i.e rhs) gotten using rhs.getName()

looking at this code above we see that it produces the same result as method one and two in that if the two compared items are the same it will return 0, if getName() is less than rhs.getName() it will return -1 ,which is a negative value, and

if getName() is greater than rhs.getName() it will return 1 which is a positive value.

Which of the following is correct implementation code for the compareTo method in-example-1
Which of the following is correct implementation code for the compareTo method in-example-2
Which of the following is correct implementation code for the compareTo method in-example-3
User Matroska
by
5.3k points