Apr 08: Polymorphic Design
Learning Objectives
After today’s class, you should be able to:
- Explain when to use an abstract class and when to use an interface.
- Identify compatibility between a variable’s type and an object’s type.
- Design a hierarchy of classes (in UML) based on a text description.
Quick Review
Design Activity
- Activity 15: Design Workshop
- If you are absent, bring a hand-drawn UML diagram on Friday
- The old version below can be used instead, if you started early
Old Version (Frogger)
Frogger is a classic 2D sprite-based video game from the 1980’s. A “sprite” is a small image which may be moved around the screen. The goal for this exercise is to design an appropriate class hierarchy for implementing Frogger in Java.
Playing Frogger
You can play the original game online: Frogger Classic
A modern version (with sound!) is here: Happy Hopper
Instructions
-
Carefully read the game specification below, underlining words that might correspond to classes in your finished hierarchy.
-
Brainstorm a class hierarchy that would allow you to implement the game logic with minimal code duplication. Look for opportunities to take advantage of polymorphism and dynamic binding. Make use of interfaces and abstract classes where appropriate.
-
Draw a “syntactically correct” UML diagram representing your design. You should include all public and protected methods, including constructors, as well as all instance variables.
Your completed design will be evaluated according to the following criteria:
- Design supports required functionality.
- UML is visually organized and neatly drawn.
- Correct UML syntax.
- Appropriate names.
- Names are meaningful.
- Method names are verbs and instance variables and classes are nouns.
- Plural nouns are only used for collections.
- Appropriate use of inheritance.
- Superclasses are used to prevent code duplication.
- Subclasses meaningfully extend the functionality of superclasses.
- Instance variables are not shadowed in subclasses.
- Classes and methods are abstract where appropriate.
- Appropriate use of polymorphism.
Note
You may assume that you have access to image files for each of the game entities and the background. You may also assume that you have access to a drawing library that provides methods like the following:
drawImage(ImageObject image, double xPosition, double yPosition)
Specification
The following screenshot and description of game play were originally copied from the Frogger Wikipedia page.
The game starts with three, five, or seven frogs (lives), depending on the settings used by the operator. The player guides a frog which starts at the bottom of the screen, to its home in one of 5 slots at the top of the screen.
The lower half of the screen contains a road with motor vehicles, which in various versions include cars, trucks, buses, dune buggies, bulldozers, vans, taxis, bicyclists, and/or motorcycles, speeding along it horizontally. The upper half of the screen consists of a river with logs, alligators, and turtles, all moving horizontally across the screen. The very top of the screen contains five “frog homes” which are the destinations for each frog. Every level is timed; the player must act quickly to finish each level before the time expires.
The only player control is the 4-direction joystick used to navigate the frog; each push in a direction causes the frog to hop once in that direction. On the bottom half of the screen, the player must successfully guide the frog between opposing lanes of trucks, cars, and other vehicles, to avoid becoming roadkill.
The middle of the screen, after the road, contains a median where the player may prepare to navigate the river.
By jumping on swiftly moving logs and the backs of turtles and alligators except the alligator jaws, the player can guide the frog safely to one of the empty lily pads. The player must avoid alligators sticking out at one of the five “frog homes”, snakes, and otters in the river, but may catch bugs or escort a lady frog for bonuses. When all five frogs are directed home, the game progresses to the next level with an increased difficulty.