Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java.

Similar presentations


Presentation on theme: "1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java."— Presentation transcript:

1 1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java

2 2 Karel J Robot-Lesson 3 NTSD-Mr. Moon Debriefing Programs HBot, Harvester, DiamondPlanter We wrote HBot, Harvester, and DiamondPlanter. We wrote a turnRight() and maybe even a turnAround() or stepBackwards() to help us in each of the 3 robot classes. Anyone want to make any comments about that? –I don’t know about you, but I found it ANNOYING to write the same thing again! Let’s look at the Inheritance Hierarchy and see if we can come up with a better solution. –Hint: We extended UrRobot with all of our new Robot classes, what could we have extended that would have been better? Did I hear you say BetterTurnerRobot

3 3 Karel J Robot-Lesson 3 NTSD-Mr. Moon Improved Object Design UrRobot BetterTurnerRobot HBot DiamondPlanter turnRight() turnAround() 1.These classes currently inherit from who? UrRobot 2.What class should they really inherit ? BetterTurnerRobot 3.What do we gain by this change? Code reuse – get turnRight, turnAround, stepBackwards for free 4.What 2 syntax changes would we need to make to the 3 robot class? extends BetterTurnerRobot instead of UrRobot Remove turnRight, turnAround methods as they will now be inherited (get for free) stepBackwards() Harvester Recall, we wrote turnRight for each of these classes For future programs, lets be sure we take advantage of BetterTurnerRobot Discuss changes to HBot, Harvester and DiamondPlanter Robot classes.

4 4 Karel J Robot-Lesson 3 NTSD-Mr. Moon Why Create a new Class (new type of Robot)? Why reinvent the wheel? You’re allowed to be lazy in my class – but you have to be smart to be lazy! Code Reuse Abstraction – free your mind from the irrelevant and work on the relevant! –Ex. If I’m going to write a program to have a robot climb stairs in several buildings, I’m going to use the StairClimber class so I can call climbStair() I can work on a bigger/better/harder problem and free my mind from the irrelevant details of taking a step and picking beepers.

5 5 Karel J Robot-Lesson 3 NTSD-Mr. Moon Why use Inheritance? You get stuff for free! You can be lazy! (what OO term is this?) –Inheritance (you get methods for free) Use things without knowing/caring how they work! (OO term ?) –Abstraction (using methods that come for free and you know what they do, but don't care or think about how it is done) Why reinvent the wheel! (OO term?) –Code Reuse Localize changes to one class/method (localization) –Ex: We could add more commands or change a command in BetterTurnerRobot. We only have to do the changes in this class, not, all of the classes. All other classes will get new change free. StairClimberHBotHarvesterDiamondPlanter BetterTurnerRobot UrRobot Our Inheritance Hierarchy

6 6 Karel J Robot-Lesson 3 NTSD-Mr. Moon Sample Inheritance Questions How many times does each Bot below move? MysteryBot1 extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } calls UrRobot's move MysteryBot2 extends MysteryBot1 { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } MysteryBot1 john = new MysteryBot1(10, 10, North, 0); john.step1(); MysterBot2 sue = new MysterBot2(10, 10, North, 0); sue.step1(); John did 2 moves (going North) he is now at (12,10) Where is the bot (john) after this code runs? Where are the bots (john) and (sue) after this code runs? John is still at (12,10). Sue did 4 moves (going North) she is now at (14,10) Notice that both robots did step1 (but results were different due to inheritance)

7 7 Karel J Robot-Lesson 3 NTSD-Mr. Moon Another Inheritance Question Give the state (Location and Direction) of each Bot below. ABetterBot extends UrRobot { public void step1() { move(); } public void step2() { turnLeft(); } } AnEvenBetterBot extends ABetterBot { public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } } AnEvenBetterBot cv = new AnEvenBetterBot(10, 10, North, 0); cv.step1(); What location is bot (elkland) at and where is it facing after this code runs? ABetterBot elkland = new ABetterBot(10, 10, North, 0); elkland.step1(); What location is bot (cv) at and where is it facing after this code runs? elkland moved north 1 block and is now at (11,10) facing north cv moved 2 blocks north, turnsLeft, turnsLeft and ends up at (12,10) facing south

8 8 Karel J Robot-Lesson 3 NTSD-Mr. Moon A Final Inheritance Question Give the state (location and Direction) of each Bot below? TestBot extends UrRobot { public void step1() { move(); step2(); } public void step2() { turnLeft(); } } HarderTestBot extends TestBot { public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } TestBot frankenstein = new TestBot(5, 5, North, 0); frankenstein.step1(); What location is bot (frankenstein) at and where is it facing after this code runs? What location is bot (cartman) at and where is it facing after this code runs? HarderTestBot cartman = new HarderTestBot(5, 5, North, 0); cartman.step1(); cartman did move, move, turnLeft, turnLeft, turnLeft. He is now at (7,5) facing east. frankenstein did move, turnLeft. He is now at (6,5) facing west.

9 9 Karel J Robot-Lesson 3 NTSD-Mr. Moon Vocabulary Review Quiz Choices (inheritance, encapsulation, abstraction, stepwise refinement, precondition, override, superclass, subclass, private, public, code reuse, cohesion, super, constructor) I am the more "general/abstract" class –superclass Hiding code details and providing easy to use interface. –encapsulation I am a method and was marked this, I can be seen by all classes (ie: a client) –public Extracting essential details about an item and ignoring unimportant details –abstraction I am a subclass and want to call a method from my parent, so I must use this? –super This is used to break a BIG task into smaller tasks –stepwise refinement I am a benefit of using inheritance (ie: you can get turnRight for free) –code reuse I need to be true before a method can be executed –precondition I am used when a new robot is built so that I can properly configure it as requested –constructor A mechanism for enhancing an existing class –inheritance I am a method and was marked this, I can not be seen by a client class –private I am method that is singular in focus (I only do 1 thing). –cohesion I am the more "specific" type of class –subclass I am the basic move method and was inherited by a subclass, but was changed to move a mile now. –override


Download ppt "1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java."

Similar presentations


Ads by Google