Copyright RIT 2009
$Id: writeup.xml,v 1.14 2009/10/10 15:42:57 vcss232 Exp $
In this lab you will learn how to use Java's set and map data structures. A set is used to store a group of unique items (objects), and implements standard set-theoretic operations such as union, intersection, and set difference. Maps organize a group of values (e.g. objects) by associating each with a unique key. A map may also be represented as a set of (key, value) pairs in which all keys are unique.
You are to work on this lab completely on your own.
In Major League Baseball (MLB), the players with the most home runs are recognized as home run champions each season in both the Ameriean League and National League. For example, in 2007, New York Yankee's Alex Rodriguez was the home run champion of the American League with 54 home runs. In some years, more than one player was recognized if they hit the same highest number of home runs. Do you know how many players became a home run champion at least once in MLB, and who they are? Who was the home run champion of the American League in 1988? Moreover, how many home runs did he hit, and for which team did he play? Is there a way to retrieve such information quickly from a long list of all the home run champions?
In this lab, we will use Set and Map in Java Collections Framework (/usr/local/jdk/docs/technotes/guides/collections/index.html) to answer these questions. By the way, Jose Canseco in Oakland Athletics was the American League home run champion in 1988 with 42 home runs. See List of MLB home run champions (http://en.wikipedia.org/wiki/List_of_Major_League_Baseball_home_run_champions) for more details.
Read the java documentation for the classes Collection, Set, TreeSet, Map, HashMap, and Iterator.
Review your class notes on Java Collections Framework.
Review last week's lab.
Test your knowledge of Java Collections using this self test (http://liang.armstrong.edu:8084/selftest/selftest6e?chapter=22) from Daniel Liang. You may answer as many questions as you like, and then get feedback on your answers immediately (repeat as desired).
In the first activity, you are to create the Player class that contains the information about a home run champion in Major League Baseball, namely, the year when a player won the home run champion title, the name of the player, the team of the player, and the number of home runs. For simplicity, we use only a firstname and a lastname for a player name, e.g., Ken Griffey instead of Ken Griffey Jr. A team name consists of the team's region and its nickname where both the region and the nickname should be a single token, e.g., NewYork Yankees and Boston RedSox. Refer to the javadoc document of Player for more details.
As discussed in the javadoc, two players are
considered equal if their names are identical and they
belong to the same team. The equals() and
hashCode() methods should be written to
conform to this rule. Note that the hashCode()
method needs to be overridden whenever the equals()
method is overridden in order to maintain the general
contract of hashCode -- if two objects are
equal according to the equals(Object) method,
then calling the hashCode method on each of
the two objects must produce the same integer result.
It is also a general rule that if two objects are equal,
then the compareTo() method should return 0.
For more details, see the javadoc documents for
Object and
Comparable.
The format for the toString() method is
"(team-name)'s (player-name) hit (# of home runs) home runs in (year)."
You can download two input files for testing -- Auxiliary/AL.txt and Auxiliary/NL.txt. Test your program fully before you submit.
When you are convinced that everything is working correctly, submit your code as follows:
try grd-232 lab6-1 Player.java
In this activity, you will be writing a Java program,
BaseballSet.java, that
reads a list of Major League Baseball home run champions,
removes duplicates, and displays player information in a sorted order.
You can do this using the TreeSet class, which implements the
Set interface
using a sorted data structure. In a TreeSet, set elements may be sorted using
the compareTo() method for the element type (in our case, Player).
You are free to write your BaseballSet
class however you wish, but it must minimally have a
main method. Run your program as follows:
|
where datafile is an input file. Each line in the input file consists of a year, a player, the team of the player, and the number of home runs during that year as illustrated below. You may assume that the command line arguments are correct and the input file exists.
|
Your output displays player information without duplicates
and in the order that the compareTo() method defines.
For example, executing the program on the previous input file
yields the following output.
|
If duplicates are encountered, the initial entry remains and all the subsequent duplicates are not added to the set. You may assume that the input files are correctly formatted. However, if an exception is thrown while opening or processing the file, print the exception message to standard error and gracefully terminate your program (do not use System.exit).
When you are convinced that everything is working correctly, submit your code as follows:
try grd-232 lab6-2 BaseballSet.java
You will now write a Java program,
BaseballMap.java,
that allows you to
retrieve the information about the home run champion for
a specified year. You may assume that there is only one
home run champion for a specified year. You may easily
store and retrieve player information using
HashMap where
year is used as the key and the values contain
Player objects.
A HashMap is a hash table
implementation of a map, in which the hashCode() value for a key is used to
determine where its associated value will be stored in a hash table; roughly
speaking, a key's hash code defines the array index where the associated value
is stored.
Just like
in the previous activity, you are free to write the
BaseballMap class however you wish, but it
must have a main method.
The program executes as follows:
|
where datafile is an input file containing player information as defined in the previous activity. You may again assume that the command line arguments are correct and the input file exists.
Once the program reads and stores the initial data into the map, the user can retrieve, add, and remove a player entry from the table. The user can also view all the players currently in the table. The commands for these operations are get, put, remove, and list. The syntax for these commands is:
|
If any other command is entered, display the message "Illegal command." on standard output. The program ends when the command quit is entered. If there is no mapping for the key provided in the get and remove commands, display the error message "No entry is found." on standard output. If the map already contains a mapping for a specified key in the put command, display the error message "The key already exists in the map." on standard output. Again, you may assume that the input files are correctly formatted.
Assume datafile has the following data:
|
Below is a sample run of the program using the datafile shown above as input. Notice the > is used as a prompt for the user. Notice also that the contents of the map are printed to standard output prior to displaying the prompt the first time.
|
You should display the player information in the order that it is stored within the map. Note that this
ordering will be defined using the hashCode() method (and not compareTo()), and may use hashCode()
values arbitrarily (even having the order change over time).
Keep in mind that because
the order is not guaranteed, it may not always
print out in the same order as the example above. Even
though you have a hashCode() method
defined for a Player, a HashMap
can use any ordering scheme it wants, and that ordering
is allowed to change over time.
When you are convinced that everything is working correctly, submit your code as follows:
try grd-232 lab6-3 BaseballMap.java
Grade Breakdown: