10 date 2008.04.21.15.00.06; author rmh3093; state Exp;
36 * This class represents a baseball player in Major League Baseball. For each
37 * player, we record the year the player won the home run champion title, the
38 * name and team of the player, and the number of home runs the player hit
43 public class Player implements Comparable<Player> {
45 private String winyear, playername, playerteam;
48 public Player(String winyear, String playername, String playerteam,
50 this.winyear = winyear;
51 this.playername = playername;
52 this.playerteam = playerteam;
57 * Get the year when the player won the title.
59 * @@return the winning year.
61 public String getYear() {
66 * Get the name of this player.
68 * @@return the name of this player.
70 public String getName() {
75 * Get the team of this player.
77 * @@return the team of this player.
79 public String getTeam() {
84 * Get the number of home runs.
86 * @@return the number of home runs.
93 * Compares two players primarily for sorting.
95 * @@param other the Player to be compared
96 * @@return returns a value less than 0 if this player's name is
97 * lexicographically less than the other player's name; returns a value
98 * greater than 0 if this player's name is lexicographically greater than
99 * the other player's name; if the two players have the same name, compare
100 * their teams; that is, <0, 0, and >0 as the team of this player is less
101 * than, equal to, or greater than the team of the other player
104 public int compareTo(Player other) {
106 if (!(playername.equals(other.getName()))) {
107 char[] thisplayer = playername.toCharArray();
108 char[] otherplayer = other.getName().toCharArray();
109 int len1 = thisplayer.length;
110 int len2 = otherplayer.length;
117 for (int i=0; i<len; i++) {
118 if (thisplayer[i] > otherplayer[i]) {
121 } else if (thisplayer[i] < otherplayer[i]) {
127 char[] thisteam = playerteam.toCharArray();
128 char[] otherteam = other.getTeam().toCharArray();
129 int len1 = thisteam.length;
130 int len2 = otherteam.length;
137 for (int i=0; i<len; i++) {
138 if (thisteam[i] > otherteam[i]) {
141 } else if (thisteam[i] < otherteam[i]) {
151 * Compare this player to the specified player. The result is true if and
152 * only if the argument is not null and is a Player object that has the
153 * same name and team as this object.
155 * @@param other the object to compare this Player against
156 * @@return true if the Player are equal; false otherwise.
158 public boolean equals(Object other) {
161 Player o = (Player)other;
162 if (playername.equals(o.getName())) {
163 if (playerteam.equals(o.getTeam())) {
172 * Get the hash code of this object. The hash code is defined as the sum
173 * of the hash code of name and the hash code of team.
175 * @@return the sum of the name and team hash code's.
177 public int hashCode() {
178 return playername.hashCode() + playerteam.hashCode();
182 * Makes a String with the name, the team, the number of home runs, and the
183 * year of this player. The format is "(team-name)'s (player-name) hit
184 * (# of home runs) home runs in (year)."
186 * @@return a string representation of this player.
188 public String toString() {
189 return playerteam + "'s " + playername + " hit " + hr +
190 " home runs in " + winyear;