Lab 5: Collections - Lists and Comparators

Copyright RIT 2008
$Id: writeup.xml,v 1.12 2009/10/02 15:21:08 vcss232 Exp $


Goal

In this lab you'll develop a solution to a problem that involves creating and manipulating a list. As a part of what you need to do, you'll obtain practice navigating or iterating through a list. You'll also experiment with several ways of arranging data in your list.


Team Setup

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

Overview

Collection Classes: A Generic Approach

As you now know, Java provides programmers with an extensive general-purpose library, part of which is known as the Java Collections Framework (/usr/local/jdk/docs/technotes/guides/collections/index.html) , or JCF. In this lab, we're going to make use of some of the ideas contained in the JCF to model a common situation.

The Problem

In many sports (for example, think of basketball at the national level or bowling or darts at a local level), people often organize a tournament at the end of a season. Teams are seeded within a tournament on the basis of the number of wins during the regular season. By placing teams in some type of list on the basis of the number of wins, it becomes relatively easy to create the first-round matches for such a tournament. We select the team that is first in the list (i.e., the team with the most wins) and pair it with the team last in the list (i.e., the team with the fewest wins). If we remove the teams that had been first and last in our list, we can repeat the process to get the next pairing, and so on, until we have no more teams in the list.

Notice that we have to place some constraints on the number of teams entered in a tournament in order to have things "work out evenly" (otherwise, we'd have to deal with some teams that receive a bye in the first round of the tournament). For the purposes of this lab, we'll insist that we have the "proper" number of teams just to keep things simple.

Pre - Lab Work

  1. Read this entire document to get an idea of the problem, the nature of the solution design that we have developed, the code that will be supplied to you to use, and the code that you will write.

  2. Read the java documentation for the classes Collection, List, LinkedList, ArrayList, Collections, Iterator, Comparator, and Comparable.

  3. Review your class notes on Collections.

  4. Review last week's lab.

  5. Fetch the materials for this lab from jar file and "unjar" them into an appropriate working directory.

In-Lab Activities

Activity #1 : Create a Team class

Read the javadoc descriptions for the main classes involved in this lab; namely, Team, Tournament, and TournamentMaker. For this activity, you will only be writing the Java code to implement the Team class, but you'll be able to test your code by using the class files included in the jar file.

Create a subdirectory called act1 and place a copy of Tournament.java, the TournamentMaker.class and the datafile from the jar file. Don't forget to also create an RCS directory in here for version control. You should do all your work in this act1 directory for this activity.

The javadoc gives you much of the information you need, but we also want you to think about some aspects of what you're doing. The following "hints" may also prove helpful.

Once you have written your Team class, you can run the overall Tournament program to test it. The Tournament is run as follows:

java Tournament number < datafile

where number is a whole number representing the number of teams to be entered into a tournament and datafile represents the name of a data file that contains input information about teams. Each line in a datafile consists of the name of a team, the number of wins during the season, and a whole number between 0 and 10, representing the team's strength of schedule, with each item separated from the next by a space. For the purposes of this lab, you may assume that information contained in a data file is correct. We have provided you with one data file for 8 teams, but you should feel free to create your own data files for testing.

How To Submit

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

try grd-232 lab5-1 Team.java

Activity #2 : Create a TournamentMaker class

Reread the javadoc description of the TournamentMaker class, then write the Java code to implement the class. You'll be able to test your code by using some of the files included in the jar file.

Create a subdirectory called act2 and place copies of the appropriate files from the previous activity in this activity's working directory.

When you execute this program, the main program first checks that the number of teams is appropriate. If not, an error message is printed to standard error and the program halts. If the number is valid, then an object of type TournamentMaker is created and its make() method is invoked. Notice that we have also deliberately kept exception handling to a minimum. For example, the datafile that is referred to on the command line may not be readable. Rather than worry about all of the possibilities, we simply allow our program to throw a general exception. We've done this so as to not obscure the main purpose of the lab.

The javadoc gives you much of the information that you need to write TournamentMaker, but we also want you to think about some aspects of what you're doing. The following "hints" may also prove helpful.

The format for the team match output should be:

The first round matchups are:

Game 1 is (team) against (team)

with the winner to play the winner of game (number)

Game 2 is (team) against (team)

with the winner to play the winner of game (number)

...

If the following input file is used:


Eagles 9 7
Cardinals 3 3
Seahawks 8 6
Lions 8 5
Bears 16 5
Marlins 12 2
Chargers 9 8
Bills 5 6
	   

The output of the program would be as follows:


% java Tournament 8 < input

Tournament Maker
Read in 8 team names, number of wins, and strength of schedule

Teams may be entered in any order 

team: Eagles has: 9 wins under a schedule whose strength was rated: 7 out of 10.
team: Cardinals has: 3 wins under a schedule whose strength was rated: 3 out of 10.
team: Seahawks has: 8 wins under a schedule whose strength was rated: 6 out of 10.
team: Lions has: 8 wins under a schedule whose strength was rated: 5 out of 10.
team: Bears has: 16 wins under a schedule whose strength was rated: 5 out of 10.
team: Marlins has: 12 wins under a schedule whose strength was rated: 2 out of 10.
team: Chargers has: 9 wins under a schedule whose strength was rated: 8 out of 10.
team: Bills has: 5 wins under a schedule whose strength was rated: 6 out of 10.

team: Bears has: 16 wins under a schedule whose strength was rated: 5 out of 10.
team: Marlins has: 12 wins under a schedule whose strength was rated: 2 out of 10.
team: Chargers has: 9 wins under a schedule whose strength was rated: 8 out of 10.
team: Eagles has: 9 wins under a schedule whose strength was rated: 7 out of 10.
team: Seahawks has: 8 wins under a schedule whose strength was rated: 6 out of 10.
team: Lions has: 8 wins under a schedule whose strength was rated: 5 out of 10.
team: Bills has: 5 wins under a schedule whose strength was rated: 6 out of 10.
team: Cardinals has: 3 wins under a schedule whose strength was rated: 3 out of 10.

The first round matchups are: 
Game 1 is Bears against Cardinals
with the winner to play the winner of game 4
Game 2 is Marlins against Bills
with the winner to play the winner of game 3
Game 3 is Chargers against Lions
with the winner to play the winner of game 2
Game 4 is Eagles against Seahawks
with the winner to play the winner of game 1
	   

How To Submit

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

try grd-232 lab5-2 TournamentMaker.java

Activity #3 : Providing a Comparator

Read the javadoc description of the TeamComparator class, and then write the Java code to implement the class. You'll be able to test your code by using some some of the files included in the jar file.

Create a subdirectory called act3 and place copies of the appropriate files from the previous activity in this activity's working directory.

We admit that this seems a bit far-fetched. Rather than ordering teams based on the number of wins, we ignore the number of wins and base our rankings only on strength of schedule. (Wouldn't it be nice if students could rank courses on the basis of "hard" or "easy", with grades based only on the rankings, not exams or other course-related tasks?!) In order to make use of your comparator, you'll have to figure out how to change the code in the TournamentMaker class (hint: it should only be one line that needs to be changed).

Using the same input file from the previous activity, the output of the program would be as follows:


% java Tournament 8 < input-1.1

Tournament Maker
Read in 8 team names, number of wins, and strength of schedule

Teams may be entered in any order 

team: Eagles has: 9 wins under a schedule whose strength was rated: 7 out of 10.
team: Cardinals has: 3 wins under a schedule whose strength was rated: 3 out of 10.
team: Seahawks has: 8 wins under a schedule whose strength was rated: 6 out of 10.
team: Lions has: 8 wins under a schedule whose strength was rated: 5 out of 10.
team: Bears has: 16 wins under a schedule whose strength was rated: 5 out of 10.
team: Marlins has: 12 wins under a schedule whose strength was rated: 2 out of 10.
team: Chargers has: 9 wins under a schedule whose strength was rated: 8 out of 10.
team: Bills has: 5 wins under a schedule whose strength was rated: 6 out of 10.

team: Chargers has: 9 wins under a schedule whose strength was rated: 8 out of 10.
team: Eagles has: 9 wins under a schedule whose strength was rated: 7 out of 10.
team: Seahawks has: 8 wins under a schedule whose strength was rated: 6 out of 10.
team: Bills has: 5 wins under a schedule whose strength was rated: 6 out of 10.
team: Lions has: 8 wins under a schedule whose strength was rated: 5 out of 10.
team: Bears has: 16 wins under a schedule whose strength was rated: 5 out of 10.
team: Cardinals has: 3 wins under a schedule whose strength was rated: 3 out of 10.
team: Marlins has: 12 wins under a schedule whose strength was rated: 2 out of 10.

The first round matchups are: 
Game 1 is Chargers against Marlins
with the winner to play the winner of game 4
Game 2 is Eagles against Cardinals
with the winner to play the winner of game 3
Game 3 is Seahawks against Bears
with the winner to play the winner of game 2
Game 4 is Bills against Lions
with the winner to play the winner of game 1
	    

How To Submit

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

try grd-232 lab5-3 TeamComparator.java TournamentMaker.java


Grade Computation

Grade Breakdown: