Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.

Similar presentations


Presentation on theme: "Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3."— Presentation transcript:

1 Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3

2 3- 2 Generality The programs we’ve written up to now were intended for a given initial situation; in almost any other situation, robots will fail to accomplish their tasks We need a way for a robot to query the local environment and decide, based on current information, what to do next

3 3- 3 Into the void Have you been wondering why there is the word void before our method definitions? void turnRight { turnLeft; turnLeft; turnLeft; }

4 3- 4 Returning a Result The reason is that a method can return a result—in essence, send an object or value back Since the turnRight method doesn’t return anything, we wrote “void” in front of the definition In the robot world, methods can return “true” or “false”, which is called a “boolean”

5 3- 5 Some Methods that Return booleans boolean agreeable { return true; } boolean cross { return false; } boolean itIsStrange { turnLeft; move; return true; }

6 3- 6 The Robot World is Itself an Object! A robot world is itself an object, defined as an instance of the class BasicWorld This object has attributes and methods The object’s internal attributes are the positions of walls and beepers The internal methods return “true” or “false” about the location of walls and beepers We don’t see the inside of the object, nor do we need to (or care to)

7 3- 7 The Robot World is Itself an Object! We don’t see the inside of the object, nor do we need to (or care to) All that matters is the object’s interface – the “controls” between it and the world

8 3- 8 Class BasicWorld boolean frontIsClear boolean leftIsClear boolean rightIsClear boolean nextToABeeper BasicWorld wall-locations beeper-locations

9 3- 9 Class BasicWorld We do not know how these methods are implemented We do not need to know how these methods are implemented If we have an instance of BasicWorld called myworld, we can send it a message myworld.frontIsClear(x, y, direction) and get back “true” or “false” (where x and y are integers, direction is East, North, South, or West)

10 3- 10 I Forgot... :) BasicRobot also has some boolean methods defined... void move void turnLeft void pickBeeper void putBeeper void turnOff BasicRobot x-location y-location direction num-beepers boolean facingNorth boolean facingSouth boolean facingEast boolean facingWest boolean anyBeepersInBag

11 3- 11 One more thing... BasicRobot also has some integer and a direction method defined... void move void turnLeft void pickBeeper void putBeeper void turnOff BasicRobot x-location y-location direction num-beepers boolean facingNorth boolean facingSouth boolean facingEast boolean facingWest boolean anyBeepersInBag int xLoc int yLoc Direct facing

12 3- 12 What is Visible, What Isn’t We can access an object’s methods, but not its private attributes boolean frontIsClear boolean leftIsClear boolean rightIsClear boolean nextToABeeper BasicWorld wall-locations beeper-locations Can be sent messages Invisible } }

13 3- 13 Similarly void move void turnLeft void pickBeeper void putBeeper void turnOff BasicRobot x-location y-location direction num-beepers boolean facingNorth boolean facingSouth boolean facingEast boolean facingWest boolean anyBeepersInBag int xLoc int yLoc Direct facing Invisible } Can be sent messages }

14 3- 14 One more thing...! (didn’t I say that before?) Sometimes it’s useful to have the opposite of a boolean We use the symbol “!” to reverse a boolean So if myworld.frontIsClear(3, 4, East) returns “true”, then ! myworld.frontIsClear(3, 4, East) returns “false”

15 3- 15 Putting it Together A robot created from BasicRobot or one of BasicRobot’s extended classes can be sent messages, and it will tell us its x and y locations, and its direction A world created from the BasicWorld class (or one of its extensions) can be sent x and y locations and directions, and return answers to things like frontIsClear

16 3- 16 An Example I’ve got a robot called bill at x=2, y=1, facing N I have a world called myworld (for simplicity) Is bill’s front clear? myworld.frontIsClear (bill.xLoc, bill.yLoc, bill.facing); The answer from myworld is “true” bill myworld

17 3- 17 “if” instruction if ( ) ; A new reserved word, “if” The is called the then clause of the if instruction The controller or robot executes by first checking if is “true” or “false” If true, is executed; if false, is ignored

18 3- 18 Example of Program Fragment if ( myworld.nextToABeeper(bill.xLoc, bill.yLoc) ) bill.pickBeeper; bill.move; First, bill is sent two messages to get its x and y locations This information is then sent in a message to myworld’s nextToABeeper method, which returns true or false

19 3- 19 Program Fragment continued if ( myworld.nextToABeeper(bill.xLoc, bill.yLoc) ) bill.pickBeeper; bill.move; If true, the Controller executes the “then” clause, and sends bill a pickBeeper message; then bill is sent the move message If false, the Controller skips the “then” clause, and just sends bill the move message

20 3- 20 Useful to think of the Objects and their Message myworld methods... bill methods... Controller yLoc xLoc nextToABeeper if ( myworld.nextToABeeper(bill.xLoc, bill.yLoc) ) bill.pickBeeper; STEP 1 STEP 2

