subject
Computers and Technology, 21.02.2020 17:57 whimsy

The Player. java file defines a class that holds information about an athlete: name, team, and uniform number. The ComparePlayers. java file contains a skeletal program that uses the Player class to collect information about two baseball players and determine whether or not they are the same player.

Complete the main() method in ComparePlayers. java so that it reads in information for two players and prints "Same player" if they are the same, or "Different players" if they are different. Use the equals() method, which Player inherits from the Object class, to determine whether the two players are the same. Are the results what you expect?

The problem is that, as defined by the Object class, equals() performs a shallow address comparison. It says that two objects are only the same if they live at the same memory location, that is, if the variables that hold references to them are aliases. Our 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 contained in the object, you must override it with a definition specific to the class. In this case, it makes sense to say that two players are "equal" (the same player) if they are on the same team and have the same uniform number (their names might differ if one name is actually a nickname or other variant of the player’s official name).

Use this strategy to define a new equals() method for the Player class. Note that the header for equals() (which you are overriding) MUST be:

public boolean equals(Object o)

This means that Player’s version of equals() must first cast its Object parameter to a Player before returning true or false based on whether that Player object’s team and uniform numbers match those of the current Player’s corresponding instance variables.

Finally, test your ComparePlayers program using your modified Player class. It should now give the results you would expect.

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

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 setName(String name)
{
}

public void setTeam (String team)
{
}

public void setNumber(int number)
{
}

}
//
// ComparePlayers
//
// Reads in two Player objects and tells whether they represent
// the same player.
//

import java. util.*;

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

//Prompt for and read in information for player 2

//Compare player1 to player 2 and print a message saying
//whether they are equal

}
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 20:30
Write a program that reads the file, then displays the average number of steps taken for each month. (the data is from a year that was not a leap year, so february has 28 days.) your program needs to use at least 3 functions not counting main and display the information in a neat well formatted fashion.
Answers: 3
question
Computers and Technology, 21.06.2019 21:00
In this lab, you add a loop and the statements that make up the loop body to a c++ program that is provided. when completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. your loop should execute until the user enters the character x instead of l for left-handed or r for right-handed. the inputs for this program are as follows: r, r, r, l, l, l, r, l, r, r, l, x variables have been declared for you, and the input and output statements have been written.
Answers: 3
question
Computers and Technology, 22.06.2019 15:30
Why would a programmer use the logical operator and in an if statement? a: when an action is to be taken that requires both conditions to be falseb: when an action is to be taken that requires both conditions to be truec: when an action is to be taken that requires the first condition to be falsed: when an action is to be taken that requires the second condition to be truei took the test and the answer is b.
Answers: 3
question
Computers and Technology, 23.06.2019 07:00
Why is investing in a mutual fund less risky than investing in a particular company's stock? a. mutual funds only invest in blue-chip stocks. b. investments in mutual funds are more liquid. c. mutual funds hold a diversified portfolio of stocks. d. investments in mutual funds offer a higher rate of return.
Answers: 2
You know the right answer?
The Player. java file defines a class that holds information about an athlete: name, team, and unifo...
Questions