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.

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."— 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

2 2 Be efficient! starts off facing East Design a robot class (called StairClimber, method called climbStair) that would be conducive to solving the following diagrammed situation (robot should climb and pick up all beepers – always 3 beepers/stair) Also, different clients need to be able to climb different numbers of stairs: When designing, keep in mind everything we’ve been discussing.

3 3 You want to do something that is not already defined in another class And you also might want to use functionality already programmed in other classes. If so, use inheritance. Why reinvent the wheel? Use Abstraction when designing your class – 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

4 4 You get stuff for free! You can be lazy (but smart)! Use things without knowing/caring how they work! Localize changes to one class/method (encapsulation – data and methods are in the same class) StairClimberMileWalkerHarvesterDiamondPlanter BetterRobot UrRobot

5 5 Remember MileWalker? Suppose a MileWalker can only move one mile. Every time you send it the move() command, you want it to move a mile. How would you do that? In this case you can redefine what the move() method does.

6 6 import kareltherobot.*; public class MileWalker extends UrRobot { public MileWalker ( int st, int av, Direction dir, int beeps ) { super(st, av, dir, beeps); } public void move( ) { super.move(); } } The move() method is being redefined. super.move() tells the robot to perform the move() method from the superclass (in this case UrRobot). This is called Polymorphism and we will look at this further in the next chapter.

7 7 How many times does each robot below move? public class MysteryBot1 extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } public class MysteryBot2 extends MysteryBot1 { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } MysteryBot1 joann = new MysteryBot1(10, 10, North, 0); joann.step1(); // where is the robot now? MysteryBot2 bobby = new MysteryBot2(10, 10, North, 0); bobby.step1(); // where is the robot now? (12, 10) facing North (14, 10) facing North

8 8 Give the state (location and Direction) of each robot below public class ABetterBot extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void step2() { turnLeft(); } public class AnEvenBetterBot extends ABetterBot { /* constructor not shown */ public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } } ABetterBot ucla = new ABetterBot(10, 10, North, 0); ucla.step1(); // where is the robot now? AnEvenBetterBot usc = new AnEvenBetterBot(10, 10, North, 0); usc.step1(); // where is the robot now? (11, 10) facing North (12, 10) facing South

9 9 Give the state (location and Direction) of each robot below public class StepperBot extends UrRobot { /* constructor not shown */ public void step1() { move(); step2(); } public void step2() { turnLeft(); } } public class MoreStepperBot extends StepperBot { /* constructor not shown */ public void step1() { move(); super.step1(); } public void step2() { turnLeft(); super.step2(); } } StepperBot ucla = new StepperBot(10, 10, North, 0); ucla.step1(); // where is the robot now? MoreStepperBot usc = new MoreStepperBot(10, 10, North, 0); usc.step1(); // where is the robot now? (11, 10) facing West (12, 10) facing South Since step1() was called from MoreStepperBot, it looks for step2() in MoreStepperBot first

10 10 “A” is-a Letter Letter is-a Symbol Semicolon is-a PunctuationMark “@’ is-a Symbol PunctuationMark is-a Symbol LetterRobot is-a BetterRobot BetterRobot is-a UrRobot Now, split into teams and arrange the objects in the proper inheritance hierarchy

11 11 Symbol LetterRobot “@” Semicolon PunctuationMark “A” BetterBot UrRobot Letter


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

Similar presentations


Ads by Google