21 3- 21 Useful to think of the Objects and their Message voidmove voidturnLeft voidpickBeeper voidputBeeper voidturnOff x-location y-location direction num-beepers booleanfacingNorth booleanfacingSouth booleanfacingEast booleanfacingWest booleananyBeepersInBag intxLoc intyLoc Direct facing bill

22 3- 22 What about Controller This all works basically the same way whether the messages are being sent by the Controller or by one object to another object So why isn’t Controller itself an object? Wait until Java...

23 3- 23 harvestAFurrow task One Possible Initial Situation — not all corners have beepers

24 3- 24 The new harvestAFurrow void harvestAFurrow { pickBeeperIfPresent; move; pickBeeperIfPresent; move; pickBeeperIfPresent; move; pickBeeperIfPresent; move; pickBeeperIfPresent; }

25 3- 25 The Beauty Is, We Can Keep Most of the Original Solution class Harvester extends BasicRobot { void turnRight {... } void positionForNext2 {... } void goToNextFurrow {... } void harvestAFurrow {... } void harvest2Furrows {... } } main { Harvester bill = new Harvester(2, 2, East, 0); bill.move; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.move; bill.turnOff; } Just add pickBeeperIfPresent

26 3- 26 Now, pickBeeperIfPresent void pickBeeperIfPresent { if ( myworld.nextToABeeper(xLoc, yLoc) ) pickBeeper; } Assume (for simplicity) that we are in myworld.

27 3- 27 turnaroundOnlyIfBlocked void turnaroundOnlyIfBlocked { if ( ! myworld.frontIsClear(xLoc, yLoc, facing) ) { turnLeft; } What’s the purpose of the inner “{ }”?

28 3- 28 Incorrect Version of the Command void turnaroundOnlyIfBlocked { if ( ! myworld.frontIsClear(xLoc, yLoc, facing) ) turnLeft; } The indentation misleads us. What does this instruction do?

29 3- 29 When Robot’s Front is Blocked It works! 12345 1 2 3 4 5 void turnaroundOnlyIfBlocked { if ( ! myworld.frontIsClear(xLoc, yLoc, facing) ) turnLeft; }

30 3- 30 When Robot’s Front is Not Blocked It doesn’t work!!! 12345 1 2 3 4 5 void turnaroundOnlyIfBlocked { if ( ! myworld.frontIsClear(xLoc, yLoc, facing) ) turnLeft; }

31 3- 31 Subtle Intent Error Incorrect instruction is syntactically correct No execution error is possible It works some of the time Uh-oh…

32 3- 32 The Moral of the Story The meaning of instructions is not always obvious Actively think of special situations where the instruction may fail to satisfy its intent — adversarial approach Rigorously simulate the robot in these situations

33 3- 33 faceNorth void faceNorth { if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; }

34 3- 34 Check the Instruction It works when robot starts facing south. What about the other three situations? void faceNorth { if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; }

35 3- 35 What if…? (1) It works when robot starts facing south. What about the other three situations? void faceNorth { if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; }

36 3- 36 What if…? (2) It works when robot starts facing south. What about the other three situations? void faceNorth { if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; if ( ! facingNorth ) turnLeft; }

37 3- 37 if/else instruction if ( ) ; else ; Similar to form of simple “if” instruction The is called the else clause of the if instruction The controller or robot executes by first checking if is “true” or “false” If true, is executed; if false, is executed (robot executes one or the other, not both)

38 3- 38 if/else can be replaced as above and can be replaced by same instructions that replaced above

39 3- 39 Mile-Long Hurdle Race 12345 1 2 3 4 5 678 6 7 8 912345 1 2 3 4 5 678 6 7 8 9 Hurdles are only one block high, but may be placed randomly. We’d rather if the robot didn’t jump up and down, but went straight where possible.

40 3- 40 The Execution Block main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; } 12345 1 2 3 4 5 678 6 7 8 9

41 3- 41 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

42 3- 42 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

43 3- 43 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

44 3- 44 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

45 3- 45 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

46 3- 46 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

47 3- 47 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

48 3- 48 The Execution Block 12345 1 2 3 4 5 678 6 7 8 9 main { Racer bill = new Racer(1, 1, East, 0); bill.raceStride; bill.turnOff; }

49 3- 49 Definition of raceStride void raceStride { if ( myworld.frontIsClear(xLoc, yLoc, facing) ) move; else jumpHurdle; } 12345 1 2 3 4 5 678 6 7 8 912345 1 2 3 4 5 678 6 7 8 9 frontIsClear is true frontIsClear is false

50 3- 50 Definition of jumpHurdle void jumpHurdle { jumpUp; move; glideDown; } 12345 1 2 3 4 5 678 6 7 8 9 Assumption: frontIsBlocked is true

51 3- 51 Definition of jumpUp void jumpUp { turnLeft; move; turnRight; } 12345 1 2 3 4 5 678 6 7 8 9

