Karel++
A Gentle Introduction to the Art of Object-Oriented Programming

Copyright 1997, John Wiley & Sons. All Rights Reserved.

ISBN 0-471-13809-6
You can purchase this book from the Wiley Home Page .



CONTENTS



1 The Robot World 1

	1.1 The Robot World 1

	1.2 Robot Capabilities 3

	1.3 Tasks and Situations 4

	1.4 Problem Set 5



2 Primitive Instructions and Simple Programs 7

	2.1 Changing Position 7

	2.2 Turning in Place 8

	2.3 Finishing a Task 9

	2.4 Handling Beepers 9

	2.5 Robot Descriptions 10

	2.6 A Complete Program 11

		2.6.1 Executing a Program 12

		2.6.2 The Form of Robot Programs 14

	2.7 Error Shutoffs 16

	2.8 Programming Errors 16

		2.8.1 Bugs and Debugging 19

	2.9 Problem Set 19

	

3 Extending the Robot Programming Language 25

	3.1 Creating a More Natural Programming Language 25

	3.2 A Mechanism That Defines New Classes of Robots 26

	3.3 Defining the New Instructions 27

	3.4 The Meaning and Correctness of New Instructions 30

	3.5 Defining New Instructions in a Program 31

	3.6 Modifying Inherited Instructions 35

	3.7 An Ungrammatical Program 36

	3.8 Tools for Designing and Writing Robot Programs 37

		3.8.1 Stepwise Refinement-A Technique for Planning, Implementing, and Analyzing 

			Robot Programs 38

		3.8.2 The Second Step-Planning harvestTwoRows and positionForNextHarvest 41

		3.8.3 The Third Step-Planning harvestOneRow and goToNextRow 43

		3.8.4 The Final Step-Verifying That the Complete Program Is Correct 45

	3.9 Advantages of Using New Instructions 47

		3.9.1 Avoiding Errors 49

		3.9.2 Future Modifications 50

		3.9.3 A Program Without New Instructions 52

	3.10 Writing Understandable Programs 53

	3.11 Robot Teams 55

	3.12 Object-Oriented Design 58

	3.13 Problem Set 61



4 Conditionally Executing Instructions 69

	4.1 The IF Instruction 69

	4.2 The Conditions That Robots Can Test 71

		4.2.1 Writing New Predicates 73

	4.3 Simple Examples of the IF Instruction 74

		4.3.1 The harvestOneRow Instruction 74

		4.3.2 The faceNorthIfFacingSouth Instruction 76

		4.3.3 The faceNorth Instruction 77

		4.3.4 Determining the Correctness of the IF Instruction 79

	4.4 The IF/ ELSE Instruction 80

	4.5 Nested IF Instructions 82

	4.6 More Complex Tests 86

	4.7 When to Use an IF Instruction 89

	4.8 Transformations for Simplifying IF Instructions 90

	4.9 Problem Set 94



5 Instructions That Repeat 101

	5.1 The LOOP Instruction 101

	5.2 The WHILE Instruction 103

		5.2.1 Why WHILE Is Needed 103

		5.2.2 The Form of the WHILE Instruction 104

		5.2.3 Building a WHILE Loop -The Four-Step Process 106

		5.2.4 A More Interesting Problem 107

	5.3 Errors to Avoid with WHILE Loops 109

		5.3.1 The Fence Post Problem 109

		5.3.2 Infinite Execution 111

		5.3.3 When the Test of a WHILE is Checked 112

	5.4 Nested WHILE Loops 113

		5.4.1 A Good Example of Nesting 113

		5.4.2 A Bad Example of Nesting 116

	5.5 WHILE and IF Instructions 121

	5.6 Reasoning About Loops 122

	5.7 A Large Program Written by Stepwise Refinement 126

	5.8 When to Use a Repeating Instruction 132

	5.9 Problem Set 134



6 Advanced Techniques for Robots 147

	6.1 Introduction to Recursion 147

	6.2 More on Recursion 150

	6.3 Tail Recursion and Looping  153

	6.4 Going Formal 155

	6.5 Searching 156

	6.6 Doing Arithmetic 161

	6.7 Polymorphism-Why Write Many Programs When One Will Do? 166 

	6.8 Conclusion 169

	6.9 Hierarchy of Known Classes 170

	6.10 Problem Set 171



A Robot Programming Summary 177



B Differences Between Karel++ and C++ 181



C Differences Between Karel++ and Java 183


Back to the Book Karel++