Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A class 1 (Ch3, pg 43) What are the options for harvesting these beepers? How many robots could we use? 2 (a.k.a. variables) • Teams of Robots (e.g.) – Could have 1 robot harvest 6 rows (we’ve seen that) – Could have 3 robots each harvest 2 rows like this: Harvester botA = new Harvester(2,2,…,…); botA.move(); botA.harvestTwoRows(); Harvester botB = new Harvester(4,2,…,…); botB.move(); botB.harvestTwoRows(); Harvester botC = new Harvester(6,2,…,…); botC.move(); botC.harvestTwoRows(); 3 • Could also intersperse the operations like this: // same instantiations Harvester botA = new Harvester(2,2,…,…); Harvester botB = new Harvester(4,2,…,…); Harvester botC = new Harvester(6,2,…,…); botA.move(); There are 3 botB.move(); separate robot botC.move(); references in botA.harvestTwoRows(); this example: botB.harvestTwoRows(); botA botB botC.harvestTwoRows(); botC 4 • Could just use one reference like this: bot is a reference Harvester bot; bot = new Harvester(2,2,…,…); bot.move(); bot.harvestTwoRows(); bot = new Harvester(4,2,…,…); bot.move(); bot.harvestTwoRows(); bot = new Harvester(6,2,…,…); bot.move(); bot.harvestTwoRows(); instantiating (i.e constructing) 3 separate objects we use assignment to assign a specific object to a reference 5 To correct the error, the object (bob) must be instantiated • Harvester bob; bob = new Harvester(2, 3, ..,..); bob.harvestTwoRows(); • What’s wrong with the above? • This causes a NullPointerException error – for now, an error in Java is called an exception – NullPointerException means the reference is not pointing to anything. 6 • References model what’s going on in the real world as well – There are lots of “Dave” references - but the particular object (person) one is referring to depends on context and whom one is, in particular, referring to at the moment – Well, these references are all neat and everything, but so what? Well, hold on a few more slides and you’ll see the power of using them - we’re headed toward an extremely important OO concept called Polymorphism. 7 • Powerful example of Polymorphism: – pretend you are all objects - if I tell each of you to “takeABreak()”, you all will hear the same message but will act in different ways (some of you will sleep, some will walk out the door and eat something, some will try to leave school!, some will do work, etc.) that’s polymorphism • sending the same message to different objects - each individual object has a particular way to interpret (implement) the message • so, back to code and a Java/Karel example… 8 • remember MileWalker? (Ch3, pg 34) – we named its one method moveMile() – we could have named the method move() and then redefined what “move” means to a MileWalker. Again, we’re modeling the real world. The concept of “move” is different depending on what type of object is “moving” (think about how a dog, fish, bird, etc., “move”) – so, since the general concept is the same, we often use the same name (it makes coding easy/logical) why would you want to try to remember moveMile(), moveLegs(), moveWings(), etc. - why not just one identifier for that - move() 9 SquareRobot DiagonalRightRobot DiagonalLeftRobot Create three separate robot classes that extend BetterRobot. Each of these robots move differently when sent the move() message. 10