52 3- 52 12345 1 2 3 4 5 678 6 7 8 9 Definition of glideDown void glideDown { turnRight; move; turnLeft; }

53 The Complete Program class Racer extends BasicRobot { void turnRight {... } void glideDown {... } void jumpUp {... } void jumpHurdle {... } void raceStride {... } } main { Racer bill = new Racer(1, 2, East, 0); bill.raceStride; bill.turnOff; }

54 3- 54 Nested if Instructions An “if” instruction that is inside the “then” or “else” clause of another “if” if ( myworld.frontIsClear(...) ) bill.move; else { putBeeper; if (bill.facingNorth) bill.move; } Example: Nested if

55 3- 55 Beeper Replanting Task 2 2 2 2 Initial Situation Final Situation Either zero, one, or two beepers per corner; enough beepers in bag to finish task.

56 3- 56 The Beauty Is, We Can Keep Most of the Original Solution class Harvester extends BasicRobot { void turnRight {... } void positionForNext2 {... } void goToNextFurrow {... } void harvestAFurrow {... } void harvest2Furrows {... } } main { Harvester bill = new Harvester(2, 2, East, 30); bill.move; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.move; bill.turnOff; } Just add new method that handles each corner right

57 3- 57 Main instruction of Solution void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else { pickBeeper; if (!myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; }

58 3- 58 Simulation on Empty Corner

59 3- 59 Simulation on Empty Corner

60 3- 60 Simulation on One-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper, then puts one down.

61 3- 61 Simulation on One-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper, then puts one down.

62 3- 62 Simulation on One-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper, then puts one down.

63 3- 63 Simulation on One-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper, then puts one down.

64 3- 64 Simulation on One-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper, then puts one down.

65 3- 65 Simulation on Two-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper. void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else { pickBeeper; if (!myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; } }

66 3- 66 Simulation on Two-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper. void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else { pickBeeper; if (!myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; } }

67 3- 67 Simulation on Two-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper. void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else { pickBeeper; if (!myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; } }

68 3- 68 Simulation on Two-beeper Corner 12345 1 2 3 4 5 678 6 7 8 2 2 2 2 Picks up beeper. void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else { pickBeeper; if (!myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; } }

69 3- 69 Breaking Up’s Not So Hard To Do void replantExactlyOneBeeper { if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; else nextToOneReplantOne; } void nextToOneReplantOne { pickBeeper; if ( !myworld.nextToABeeper(xLoc, yLoc) ) putBeeper; } Use an auxiliary method to make things clearer

70 3- 70 Can We Verify the Program? Not through complete simulation: there are 205,891,132,094,649 possible initial situations (3 30 ) But we have verified that replantExactlyOneBeeper works correctly on any corner with 0, 1, or 2 beepers We can verify that the instruction is executed on every corner of the field

71 3- 71 In General… Verify that each defined instruction works correctly in all possible situations in which it can execute Verify that the program executes each defined instruction at the appropriate time Another advantage to stepwise- refinement

72 3- 72 Transformations for Simplifying “if” Instructions Two execution equivalent instructions: if (myworld.frontIsClear(..)) move; else jumpHurdle; if (! myworld.frontIsClear(..)) jump-hurdle; else move; In general, we can turn one if/else into an execution equivalent one by: 1. Replacing with its opposite, and 2. Interchanging the “then” and “else” clauses This is called test reversal

73 3- 73 One Use for Test Reversal if ( myworld.frontIsClear(xLoc, yLoc) ) do-nothing; else ; Not legal, unless we define “do-nothing”. Instead, write the following: if ( ! myworld.frontIsClear(xLoc, yLoc) ) ;

74 3- 74 Bottom Factoring if { ; } else { ; } Two execution equivalent instructions. if ; else ; ; We have factored out from the bottom of each clause.

75 3- 75 Successful Top Factoring if (facingNorth) { move; turnLeft; } else { move; turnRight; } Two execution equivalent instructions. move; if (facingNorth) turnLeft; else turnRight; We have factored out move from the top of each clause.

76 3- 76 Unsuccessful Top Factoring if (nextToABeeper) { move; turnLeft; } else { move; turnRight; } These are NOT two execution equivalent instructions. Consider the case where the robot is standing next to a beeper, with the corner in front of him not having a beeper. move; if (nextToABeeper) turnLeft; else turnRight;

77 3- 77 When Can We Top Factor? We can top factor an instruction only when the test that the robot performs does not change between the original and factored version of the instruction. The move instruction, coming before the nextToABeeper test, changed the nature of the test. The move instruction, coming before the faceNorth test, did not change the nature of the test.

78 3- 78 Redundant-Test Factoring if (facingWest) { move; if facingWest turnLeft; } if (facingWest) { move; turnLeft; } We can remove the second test of facingWest, because the move instruction cannot change the direction the robot is facing. We cannot use this kind of factoring if the intervening instructions change or might change the outcome of the test.


Download ppt "Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3."

Similar presentations


Ads by Google