In: Computer Science
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.
// *********************************************************
// 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
}
}
//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: