Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Overriding the equals Method

Jeremy12
Jeremy12 


File Player.java contains a class that holds information about an athlete: name, team, and uniform number. File ComparePlayers.java contains a skeletal program that uses the Player class to read in information about two baseball players and determine whether or not they are the same player. 1. Fill in the missing code in ComparePlayers so that it reads in two players and prints “Same player” if they 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. Attachments Tags Clarifications

Answers

Java - Overriding the equals Method - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Overriding the equals Method assignment including source and comments Attachments
ComparePlayers.zip (1 K) Download
Preview
import java.util.Scanner; public class Player { private String name; private String team; private int jerseyNumber; //----------------------------------------------------------- // Prompts for and reads in the player's name, team, and // jersey number. //-----------------------------------------------------------

Preview
************************************************************** // ComparePlayers //// Reads in two Player objects and tells whether they represent // the same player. // ************************************************************** public class ComparePlayers{ public static void main(String[]

ComparePlayers_output.png (3 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Overriding the equals Method ComparePlayer

mariobeka
mariobeka 



Below you will find all the code that was asked for and also some test cases
screenshots of the program working.
Attachments
ComparePlayer.zip (3 K) Download





Preview
for and read in information for player 2 player2.readPlayer(); // Compare player1 to player 2 and print a message saying // whether they are equal

Preview
Player { private String name; private String team; private int jerseyNumber; // ----------------------------------------------------------- // Prompts for and reads in the player's name, team, and // jersey number. // ----------------------------------------------------------- public void readPlayer() { Scanner

Screen Shot 2014-09-19 at 6.38.51 PM.png (22 K) Download Preview

Screen Shot 2014-09-19 at 6.39.17 PM.png (18 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer