Due: Wednesday, Oct 31 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?))
Develop Java class and interface definitions that correspond to the Scheme/Racket definitions shown above.
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.
Include examples of data (in an Examples class).
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.
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.
Write a method allScoresValid on tournaments that determines whether every match in the tournament has a valid score.
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.
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.
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.
Submit .java files containing the final versions of all classes, interfaces, and examples developed for this assignment. Do not submit the .class files. Put your Examples class in its own file called Examples.java. Your may put all of your other classes and interfaces either into a single file or into separate ones (as you prefer).
This assignment will earn points towards the following learning outcomes:
Define classes and interfaces (skill)
Define correct methods (skill)
Create class hierarchy for a problem (ability)
Produce clean code (ability)
Write comprehensive test suites (ability)
Here are some details on what we will look for in grading this assignment:
Do your classes have appropriate variables and methods for the given problem?
Did you create an appropriate set of classes for the given problem?
Did you use interfaces where appropriate?
Do your methods have appropriate parameters for the given problems?
Do your methods produce the right answer?
Is your code well documented, with brief purpose statements on each method?
Is your code indented and presented in a clean, readable manner?
Are your test cases thorough, covering possible variations in the input data and exercising all of your code?
In previous years, typical submissions have lost the most points on inadequate test cases. This class takes test-case development seriously. A strong test suite indicates that you really understand both the data you are working with and the nuances of the problem. We grade your tests as closely as your code, so take time to think out your test cases and whether they really exercise both the data structure and the methods you are writing.