Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.

Similar presentations


Presentation on theme: "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3."— Presentation transcript:

1 1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3

2 2 polymorphism is ubiquitous (everywhere) in OOP there are many uses and examples of it let’s now build toward another example of polymorphism –but first, as last time, we need some setup…

3 3 one object controlling others (overseeing the movement of others) we now want a MoveChoreographer class, which, when constructed, is passed 3 friends (robots) the MoveChoreographer has one method called, moveFriends() which, when invoked, “moves” each friend once this Choreographer model of problem solving, by the way, can been seen in the “general contractor” analogy we used in the ppt from Ch. 3 - the general contractor doesn’t really do the work, she just delegates it to other objects

4 4 public class MoveChoreographer extends UrRobot { private UrRobot friendA; private UrRobot friendB; private UrRobot friendC; // constructor on next slide // other methods } instance variables objects not only do things (behavior), they can also remember things (state) using instance variables

5 5 public MoveChoreographer (int st, int av, Direction dir, int numBeepers, UrRobot botA, UrRobot botB, UrRobot botC ) { super (st, av, dir, numBeepers); // must come first in method friendA = botA; friendB = botB; friendC = botC; } instance variables being assigned

6 6 public void moveFriends() { friendA.move(); friendB.move(); friendC.move(); }

7 7 public class MoveChoreographer extends UrRobot { private UrRobot friendA; private UrRobot friendB; private UrRobot friendC; public MoveChoreographer (int st, int av, Direction dir, int numBeepers, UrRobot botA, UrRobot botB, UrRobot botC ) { super (st, av, dir, numBeepers); // must come first in method friendA = botA; friendB = botB; friendC = botC; } public void moveFriends() { friendA.move(); friendB.move(); friendC.move(); }

8 8 Can you now give some sample client code that uses a MoveChoreographer object? Pass a MileWalker, DropBeeperAnd Walker and a BackardWalker to the MoveChoreographer object (do so now for 5 minutes…) an example: UrRobot bot1 = new MileWalker(2, 4, North, 0) ; UrRobot bot2 = new DropBeeperAndWalker(2, 5, North, infinity); UrRobot bot3 = new BackwardWalker(2, 6, North, 0); MoveChoreographer chor; chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); chor.moveFriends(); draw a picture and show the before and after

9 9 The statement from the previous slide, chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); is kind of neat. When someone constructs a MoveChoreographer, he can pass any 3 robots in any order as long as each one is-A UrRobot (it extends from a UrRobot). The MoveChoreographer only wants to be guaranteed that it can perform a move() on any object passed to it - since there is a move() in UrRobot, it chose to make its parameters of type UrRobot, guaranteeing (to itself and the compiler) that it will be able to call move() at run-time. The term that describes which particular move() will be called at run-time is ____________. Polymorphism

10 10 Initial SituationEnd Situation Create a Gardener robot that will use GardenerHelper robots to help plant a garden. The Gardener class will have a constructor method similar to the MoveChoreographer robot that we created in class. Gardener’s specialty is to supervise 3 other GardenerHelper robots to plant beepers. You should program the GardenerHelper class to plant beepers around the plus-shaped wall. The GardenerHelper is a BetterRobot and the Gardener robot is a GardenerHelper. The Gardener robot should be programmed to use itself and the 3 other robots that are passed to it to plant each corner. The client program should pass the helper robots to the Gardener robot in the correct street/avenue that they should start on and facing in the correct direction. gardenerWorld.txt


Download ppt "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3."

Similar presentations


Ads by Google