1 Data Definitions
2 Methods
3 What to Turn In

Assignment 1: Modeling and Implementing Tournaments (Standard)

Due: Tuesday, Nov 1 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.

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, but baseball scores require different information:

    ;; a baseball-score is

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

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

    Extend your current definitions to also support baseball scores.

  3. Include examples of data (in an Examples class).

2 Methods

  1. The rules within a sport often imply that real scores satisfy certain constraints. For example, baseball games require at least 9 innings and may not end in a tie. The type of a baseball score (three numbers) isn’t rich enough to capture these constraints. We therefore want to write a function to check that scores are valid (by the rules of the corresponding sport).

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

    • Soccer scores: If the two teams have the same number of goals, extra time had to have been played.

    • Baseball scores: at least 9 innings must have been played and the two teams may not have the same number of runs.

  2. Your code should require every score (even those for other sports that we might add later) to have an isValid method. Make sure your code satisfies this requirement and provide a sentence (as a comment) indicating how you have done this.

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

  4. Just as games have rules that restrict valid scores, tournaments also have a notion of validity. For example, in most tournaments, the players in each round advanced from the previous round (in other words, you can’t make it into the final match without having played in the semi-finals, etc). Write a method playersAlwaysAdvanced on tournaments that produces a boolean indicating whether each contestant in an advance match played in one of the feeder matches.

  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. Answer with a couple of sentences in a comment: Did your matchesPlayed method assume a valid tournament in which players advanced from feeder matches? If yes, explain where you used that assumption and how you checked for it. If no, explain where you could have used that assumption and where you would have checked for it. You do not need to rewrite your code – just answer based on your matchedPlayed implementation.

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.