Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.

Similar presentations


Presentation on theme: "Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps."— Presentation transcript:

1 Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps

2 Why should we extend Karel’s vocabulary? Karel’s vocabulary is limited. For example, Karel does not understand a TurnRight(); command. He can only turn right by executing three TurnLeft(); commands.

3 As a second example, suppose we need to program Karel to travel over vast distances. Assume that the robot must move east one mile (a mile is eight blocks long), pick up a beeper, and then move another one mile north. Karel understands how to move a block, but not a mile. Conversion from miles to blocks is straightforward, but results in a very long and unreadable program. How many Move(); instructions would be needed to move Karel one mile?

4 Karel can “learn” new commands via our instructions Our programs can furnish him with a dictionary of useful instruction names and their definitions. Each definition must be built from simpler instructions that Karel already understands. Given this ability to extend Karel’s vocabulary, we can solve our problems using instructions natural to our way of thinking.

5 How do we instruct Karel to learn a new command? For example, suppose we want Karel to learn the new command TurnRight, then how do tell Karel to remember this information? We know that if we instruct Karel to perform three TurnLeft instructions, then we can accomplish a TurnRight.

6 Defining a new function void identifier () { statement(s) that define the function }

7 1 2 3 4 5 6 1 2 3 4 5 6 Suppose we want Karel to pickup the beeper shown in the situation file. Then what are Karel’s instructions?

8 #include int main() { TurnOn(); TurnLeft(); Move(); PickBeeper(); TurnOff(); } 1 2 3 4 5 6 1 2 3 4 5 6 The instructions for Karel to pickup the beeper shown in the situation file.

9 #include int main() { TurnOn(); TurnLeft(); Move(); PickBeeper(); TurnOff(); } 1 2 3 4 5 6 1 2 3 4 5 6 Now, lets modify Karel’s instructions so as to learn a new command, TurnRight.

10 #include void TurnRight() { TurnLeft(); } int main() { TurnOn(); TurnRight(); Move(); PickBeeper(); TurnOff(); } 1 2 3 4 5 6 1 2 3 4 5 6 Now, lets modify Karel’s instructions so as to learn a new command, TurnRight.

11 Defining a new function/command void identifier () { statement(s) that define the function }

12 Using this function definition mechanism to create a kind of dictionary for Karel, new commands can be defined to extend Karel’s vocabulary by writing functions. For example, although Karel does not have a predefined TurnRight(); command, we can user-define this instruction by writing the function shown in the previous slide and on the next slide.

13 void TurnRight() { TurnLeft(); }

14 Similarly, we can define MoveAMile(); to mean eight Move(); instructions. void MoveAMile() { Move(); }

15 Benefits of extending Karel’s vocabulary We can use instructions “natural” to us. As in the move a mile problem, the size of our programs can be significantly reduced. The smaller program is much easier to read and understand.

16 #include int main() { TurnOn(); Move(); PickBeeper(); Move(); TurnLeft(); Move(); PutBeeper(); Move(); TurnLeft(); Move(); TurnLeft(); Move(); TurnLeft(); TurnOff(); }

17 #include int main() { TurnOn(); Move(); PickBeeper(); Move(); TurnLeft(); Move(); PutBeeper(); Move(); TurnLeft(); Move(); TurnLeft(); Move(); TurnLeft(); TurnOff(); } TurnRight();

18 #include void TurnRight() { TurnLeft(); } int main() { TurnOn(); Move(); PickBeeper(); Move(); TurnLeft(); Move(); PutBeeper(); Move(); TurnRight(); Move(); TurnRight(); Move(); TurnRight(); TurnOff(); }

19 A Stair Cleaning Task Karel is supposed to climb stairs and pick up the beepers on each step. When he is done, he should be standing on the top step, facing East.

20 #include …. int main() { TurnOn(); TurnLeft(); Move(); TurnRight(); Move(); PickBeeper(); TurnLeft(); Move(); TurnRight(); …

21 If we invent a new instruction called ClimbAStep(); then the program can be written as: #include int main() { TurnOn(); ClimbAStep(); PickBeeper(); ClimbAStep(); PickBeeper(); ClimbAStep(); PickBeeper(); TurnOff(); }

22 Now we must write the function C limbAStep(); void ClimbAStep() { TurnLeft(); Move(); TurnRight(); Move(); } Notice that this function depends on TurnRight();, an instruction that we wrote previously.

23 Recapping how to do function definitions The reserved word void starts a new function followed by the name of the function. The name of the function specifies what the procedure is intended to do. The statements that perform the task are enclosed in matching curly braces { }. These statements specify how the new instruction does what the name implies. The two must match exactly – otherwise one or both need to be changed.

24 To demonstrate this, there are no restrictions prohibiting the following instructions definition: void TurnRight() { TurnLeft(); } This is a perfectly legal definition, but it is wrong because it does not accomplish what it should.


Download ppt "Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps."

Similar presentations


Ads by Google