131k views
3 votes
Public class Player implements Comparable{private String name;private int goalsScored;// other methods go herepublic 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;

User Esentis
by
7.5k points

1 Answer

2 votes

Answer:

The answer is "Option C"

Step-by-step explanation:

In the given code, a child class "player" is defined, which inherits its base class that is "Comparable". In the child class, a private variable is declared, that is "name and goalsScored", in which name is string variable and "goalsScored" is an integer variable.

In this class a method, compareTo is declared, that accepts an object as a parameter, inside the method a player class object is created, that value is held by the reference variable, and this function will return a value, and another option is wrong, that can be described as follows:

  • In option A, It is wrong, because there is not a reference variable.
  • Option B and Option D are wrong, because in object creation first, we use the class name, which is missing.

User Scandinave
by
8.3k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.