Lab 6: Collections - Sets and Maps

Copyright RIT 2009
$Id: writeup.xml,v 1.14 2009/10/10 15:42:57 vcss232 Exp $


Goal

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.


Team Setup

You are to work on this lab completely on your own.

Overview

The Problem

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.

Pre - Lab Work

  1. Read the java documentation for the classes Collection, Set, TreeSet, Map, HashMap, and Iterator.

  2. Review your class notes on Java Collections Framework.

  3. Review last week's lab.

  4. 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-Lab Activities

Activity #1 : MLB home run champions

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.

How To Submit

When you are convinced that everything is working correctly, submit your code as follows:

try grd-232 lab6-1 Player.java

Activity #2 : Adding players to a set

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:

java BaseballSet datafile

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.


1997    Ken Griffey	Seattle Mariners        56
1998    Ken Griffey	Seattle Mariners        56
1999    Ken Griffey	Seattle Mariners        48
2000    Troy Glaus     	Anahiem Angels  	47
2001    Alex Rodriguez 	Texas Rangers   	52
2002    Alex Rodriguez 	Texas Rangers   	57
            

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.


Texas Rangers's Alex Rodriguez hit 52 home runs in 2001.
Seattle Mariners's Ken Griffey hit 56 home runs in 1997.
Anahiem Angels's Troy Glaus hit 47 home runs in 2000.
            

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).

How To Submit

When you are convinced that everything is working correctly, submit your code as follows:

try grd-232 lab6-2 BaseballSet.java

Activity #3 : Using a map to retrieve player information

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:

java BaseballMap datafile

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:


get year
put year player-firstname player-lastname team-region team-nickname homerun
remove year
list
	    

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:


% more datafile
1994    Ken Griffey     Seattle Mariners        40
1992    Juan Gonzalez   Texas Rangers           43
1918    Babe Ruth       Boston RedSox           11
1937    Joe DiMaggio    NewYork Yankees         46
1908    Sam Crawford    Detroit Tigers          7
1953    Al Rosen        Cleveland Indians       43
            

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.


% java BaseballMap datafile
Detroit Tigers's Sam Crawford hit 7 home runs in 1908.
Texas Rangers's Juan Gonzalez hit 43 home runs in 1992.
NewYork Yankees's Joe DiMaggio hit 46 home runs in 1937.
Seattle Mariners's Ken Griffey hit 40 home runs in 1994.
Cleveland Indians's Al Rosen hit 43 home runs in 1953.
Boston RedSox's Babe Ruth hit 11 home runs in 1918.
>get 1994
Seattle Mariners's Ken Griffey hit 40 home runs in 1994.
>get 1900
No entry is found.
>put 1968 Frank Howard Washington Senators 44
Washington Senators's Frank Howard in 1968 has been added.
>put 1968 Frank Howard Washington Senators 44
The key already exists in the map.
>remove 1918
Boston RedSox's Babe Ruth in 1918 has been removed.
>list
Detroit Tigers's Sam Crawford hit 7 home runs in 1908.
Texas Rangers's Juan Gonzalez hit 43 home runs in 1992.
Washington Senators's Frank Howard hit 44 home runs in 1968.
NewYork Yankees's Joe DiMaggio hit 46 home runs in 1937.
Seattle Mariners's Ken Griffey hit 40 home runs in 1994.
Cleveland Indians's Al Rosen hit 43 home runs in 1953.
>remove 1950
No entry is found.
>quit
            

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.

How To Submit

When you are convinced that everything is working correctly, submit your code as follows:

try grd-232 lab6-3 BaseballMap.java


Grade Computation

Grade Breakdown: