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?))
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 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.
Include examples of data (in an Examples class) and templates for each class in your final class hierarchy.
Tennis distinguishes between men’s and women’s matches. Add this distinction to your data definition.
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.
Write a method Winner on scores that consumes the names of the two contestants (in order) and returns whichever one won that match.
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.
Edit your score interface (if necessary) to require the Winner and isValid methods on all types of scores.
Write a method allValid on tournaments that determines whether every match in the tournament has a valid score.
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.
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).
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:
The maximum number of sets is 3 for women, 5 for men.
Within each set, each player played at most 7 games. If one player has 7 games, the other player must have 5 or 6 games. Otherwise, one player must have 6 games and the other must have no more than 4 games.
The contestants played only as many sets as needed to determine the winner. For example, if the same contestant in a women’s match won the first two sets, the third match was not played (has no score). For men, play stopped after one player won three sets.
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.
Grading will follow the standard rubric for all assignments.