Question

In: Computer Science

Overriding the equals Method File Player.java contains a class that holds information about an athlete: name,...

  1. Overriding the equals Method

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?
  1. 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.

// *********************************************************

// Player.java

//

// 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 jerseyNumber;

//-----------------------------------------------------------

// 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: ");

jerseyNumber = scan.nextInt();

}

}

// **************************************************************

// ComparePlayers.java

//

// Reads in two Player objects and tells whether they represent

// the same player.

// **************************************************************

import java.util.Scanner;

public class ComparePlayers

{

public static void main(String[] args)

{

Player player1 = new Player();

Player player2 = new Player();

Scanner scan = new Scanner(System.in);

//Prompt for and read in information for player 1

//Prompt for and read in information for player 2

//Compare player1 to player 2 and print a message saying

//whether they are equal

}

}

Solutions

Expert Solution

//Player.Java

public class Player {

       private String name;

       private String team;

       private int jerseyNumber;

       //Getters and Setters

       public void setName(String name){

              this.name = name;

       }

       public void setTeam (String team){

              this.team = team;

       }

       public void setNumber(int number){

              this.jerseyNumber = number;

       }

       public int getJerseyNumber() {

              return jerseyNumber;

       }

       public void setJerseyNumber(int jerseyNumber) {

              this.jerseyNumber = jerseyNumber;

       }

       public String getName() {

              return name;

       }

       public String getTeam() {

              return team;

       }

       @Override

       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 (team == null) {

                     if (other.team != null)

                           return false;

              } else if (!team.equals(other.team))

                     return false;

             

              return true;

       }

}

//Player.Java ends here

//ComparePlayers.java

import java.util.Scanner;

public class ComparePlayers {

       public static void main(String[] args) {

              Player player1 = new Player();

              Player player2 = new Player();

              // Prompt for and read in information for player 1

              Scanner sc = new Scanner(System.in);

              System.out.print("Enter name of player 1: ");

              String n1 = sc.nextLine();

              player1.setName(n1);

              System.out.print("Enter team of player 1: ");

              String t1 = sc.nextLine();

              player1.setTeam(t1);

              System.out.print("Enter jersey of player 1: ");

              int j1 = sc.nextInt();

              player1.setNumber(j1);

              sc.nextLine();

              System.out.println();

              System.out.print("Enter name of player 2: ");

              String n2 = sc.nextLine();

              player2.setName(n2);

              System.out.print("Enter team of player 2: ");

              String t2 = sc.nextLine();

              player2.setTeam(t2);

              System.out.print("Enter jersey of player 2: ");

              int j2 = sc.nextInt();    

              player2.setNumber(j2);

              System.out.println();

             

              if(player1.equals(player2))

                     System.out.println("player 1 is equal to player 2");

              else

                     System.out.println("player 1 is not equal to player 2");

             

              sc.close();

       }

}

//ComparePlayers.java ends hee

Output:


Related Solutions

Create a class that holds data about a job applicant. Include a name, a phone number,...
Create a class that holds data about a job applicant. Include a name, a phone number, and four Boolean fields that represent whether the applicant is skilled in each of the following areas: word processing, spreadsheets, databases, and graphics: Include a constructor that accepts values for each of the fields. Also include a get method for each field. Create an application that instantiates several job applicant objects and pass each in turn to a Boolean method that determines whether each...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Consider a class Book that contains information about a Book. The class should has the following...
Consider a class Book that contains information about a Book. The class should has the following attributes: • The title of the book • The author of the book • Year of publication (e.g. 1994) • The number of people that have rated this book as a 1 (Terrible) • The number of people that have rated this book as a 2 (Bad) • The number of people that have rated this book as a 3 (OK) • The number...
C++ There is a file, called EmployeeInfo.txt, that contains information about company employees work for a...
C++ There is a file, called EmployeeInfo.txt, that contains information about company employees work for a week. You will write a program that reads the info from the file, and produces an output file called EmployeePay.txt with information about the employees pay amount for that week. Details: The input file is called EmployeeInfo.txt There are 4 lines of input Each line has the same form Last Name, first Name, hours worked, pay rate, tax percentage, extra deductions Example: John Doe...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
C++ Design a class named TermPaper that holds an author's name, the subject of the paper,...
C++ Design a class named TermPaper that holds an author's name, the subject of the paper, and an assigned letter grade. Include methods to set the values for each data field and display the values for each data field. Create the class diagram and write the pseudocode that defines the class. Pseudocode help please
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
Suppose we have an input file that contains information about how many hours each employee of...
Suppose we have an input file that contains information about how many hours each employee of a company has worked. The file looks like the following (Employee's name, Hours): John 6.5 9.5 7.25 9 8.55 Oscar 12.5 13.5 14 16 18.75 Judith 7 7 7 ••• Construct a program that reads this file and calculates the total number of hours worked by each individual. Make sure that the program handles each specific exception that could occur.' python
Create a class Team to hold data about a college sports team. The Team class holds...
Create a class Team to hold data about a college sports team. The Team class holds data fields for college name (such as Hampton College), sport (such as Soccer), and team name (such as Tigers). Include a constructor that takes parameters for each field, and get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to Sportsmanship! Save the class in Team.java. Create a UML class diagram as well....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT