160k views
4 votes
Public class Player implements Comparable

{
private String name;
private int goalsScored;

// other methods go here

public int compareTo(Object otherObject)
{ __________________________________
return (goalsScored – otherPlayer.goalsScored);
}
}

What statement can be used to complete the compareTo()method?
A. Player otherPlayer = otherObject;
B. Object otherPlayer = otherObject;
C. Player otherPlayer = (Player) otherObject;
D. Object otherPlayer = (Player) otherObject;

1 Answer

4 votes

Answer:

OPtion A

Step-by-step explanation:

As otherObject is coming as a parameter to the function, there must be another object of type player that it should be assigned to so that we can compare its field.

IN Option B there is syntax error that Object class is not defined, if defined also the comparison will not work because the comparison can be done for same type objects.

In Option C the right hand size is syntax error because there is no definition of it in Java.

In Option C also there is syntax error and it its right hand size is not defined.

User Jacky Boen
by
7.6k points