15-121 SPRING 2010 [CORTINA]

LAB 11


In this lab, you will experiment with maps and sets in Java.

EXERCISES

Create a project in Eclipse named Lab11 with a class named MapSetTester with a main method. You will be using the HashMap and TreeSet classes, so open up the Java API so you can refer to the documentation for these classes as you work on these problems.

  1. In your main method, create a HashMap named networkMap that will store key,value pairs where the key is a TV network name and the associated value is a TreeSet that contains television shows from that network:

    HashMap<______,______> networkMap = new HashMap<______,______>();
    

  2. In a loop, ask the user for the name of a television show and then ask the user for the name of the TV network for that show. After you have the input values, find the network in the hash map.

    1. If the network is not in the hash map yet, create a tree set with the name of the television show in it, and then add the key,value pair (network, set with 1 show) to the hash map.

    2. If the network is in the hash map, add the television show to the tree set for that network.

    After you update the hash map, output the contents of the hash map (i.e. System.out.println(networkMap); ). Repeat this until 10 shows have been entered.

    Sample Output (note how the networks are not necessarily in lexicographic order, but the television shows for each network are in lexicographic order... why?):

    Input tv network: FOX
    Input tv show on FOX: The Simpsons
    {FOX=[The Simpsons]}
    
    Input tv network: NBC
    Input tv show on NBC: ER
    {FOX=[The Simpsons], NBC=[ER]}
    
    Input tv network: ABC
    Input tv show on ABC: 20/20
    {FOX=[The Simpsons], NBC=[ER], ABC=[20/20]}
    
    Input tv network: CBS
    Input tv show on CBS: Survivor
    {CBS=[Survivor], FOX=[The Simpsons], NBC=[ER], ABC=[20/20]}
    
    Input tv network: ABC
    Input tv show on ABC: Lost
    {CBS=[Survivor], FOX=[The Simpsons], NBC=[ER], ABC=[20/20, Lost]}
    
    Input tv network: FOX
    Input tv show on FOX: Family Guy
    {CBS=[Survivor], FOX=[Family Guy, The Simpsons], NBC=[ER], ABC=[20/20, Lost]}
    
    Input tv network: CBS
    Input tv show on CBS: CSI
    {CBS=[CSI, Survivor], FOX=[Family Guy, The Simpsons], NBC=[ER], ABC=[20/20, Lost]}
    
    Input tv network: FOX
    Input tv show on FOX: American Idol
    {CBS=[CSI, Survivor], FOX=[American Idol, Family Guy, The Simpsons], NBC=[ER], ABC=[20/20, Lost]}
    
    Input tv network: ABC
    Input tv show on ABC: Desperate Housewives
    {CBS=[CSI, Survivor], FOX=[American Idol, Family Guy, The Simpsons], NBC=[ER], ABC=[20/20, Desperate Housewives, Lost]}
    
    Input tv network: NBC
    Input tv show on NBC: Law And Order
    {CBS=[CSI, Survivor], FOX=[American Idol, Family Guy, The Simpsons], NBC=[ER, Law And Order], ABC=[20/20, Desperate Housewives, Lost]}
    

  3. Once you have your hash map set up, output the contents of the hash map in alphabetical order by network, one network per line.

    1. Create an array list initialized with the set of keys of the hash map. (HINT: The keySet method of the HashMap class returns a set of the keys and the ArrayList has a constructor that allows you to initialize the array list using a collection (and a set is a collection).

    2. Sort the array list. You can do this in one line.

    3. Iterate over the (sorted) array list of keys, get the corresponding value (set of tv shows) for each key from the hash map and print them out. (Note: You won't have to sort the tv shows for the network since they'll already be sorted... why?)

    Sample output:

    ABC: [20/20, Desperate Housewives, Lost]
    CBS: [CSI, Survivor]
    FOX: [American Idol, Family Guy, The Simpsons]
    NBC: [ER, Law And Order]
    

  4. Complete your tester by writing a loop that asks the user to enter a TV show and then you should use the map to output the network that maps to that tv show, if any. Repeat until the user enters an empty string.

    HINT: Iterate over each key (network) in the map, and see if its value (set of shows) contains the desired show. If so, print out the network. Otherwise, if none of the networks have that show, report "UNKNOWN". You don't have to worry about uppercase/lowercase, just match the TV show name exactly.

HANDIN

If you worked with another student, put both of your names in a comment at the beginning of your program. At the end of lab, create a zip file of your program and submit it to the handin server http://handin.intro.cs.cmu.edu/v1. (If you worked together, you only have to submit one program.)

FUTURE WORK: FUN STUFF FOR LATER