; Poly - PowerPoint
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Poly - PowerPoint

VIEWS: 23 PAGES: 52

  • pg 1
									Helping Students “See”
     Polymorphism
               Don Slater
  Lecturer, Carnegie Mellon University
            dslater@andrew.cmu.edu
        http://www.cs.cmu.edu/~djslater
      Polymorphism


Means “many shapes”
Sending the same message to different
objects, and having them behave
differently
Spiral 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
Using 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
Karel 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
  Robot 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
  A 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
  Robot 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()
   Two 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
       RacerRobot
Demonstration of polymorphism with
abstract classes




                               SteepleChase

                               HighHurdles

                                     Hurdles
                                     Sprint
    Similar, but not
    Identical Tasks

Each robot races to the finish line, a
beeper
   runRace()
But different obstacles may be
encountered along the way
   raceStride()
      RacerRobot
     Abstract Class


Implement the runRace() method
Specify the abstract method raceStride()
method
Sprinter 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
   RacerInterface

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
Turtle 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()
Ready to Race
An 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
The 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 prices

Owen Astrachan
“A Computer Science Tapestry”, Chapter 7
McGraw 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
    RandomWalker

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
RandomWalker
2-D Random Walker


Now the robot may move North and South
as well as East and West
Still counts steps and reports distance
2D Random Walker
2-D RandomWalker

We want to track multiple robots in two-
dimensional space
The Karel J Robot simulator has some
weaknesses in doing this
   cannot differentiate between robots
   cannot remove a robot once it is on the
   screen
Multiple Robots in
  Karel’s World
2-D RandomWalker


A better Environment for tracking
randomly moving objects exists in the
Marine Biology Simulation of the Advanced
Placement Computer Science Exam
     Marine Biology
       Simulation

Developed by Alyce Brady, Chris Nevison,
and Julie Zelinski
A simulation using multiple interacting
classes facilitating the study of a marine
environment
   For the current exam there is a C++
   version
   We will look at the Java version
Multiple Fish in MBS
    Environment
2-D RandomWalker


We want to turn our Walker Robot into a
Fishbot and move it the Marine Biology
Simulation
Walker in The MBS
   Environment
Extending from Fish

What does
inheritanc
e get us?
DarterFis
h will be a
model for
FishBot
      Karel 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
 Building 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 (fortuitous)
  Now we have a Color, but no beepers
 the 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?
     Walker 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 robot’s path is clear, it will then
   change its position
        Fish 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
Why 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
         However...


For our purposes, (demonstrating
polymorphism, you thought I forgot) we will
assume a 2D world
The Environment provides us methods we
can work with
       FishBot c’tor

Will need three different c’tors 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 c’tor looks virtually
the same
FishBot 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
     Fishbot move()

Get’s 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
             Finally


Some changes must be made to the
MBSGUI class
  drives the simulation
  add a FishBot to the list of fish in the
  simulation
  What’s 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
            Credits

Graphics - screen shots from
   KarelJRobot simulator
   BlueJ
DarterFish class diagram copied from
Alyce Brady’s web site
Sprinter classes derived from Karel++
examples and problems
Documentation and
References - Karel

Joe Bergin’s Karel website
  http://csis.pace.edu/~bergin/KarelJava2ed/Karel++JavaEdition.html
There are other excellent Karel
implementations, simulators, and resources
   A Google search on “Karel the Robot”
   returns about 13,700 hits
   I will list some at my website after the
   conference
Documentation and
 References - MBS

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
Documentation and
 References - MBS

Alyce Brady’s 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 - APCS Chief Reader - the
Unofficial APCS website
  http://cs.colgate.edu/APCS/Java/MBScasestudy/MBSca
  sestudy.htm -- under construction
  Materials from an on-line training program he conducted
  last year

								
To top