CSC 171
Homework 5
Polymorphism, Inheritance, and Lists

Assigned Apr 1, due Apr 15

For each method, be sure to follow the design recipe for methods.

For each animation, be sure to follow the design recipe for animations.

Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or Dr. Stemkoski have already written.

Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.

How to turn this in

You are encouraged to do this in teams of two students; turn in one copy of the project with both names on it (in the README file or in comments in the Java source files). Even if you do the assignment without a partner, make sure your name is on it.

  1. Recall the PlacedCircle class from homework 4.

  2. Recall the StringList data structure from Mar. 27's class. Add the following methods to it:

  3. Define an IntList data structure to represent a variable-length list of integers. This should be very similar to the StringList data structure developed in lecture. Define the following methods on it:

  4. Develop an animation that displays a bullseye pattern of black outlined circles on a white background. Each second, an additional ring is added three pixels outside the previous outer ring.

    Hint: Use an IntList as the model.

  5. Develop an animation that starts with a blank screen, and every second adds a dot at a random location. If you click on any of the dots, the game ends. (As the screen fills up, this becomes really easy!)

    Hint: Use a PosnList as the model.

  6. Back to the StringList class... Write a method tallyVotes which, given a list of Strings representing votes, produces a list of Candidates: each name that got at least one vote should appear exactly once in the list of Candidates, associated with the number of votes that name got. For example, if the votes were
    Bob, Alice, Mary, Bob, Mary, Bob
    then the answer should be something like
    new Candidate("Bob",3), new Candidate("Alice", 1), new Candidate("Mary",2)

    In the CandidateList class (which you wrote in order to do tallyVotes), add a method named sortByVotes that returns a CandidateList with the same elements, but sorted in decreasing order of vote count. So in the above example, the result of sortByVotes would be
    new Candidate("Bob",3), new Candidate("Mary",2), new Candidate("Alice",1)

  7. Extra credit: Develop an animation that starts with a blank screen. Every time you press the + button, a ball is added at a random location, moving in a random direction, and bouncing off walls when it hits them. Every time you press the - button, the most-recently-added ball (if any) is removed.

    Here's a video of the game in action. (Actually, I wrote this one in Scheme, not Java, but it's the same problem.)

  8. Extra credit: (This problem isn't particularly hard, but it's one more problem in an already-long assignment.)
    Recall that the Shape interface currently has two subclasses: PlacedCircle and PlacedRect.
    Add another subclass named CombinedShape. A CombinedShape is made up of two other Shapes: it could be two PlacedCircles, two PlacedRects, one of each, even one or two other CombinedShapes. Make sure the area, contains, perimeter, makeImage, and skinny methods work for this too.
    (For area and perimeter, assume the two shapes don't touch one another, so you can just add the areas or perimeters of the sub-Shapes. Trying to find all the intersections is much more difficult....)
    (If you got overlaps to work correctly between circles and rectangles, it's probably not hard to get it to also work with CombinedShapes.)

Grading standards

Error log:       /50
(I'm not grading on how many or how few errors you encountered, only on whether you recorded them adequately.)

Programming: To save time and get you feedback more quickly, we won't grade all of the problems, but a selection of them. Each animation requires writing a class with two or more methods; in some cases you'll also need to write a new class to be the model.

General skills:

Following directions /10
Writing contracts from word problems /10
Choosing test cases /10
Choosing names; readability /10
Coding /10
Code re-use and function composition /10

Last modified:
Stephen Bloch / sbloch@adelphi.edu