## Assignment 2: Extending Tournaments

Start from your Tournaments assignment from last week. Your class definitions from included tournaments and scores, distinguishing between scoring details for soccer, baseball, and tennis. This week, we extend the example to capture additional differences among these sports that arise in tournaments.

### 1Data Definitions

Different tournaments have different notions of contestants: baseball and soccer are team sports, while tennis is an individual sport (we’ll ignore doubles). Soccer and tennis both track rankings (so top-ranked competitors won’t face each other in an early tournament round). This week, we extend our model of tournaments to capture both team sports and ranked sports.

Your task is to edit your class hierarchy from last week to include baseball, soccer, and tennis contestants subject to the following constraints:

• The contestants in match-data are generalized to accommodate team and/or ranked players.

• Baseball contestants are teams.

• Soccer contestants are teams and ranked.

• Tennis contestants are ranked.

• Each team contestant has a number of players and the name of the team captain.

• Each ranked contestant has a ranking, which must be an integer larger than 0 (check for this).

• All ranked contestants support a method hasBetterRanking that consumes another ranked contestant and determines whether the first is ranked as better than the second contestant. Lower numbers represent better ranks.

### 2Valid Tournaments

Tournaments are clearly binary trees. In class, we studied different types of binary trees with different invariants. What are the invariants of tournaments?

• Tournament trees are balanced.

• If players in the tournament are ranked, no initial match is between players in the same half of the ranking (in other words, all players in the top half of the ranking compete against a player in the lower half of the ranking in the first round).

• The winner of each match advances to the next round. Put differently, for each non-initial-match node, each player won one of the two feeder matches.

Although we have not written programs to create tournaments, we could imagine wanting to test that programs that do create tournaments produce valid ones. Your task here is to create an hasInvariant method on tournaments that returns a boolean indicating whether the tournament meets all three of these invariants.

How you do this is up to you. We recommend writing separate methods per invariant, though. You will not be graded on the efficiency of hasInvariant (so multiple traversals over the tournament tree is fine). We care more about your having captured the invariants cleanly and in properly handling the fact that one invariant (on rankings) applies to only some tournaments but not others.

Extend your Examples class as appropriate for your new data definitions and methods.

Answer these questions in a text file called questions.txt.
1. Does your data definition formally capture that soccer and tennis contestants are similar in having ranks? If yes, how? If no, what change to your code would achieve that?

2. How do you enforce the requirement that a ranking must be larger than zero?

3. Does your implementation of hasBetterRanking require the two contestants to be in the same sport? How or why not? (You don’t have to change your code to achieve this – credit is based on your answering the question in accurate technical terms relative to your code.

4. How does your hasInvariant implementation handle the additional invariant on ranked tournaments without duplicating common code about all valid tournaments?

### 4What to Turn In

Submit your .java files along with a text file question.txt with your answers to the extension questions. Do not submit the .class files. You may submit either a separate file for each class/interface (standard for Java) or a single file containing all classes and interfaces.