5 * $Id: Player.java,v 1.1 2008/04/21 15:00:06 rmh3093 Exp rmh3093 $
8 * $Log: Player.java,v $
9 * Revision 1.1 2008/04/21 15:00:06 rmh3093
15 * This class represents a baseball player in Major League Baseball. For each
16 * player, we record the year the player won the home run champion title, the
17 * name and team of the player, and the number of home runs the player hit
22 public class Player
implements Comparable
<Player
> {
24 private String winyear
, playername
, playerteam
;
27 public Player(String winyear
, String playername
, String playerteam
,
29 this.winyear
= winyear
;
30 this.playername
= playername
;
31 this.playerteam
= playerteam
;
36 * Get the year when the player won the title.
38 * @return the winning year.
40 public String
getYear() {
45 * Get the name of this player.
47 * @return the name of this player.
49 public String
getName() {
54 * Get the team of this player.
56 * @return the team of this player.
58 public String
getTeam() {
63 * Get the number of home runs.
65 * @return the number of home runs.
72 * Compares two players primarily for sorting.
74 * @param other the Player to be compared
75 * @return returns a value less than 0 if this player's name is
76 * lexicographically less than the other player's name; returns a value
77 * greater than 0 if this player's name is lexicographically greater than
78 * the other player's name; if the two players have the same name, compare
79 * their teams; that is, <0, 0, and >0 as the team of this player is less
80 * than, equal to, or greater than the team of the other player
83 public int compareTo(Player other
) {
85 if (!(playername
.equals(other
.getName()))) {
86 char[] thisplayer
= playername
.toCharArray();
87 char[] otherplayer
= other
.getName().toCharArray();
88 int len1
= thisplayer
.length
;
89 int len2
= otherplayer
.length
;
96 for (int i
=0; i
<len
; i
++) {
97 if (thisplayer
[i
] > otherplayer
[i
]) {
100 } else if (thisplayer
[i
] < otherplayer
[i
]) {
106 char[] thisteam
= playerteam
.toCharArray();
107 char[] otherteam
= other
.getTeam().toCharArray();
108 int len1
= thisteam
.length
;
109 int len2
= otherteam
.length
;
116 for (int i
=0; i
<len
; i
++) {
117 if (thisteam
[i
] > otherteam
[i
]) {
120 } else if (thisteam
[i
] < otherteam
[i
]) {
130 * Compare this player to the specified player. The result is true if and
131 * only if the argument is not null and is a Player object that has the
132 * same name and team as this object.
134 * @param other the object to compare this Player against
135 * @return true if the Player are equal; false otherwise.
137 public boolean equals(Object other
) {
140 Player o
= (Player
)other
;
141 if (playername
.equals(o
.getName())) {
142 if (playerteam
.equals(o
.getTeam())) {
151 * Get the hash code of this object. The hash code is defined as the sum
152 * of the hash code of name and the hash code of team.
154 * @return the sum of the name and team hash code's.
156 public int hashCode() {
157 return playername
.hashCode() + playerteam
.hashCode();
161 * Makes a String with the name, the team, the number of home runs, and the
162 * year of this player. The format is "(team-name)'s (player-name) hit
163 * (# of home runs) home runs in (year)."
165 * @return a string representation of this player.
167 public String
toString() {
168 return playerteam
+ "'s " + playername
+ " hit " + hr
+
169 " home runs in " + winyear
;