CS 2102 - B Term 18

Homework 2 - Abstract Classes and Programming with Lists

Due: Wednesday, November 7 at 5pm (One-day extension due to Election Day on November 6. Please remember to vote!)

Read the Expectations on Homework and the Homework Tips.

Assignment Goals

LinkedList API: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html

Note to those with prior Java experience: One goal of 2102 is to help everyone learn when different iteration constructs (for, while, etc) are needed for a particular problem. Style grading will check whether you are using appropriate constructs. This week, we will cover the per-element style for loop, not the for loop that uses a variable to index into elements. For full points, do not use index-based for loops on this assignment. Use a per-element style loop instead.

We will have covered the material for this assignment during the second week of classes.


Problem Statement

This week, we extend your initial classes for athletic competitions so you can practice programming with lists in Java. We also add a new kind of event so you can practice working with abstract classes.
  1. Modify SkiingResult so that the times of each lap are stored in a LinkedList instead of four separate doubles. The constructor will still take four doubles, but they will be added to a LinkedList inside the constructor.

  2. Modify pointsEarned in SkiingResult to now total the points across all laps in the list. If the list is empty, return zero points. Be sure to use loops!

  3. Another Nordic event in the Olympics is called the Mass Start event. This is similar to a marathon for runners; all the skiers assemble en masse and the first skier to cross the finish line wins. Add a class MassStartResult to your project. Like a SkiingResult, a MassStartResult has a finishing position, four laps, and the number of penalties. The pointsEarned is the total time across all four laps, and the penalties are calculated the same way as SkiingResult. Unlike a SkiingResult, a MassStartResult also has a starting position which is the first parameter in the constructor. MassStartResult should also be an IEvent.

    Create abstract classes as needed to share appropriate details between SkiingResult and MassStartResult. You may use whatever names you wish for these classes.

    Do NOT add a MassStartResult field to your Athlete or FinalResult classes. We are simply creating the ability to have a new kind of event, but the athletes in this assignment will not participate in it. Larger projects often have classes that don't get used in all scenarios.

  4. Add a name field (type String) to the Athlete class. Remember to also modify your constructor.

  5. Create a class Competition, which contains an integer indicating the (non-negative) number of ShootingRounds to be used in this competition. It should also contain a LinkedList of Athletes.

  6. Modify your ShootingResult class to now contain a list of ShootingRounds (rather than a fixed four rounds). We assume the rounds are in order (the first round went first, the second went second, etc).

  7. Modify pointsEarned in ShootingResult to now total the points across all rounds in the list. If the list is empty, return zero points. Be sure to use loops!

  8. Shooting rounds can take two forms: prone or standing. Add a boolean field to your ShootingRound class indicating whether or not the round is standing. Remember to also modify your constructor.

  9. Write a method called bestRoundByType in the ShootingResult class to return the best round in the list. The method should take a boolean as a parameter to indicate if we want the best prone (false) or the best standing (true) round. If there is a tie between the top two rounds, just return one of the rounds. If there are no rounds of the specified type, return null.

    Pay particular attention to creating a thorough set of tests for this method.

  10. Within a single competition, all of the athletes should have completed the same number of rounds in the Shooting event. Write a method in the Competition class called shootingDNF (for "did not finish"), which produces a LinkedList of the names of the Athletes in the competition whose list of ShootingRounds is less than the number of rounds stored in the Competition class. If there are no Athletes who meet these criteria, the method should return an empty linked list.

    The names of the Athletes should occur in the same order in the returned list as they were in the list within the Competition.

  11. Write a method in the Competition class called finalScoreForAthlete, which takes the name of an athlete and returns the final score that the athlete earned in the biathlon. You may assume that no two athletes have the same name. You may also assume that the athlete name given is in the competition (we'll talk about how to handle error cases later in the course).

  12. Write a method in the Competition class called anyImprovement, which takes another Competition as input and returns true if any of the athletes in "this" competition had a better final score than they had in the given competition. You may assume that both competitions have the same athletes, but the athletes may appear in different orders within the athlete lists in both competitions.

    Pay particular attention to creating a thorough set of tests for this method.

  13. Look back on your solutions to finalScoreForAthlete and anyImprovement. In hindsight, do you see any helper methods that you should have written that could have been shared over those two problems, or are you happy with how you organized the code?

    Put your answer (a couple of sentences) in a comment at the bottom of your Competition class. You do not need to write any code or rewrite either method for this question. We're just asking you to reflect on your code and tell us what changes you might have made were you to do this pair of problems again.

Support Files

Here are three files that may be helpful. You can download these directly into your project directory for this assignment.

Grading

The grading rubric for Homework 2 will not be posted.

Here are some details on what we will look for in grading this assignment:

Programs must compile in order to receive credit. If you submit a program that doesn't compile, the grader will notify you and give you one chance to resubmit within 24 hours; a penalty (25% of the total points for the assignment) will be applied as a resubmission penalty. Code that is commented out will not be graded.

What to Turn In

Submit (via InstructAssist) a single zip file (not tar, rar, 7zip, etc) containing all of your .java files that contain your classes, interfaces, and examples for this assignment. The name of the project in InstructAssist is Homework 2. Do not submit the .class files. Make sure all of your tests are in separate files from your code, as explained in the Expectations for Preparing Homework. You may put all of your other classes and interfaces either into a single file or into separate ones (as you prefer). If you have separate src and test subdirectories, you may retain that structure in your zip file.

Make sure that your test files include ONLY calls to methods that are listed in the assignment handout. If your tests include calls to other/helper methods or references to fields, your tests will fail to compile against our solution and we won't be able to auto-grade your work.