1 Data Definitions
1.1 Advanced Problems on Data Definitions
2 Methods
2.1 Advanced Problem on Methods
3 What to Turn In
4 Grading

Assignment 1: Modeling and Implementing Tournaments

Due: Tuesday, Nov 2 at 11:59pm via Turnin

Many tournaments are organized into elimination rounds, in which pairs of remaining contestants play a match and the winner advances onto the next round. In this assignment, we will model and implement programs over tournaments in Java.

Each section of this assignment has a set of problems marked Advanced Problems. We recommend these for students already experienced with Java or those wanting to push on their skills. Advanced Problems earn extra credit points that we will apply if you end up on the boundary between two course grades at the end of the course.

As a starting point, the following Scheme/Racket data definitions capture the elimination rounds of the Soccer World Cup:

;; A tournament is either

;; - (make-init-match match-data)

;; - (make-advance-match match-data tournament tournament)

(define-struct init-match (data))

(define-struct advance-match (data feeder1 feeder2))

 

;; a match-data is (make-match-data string string soccer-score)

(define-struct match-data (team1 team2 score))

 

;; a soccer-score is (make-soccer-score number number boolean)

(define-struct soccer-score (goals1 goals2 extra-time?))

1 Data Definitions

  1. Develop Java class and interface definitions that correspond to the Scheme/Racket definitions shown above.

  2. The same basic structure could capture the baseball World Series and tennis Grand Slams, but scores in these sports have a different structure:

    ;; a baseball-score is

    ;;   (make-baseball-score number number number)

    (define-struct baseball-score (runs1 runs2 total-innings))

     

    ;; a tennis-score is (make-tennis-score number number)

    (define-struct tennis-score (sets1 sets2))

    Edit your current definitions to also support baseball and tennis scores.

  3. Include examples of data (in an Examples class) and templates for each class in your final class hierarchy.

1.1 Advanced Problems on Data Definitions

  1. Tennis distinguishes between men’s and women’s matches. Add this distinction to your data definition.

  2. Real tennis scores contain details about sets, not just the number of sets that each player won. The score in each individual set indicates a number of games won by each player. Whoever wins more games wins the set. In a women’s match, the first player to win two sets wins the match (so at most 3 sets are played). In a men’s match, the first player to win three sets wins the match (so at most 5 sets are played).

    For example, a match between two women might have the score 6-2, 3-6, 6-4, indicating that the first player won the first set 6 games to 2, the second player won the second set 6 games to 3, and the first player won the third set (and hence the match) 6 games to 4.

    Change your definition of tennis scores to represent detailed sets.

2 Methods

  1. Write a method Winner on scores that consumes the names of the two contestants (in order) and returns whichever one won that match.

  2. Write a method isValid on scores that determines whether the score is valid for its corresponding sport. In particular:

    • Soccer scores: No restrictions

    • Baseball scores: at least 9 innings must have been played

    • Tennis scores: the total number of sets is at most 5, with at most 3 won by each player.

  3. Edit your score interface (if necessary) to require the Winner and isValid methods on all types of scores.

  4. Write a method allValid on tournaments that determines whether every match in the tournament has a valid score.

  5. Write a method matchesPlayed on tournaments that consumes a contestant name and produces the number of matches in the tournament in which the named contestant played.

  6. Our description of tournaments says that the winner of each match advances to the next round. Nothing in our data definitions, however, requires this: our examples could have an error in which the loser of a match (or worse, a contestant that didn’t even play in the match) advanced. Write a method winnerAlwaysAdvanced on tournaments that produces a boolean indicating whether each contestant in a match was the winner of one of the feeder matches (for initial matches, this method should produce true).

2.1 Advanced Problem on Methods

The notion of valid scores is more sophisticated once you model the details of sets. Alter your isValid method to account for the following constraints:

3 What to Turn In

Submit .java files containing the final versions of all classes, interfaces, and examples developed for this assignment. 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.

4 Grading

Grading will follow the standard rubric for all assignments.