Homework 1 - Data definitions and Methods CS 2102 - Dterm 11 Homework 1 - Data definitions and Methods in Java Due: Tuesday, March 22 at 5pm Assignment Goals To design hierarchical data in Java using interfaces and classes To design methods in Java that operate over binary trees To become familiar with Java's method dispatch Problems 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 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?))
Data defintions Develop Java class and interface definitions that correspond to the 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. Methods 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). Grading Here is the general grading rubric that will be used for all assignments in this course. There will be specific additional requirements for each particular assignment. What to Turn In Using web-based turnin, 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 single file containing all classes and interfaces, or, if you organized your homework as a project, a separate file for each class/interface (standard for Java).