Title: Don Slater
1Helping Students See Polymorphism
- Don Slater
- Lecturer, Carnegie Mellon University
- dslater_at_andrew.cmu.edu
- http//www.cs.cmu.edu/djslater
2Polymorphism
- Means many shapes
- Sending the same message to different objects,
and having them behave differently
3Spiral Down Pedagogy and Karel
- Develop mental models with multiple passes at
the material - Originally emphasized program decomposition, top
down design and control structures - Dynamic polymorphism is the key idea to Karel
and Karel J Robot - Introducing Objects with Karel J. Robot
-
- Joe Bergin, ECOOP 2000
4Using Visualization in Teaching CS
- So much in programming happens under the hood
- Experienced programmers have developed abstract
models for understanding what is going on - Inexperienced programmers need help in developing
these models
5Karel the Robot and me
- Published by Richard Pattis in 1981
- Pascal
- Joe Bergin, et. al in 1995 - 97 published Karel
- C
- Joe Bergin, 1999 (?), developed KarelJRobot
- Java
6The Robot World
7Robot Capabilities
- A Robot can move
- Forward in the direction it is facing,from corner
to corner - Turn in place
- Turn itself off
- A Robot can detect
- Walls that are 1/2 block in front of them
- Beepers on the same corner as the robot
- Other robots on the same corner with the robot
- A Robot can navigate by detecting the direction
it is facing (north, south, east, west) - A Robot can manipulate beepers
- by carrying them,
- picking them up, and putting them down,
- knowing if it is carrying any
8A Robot Task
Initial Situation
9A Robot Program
- If polymorphism is the key idea, then subclassing
has to start right away - The Robot base class is ur_Robot
- move() turnLeft()
- pickBeeper() putBeeper()
- turnOff()
- All Robots are subclasses of ur_Robot
10Robot Predicates
- The Karel J. Robot package contains a Robot class
- a sub-class of ur_Robot
- with predicate methods (return boolean)
- frontIsClear()
- nextToABeeper() nextToARobot()
anyBeepersInBeeperBag() - facingNorth() facingSouth() facingEast()
facingWest()
11Two Step Robot
- Demonstrates polymorphism through inheritance and
overwritten methods (move) - First pass at overwriting an inherited method
- Use of the super reserved word
- illustration of the IS-A relationship
- TwoStepRobot is a Robot
12RacerRobot
- Demonstration of polymorphism with abstract
classes
SteepleChase
HighHurdles
Hurdles
Sprint
13Similar, but not Identical Tasks
- Each robot races to the finish line, a beeper
- runRace()
- But different obstacles may be encountered along
the way - raceStride()
14Sprinter Class Relationships
15RacerRobot Abstract Class
- Implement the runRace() method
- Specify the abstract method raceStride() method
16Sprinter v. Hurdler
- A Sprinter race stride is substantially different
than a Hurdler race stride - raceStride is simply a move()
- subclass HurdleRobot realizes raceStride and then
specifies up(), over(), and down() as behaviors a
Hurdler robot must implement
17RacerInterface
- I have also generated an interface for RacerRobot
- RacerRobot implements raceStride in terms of
running to the beeper - If we wanted to build RacerRobots that ran for a
specified distance, instead of the finish line - then raceStride could be implemented differently
18Turtle and Hare Robots
- Let us work with a collection of racing Robots
- We will use the Racer Interface but now create a
Turtle (slow and steady) a Hare, (move when the
spirit moves it) when we realize raceStride()
19Ready to Race
20Turtle and Hare Class Relationships
21An array of Racers
- We have built an array of Racers, holding both
HareRobots and TurtleRobots - We cycle through the array, sending each Racer
the raceStride() message - Each will behave according to the nature of its
class - builds on the IS-A relationship we saw at the
beginning
22The RandomWalker
- A random walk is a model built on mathematical
and physical concepts that is used to explain how
molecules move in a closed space, or as the basis
for several mathematical models that predict
stock market pricesOwen AstrachanA Computer
Science Tapestry, Chapter 7McGraw Hill, New
York, 1997 - We can also use random walks as a way to further
our understanding of the use and development of
classes and polymorphism
23RandomWalker
- The robot randomly moves East and West around its
starting point - The robot flips a coin to determine which
direction to move - It keeps track of how many steps it has taken
- It can report its distance from origin
24RandomWalker
252-D Random Walker
- Now the robot may move North and South as well as
East and West - Still counts steps and reports distance
262D Random Walker
272-D RandomWalker
- We want to track multiple robots in
two-dimensional space - How to differentiate between multiple robots
- We can give a robot different colors
- We can change the visibility of a robot
28Multiple Robots in Karels World
292-D RandomWalker
- A better Environment for tracking randomly moving
objects exists in the Marine Biology Simulation
of the Advanced Placement Computer Science Exam
30Marine Biology Simulation
- Developed by Alyce Brady, Chris Nevison, and
Julie Zelinski - A simulation using multiple interacting classes
facilitating the study of a marine environment
31Multiple Fish in MBS Environment
322-D RandomWalker
- We want to turn our Walker Robot into a Fishbot
and move it the Marine Biology Simulation
33Walker in The MBS Environment
34Robot 2D Class Relationships
35An MBS Overview
36Extending from Fish
- What does inheritance get us?
- DarterFish will be a model for FishBot
37Karel to MBS
- By having our Robot extend Fish instead of
MyRobot we can use our Robots in the MBS
Environment with the flexibility and display
advantages it provides
38Building a FishBot
- The mechanism for dropping our FishBot into the
Environment of the MBS - By becoming a subclass of Fish we inherit an
Environment object - Instead of keeping track of streets and avenues
we need to have a Location object (also 2-D) - We still have a Direction and Color (fortuitous)
- No beepers (at present)
39the move() method
- It is also fortuitous that Robot classes and the
MBS use move() to advance the objects from one
location to the next - How hard will it be to take advantage of this to
use inheritance and polymorphism to create our
FishBot?
40Walker move()
- The robot is entirely responsible for navigating
through its world - The robot gets a random value
- The random value is used to rotate the robot to
the appropriate direction - If the robots path is clear, it will then change
its position
41Fish move()
- A fish interacts with its environment in order
to - Find out what neighboring positions are available
(the fish does get to select one of these
neighboring positions) - What direction it is facing
- Be displayed
42Why this difference?
- Robots are always going to work in a 2-D world
- In the MBS, an Environment can be implemented in
many different ways - hexagonal?
- A fish should not make assumptions about the
world it lives in
43However...
- For our purposes, (demonstrating polymorphism,
you thought I forgot) we will assume a 2D world - The Environment provides us methods we can work
with
44FishBot ctor
- Will need three different ctors to deal with
different ways MBS builds Fish - essentially cut and paste
- For parameters, change street and avenue to
Location, numBeepers to Color, and add an
environment - The actual body of the ctor looks virtually the
same
45FishBot nextLocation()
- In the Fish class, not in the Walker class
- A useful way to think about the behavior
- Most of the Walker move is here but
- instead of turning to face a different direction
we build a new location - Check for validity with Environment
46Fishbot move()
- Gets its own next location (assuming a 2D world)
- If not valid, will remain unchanged
- Tell the environment the new location
- Have the environment tell it what direction it is
facing - Updates number of steps
47Finally
- Some changes must be made to the MBSGUI class
- drives the simulation
- add a FishBot to the list of fish in the
simulation
48Whats the Point?
- A demonstration of loose coupling (?) of classes
- A demonstration of the power of inheritance
- A steppingstone into the MBS
- identifies different components of MBS
- can compare and contrast different ways to
implement a RandomWalk / MBS
49Credits
- Graphics - screen shots from
- KarelJRobot simulator
- BlueJ
- DarterFish class diagram copied from Alyce
Bradys web site - Sprinter classes derived from Karel examples
and problems
50Documentation and References - Karel
- Joe Bergins Karel website
- http//csis.pace.edu/bergin/KarelJava2ed/KarelJ
avaEdition.html - http//www.cafepress.com/kareljrobot
- There are other excellent Karel implementations,
simulators, and resources - A Google search on Karel the Robot returns
about 10,200 hits
51Documentation and References
- AP Central The official site for all Advanced
Placement course information - http//apcentral.collegeboard.com
- After registration, you have access to official
MBS materials - manuals, documentation, software
- There is access to a growing collection of
resources, many related to the MBS
52Documentation and References - MBS
- Alyce Bradys MBS web site
- http//max.cs.kzoo.edu/AP/MBS/index.html
- The object diagrams are useful, source code for
black box classes also available here - Chris Nevison - Former APCS Chief Reader - the
Unofficial APCS website - http//cs.colgate.edu/APCS/Java/MBScasestudy/MBSca
sestudy.htm - Materials from training programs he has conducted