Overriding the equals Method File Player java contains a class that holds inform
ID: 3709299 • Letter: O
Question
Overriding the equals Method File Player java contains a class that holds information about an athlete: name, team, and uniform number. File baseball players and determine whether or not they are the same player Fill in the missing code in ComparePlayers so that it reads in two players and prints "Same player" if they a contains a skeletal program that uses the Player class to read in information about two are the same, "Different players" if they are different. Use the equals method, which Player inherits from the Object class, to determine whether two players are the same. Are the results what you expect? 2. The problem above is that as defined in the Object class, equals does an address comparison. It says that two objects are the same if they live at the same memory location, that is, if the variables that hold references to them are aliases. The two Player objects in this program are not aliases, so even if they contain exactly the same information they will be not equal." To make equals compare the actual information in the object, you can override it with a definition specific to the class. It might make sense to say that two players are "equal" (the same player) if they are on the same team and have the same uniform number Use this strategy to define an equals method for the Player class. Your method should take a Player object and return true if it is equal to the current object, false otherwise. Test your ComparePlayers program using your modified Player class. It should give the results you would expect. D /1 Player.java /1 Defines a Player class that holds information about an athlete import java.util.Scanner public class Player private String name: private String team; private int jerscyNumber // Prompts for and reads in the player's name, team, and // jersey number public void readPlayer ) Scanner scan new Scanner (System.in) System.out.print("Name:) name scan.nextLine) System. out. print (Team: team scan.nextLine ); System.out print("Jersey number: jerseyNumberScan.nextInt :Explanation / Answer
ComparePlayer.java
public class ComparePlayer {
public static void main(String[] args) {
Player player1 = new Player();
Player player2 = new Player();
player1.readPlayer();
player2.readPlayer();
if(player1.equals(player2)){
System.out.println("Both players are same");
} else {
System.out.println("Both players are not same");
}
}
}
Player.java
import java.util.Scanner;
public class Player {
private String name, team;
private int jerseyNumber;
public void readPlayer() {
Scanner scan = new Scanner(System.in);
System.out.println("Name: ");
name = scan.nextLine();
System.out.println("Team: ");
team = scan.nextLine();
System.out.println("Jersey Number: ");
jerseyNumber=scan.nextInt();
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Player other = (Player) obj;
if (jerseyNumber != other.jerseyNumber)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (team == null) {
if (other.team != null)
return false;
} else if (!team.equals(other.team))
return false;
return true;
}
}
Output:
Name:
Suresh
Team:
AAA
Jersey Number:
111
Name:
Suresh
Team:
AAA
Jersey Number:
111
Both players are same
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.