Overriding the equals method

Assignment Help JAVA Programming
Reference no: EM13309868

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?

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.

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

// Player.java

//

// Defines a Player class that holds information about an athlete.

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

importjava.util.Scanner;

public class Player

{

private String name;

private String team;

privateintjerseyNumber;

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

// 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();

}

}

Chapter 9: Inheritance 163

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

// ComparePlayers

//

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

// the same player.

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

importjava.util.Scanner;

public class ComparePlayers

{

public static void main(String[] args)

{

Player player1 = new Player();

Player player2 = new Player();

Scanner scan = new Scanner();

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

Reference no: EM13309868

Questions Cloud

How fast are they going after the collision : A 4.0 kg cube of iron slides from rest down a frictionless ramp (1.5 m above flat surface), How fast are they going after the collision
What is the maximum exiting velocity and maximum height : A fireman reduces the exit area on a nozzle so that the velocity inside the hose is quite small relative to the exiting velocity. What is the maximum exiting velocity and what is the maximum height
Calculate the diffie-hellman key values : Calculate the Diffie-Hellman key values. Calculate values for both keys (secret key a and secret key b). You must show your work for full credit.This includes the calculations each person performs to calculate and verify the values.
Obtain the phase shift between the current and the voltage : An RLC circuit has a resistance of 2 komega, a capacitance of 32 uF, and an inductance of 52 H. obtain the phase shift between the current and the voltage
Overriding the equals method : 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?
Strategic managerial communications : Strategic Managerial Communications
What is the angle the incline makes with the horizontal : A thin spherical shell rolls down an incline without slipping. what is the angle the incline makes with the horizontal
Discuss what are the three key aspects of research : Discuss what are the three key aspects of research? Which one of them you consider the most important, or are they equally important?
What will be the concentration of tce in the aqueous phase : The dimensionless henry's constant for TCE at 25 degrees celsius is 0.4. A sealed glass vial is prepared that has an air volum of 4mL overlying an aquesous volume of 36mL.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd