Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

here us compare players

// ********************************************************** // ComparePlayers // // 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) { Scanner scan = new Scanner(System.in);

Player player1 = new Player(); Player player2 = new Player();

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

} } here is players

// ********************************************************** // 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.next(); System.out.print("Team: "); team = scan.next(); System.out.print("Jersey number: "); jerseyNumber = scan.nextInt(); }

} this is what I was given please do not do anything past a beginner level of computer science so I can understand and let me know if there are any questions :))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago