OOPSLA '99 Workshop Proposal
Quest for Effective Classroom Examples

Karel the Robot as an Example Object

Byron Weber Becker
University of Waterloo
24-August-1999

Abstract: The author believes that a truly objects-first approach is the best way to teach object-oriented programming. Karel the Robot provides a predefined object which students may use to naturally learn about object instantiation, method invocation and extending the robot object with new methods. Traditional early topics such as selection and iteration may be taught using the robot object. In the author's experience, this approach gives much more satisfying results than shifting mid-way through the course from a procedural paradigm to a object-oriented paradigm.

Introduction

Current Java textbooks aimed at the CS1 audience typically begin with a vague, obligatory description of objects (it is an OO language after all!) but then quickly digress to "fundamental concepts" such as primitive types, selection, repetition and perhaps I/O. After a number of chapters, usually devoid of any objects whatsoever, students are suddenly expected to begin writing their own objects. With these typical textbooks, students are left with a number of legitimate questions: What is an object? How do I use these things I'm writing? What are the characteristics of a "good" object? How do all these other things I've been learning relate to objects?

At the University of Waterloo we believe that teaching object-oriented programming involves a different set of "fundamental concepts": using objects to solve a problem. Whenever possible, it makes sense to maximize student exposure and practise with the fundamentals by teaching them first. Therefore, we present a class, Karel the Robot, from which students instantiate objects, invoke methods on those objects, and extend the class to create objects with new capabilities.

We have been extremely pleased with this approach. It is not original with us. It has its origins with Richard Pattis long before objects were common in first year CS. Its current published source is Karel++: A Gentle Introduction to the Art of Object-Oriented Programming by Joseph Bergin, et. al. (Wiley, 1997). I, together with Jack Rehder, have translated this book to Java for use at University of Waterloo and constructed our CS1 course to use it. Our course website is at http://www.undergrad.math.uwaterloo.ca/~cs130/ and includes classes implementing the robot and its world as well as closed lab activities and assignments.

Karel is, I think, a different sort of example than the samples given in the Call for Contributions. It is a large example used for 2-4 weeks. It is a philosophy for organizing the beginning of the course. It forms the basis for instruction on many of the topics in the Call for Contributions list of suggested topics, but doesn't directly address any of them. Nevertheless, I am confident it would be an important contribution to the workshop.

Karel the Robot

image of example Robot program
Figure 1

Karel inhabits a very simple world. There are avenues running north and south numbered 1 to infinity and streets running east and west, also numbered one to infinity. Walls may be placed between the avenues or streets, running parallel to them. Beepers may be placed on the intersections of the avenues and streets.

Within this world, robots may move forward from one intersection to an adjacent intersection unless the way is blocked by a wall. They may turn left 90 degrees to face a different direction. Robots may pick up a beeper from their current intersection, or place a beeper on their current intersection provided they are carrying at least one beeper. Robots may carry beepers between intersections in their "beeper bag" and may detect whether or not their beeper bag is empty.

Robots may also determine if they are facing North (or any of the other 3 compass positions), if there is a beeper on the current intersection or if there is another robot on the current intersection.

The world and the actions of any robots within it are shown visually on the computer monitor. A sample involving a number of walls (rectangles), beepers (circles) and one robot (an arrowhead) is shown in Figure 1. The same example (and several others), running as applets, are available at http://www.math.uwaterloo.ca/~bwbecker/papers/oopsla99/samples.html.

Course Outline

A typical CS1 course using Karel the Robot might begin like this:

Week Topics
01 Karel and the robot world. Basic capabilities. A first program. Instantiating an object and invoking methods. Classes vs. objects. Details of the development environment.
02 Extending the robot class using inheritance with new methods to give it new capabilities. Step-wise refinement.
03 Selection and repetition to program the robots to perform more complex tasks. Using methods returning a boolean value; boolean expressions. Introduce integers as loop counters.
04 Methods with parameters. For example, create a new class of robot which can harvest a field of beepers where the size is specified via parameters.
05 Extending classes with new instance variables. We show several examples using robots but then make the transition to other examples.

Perceived Advantages

There are a number of advantages to using Karel the Robot to introduce objects. A number have been alluded to already. They are more carefully enumerated here.

  1. The fundamental concepts in OO programming (instantiating objects, invoking methods, extending existing classes) are presented first. This emphasizes them in the minds of the students, gives the longest possible time to practise them, and avoids a paradigm shift (and the resulting confusion) from procedural programming to OO programming.
  2. Robots are fun! Students enjoy directing them to do various tasks. Acting out a program in class is more fun than tracing a listing. Visual output is more fun than textual output.
  3. Robot programs are visual. The animation provides visual feedback on the correctness of an algorithm. Seeing the robot's progress makes debugging easier. Many problems can be specified using a picture of the initial situation and another of the final situation, plus a few lines of text.
  4. Since robots provide output visually, traditional input and output can be delayed. This is particularly attractive in Java where I/O is cumbersome, at best.
  5. Many beginning programmers want to write and understand the entire program, something not feasible in the real world. This approach never gives them the chance to start in that mode of thinking.

Possible Objections

One major objection is philisophical: just what are the fundamental concepts? Some have objected to delaying the introduction of traditionally fundamental topics until after the introduction of objects. Depending on the person, these "fundamentals" include one or more of primitive types, selection, repetition, and textual I/O.

As programming languages have provided more and more abstractive capabilities, the starting point for students has also shifted. Most of us are past the argument that assembly language jumps are more fundamental than selection and repetition and should therefore be taught first. A decade ago it was common to delay procedures until well after selection and repetition. Now the accepted pedagogy is that procedures should be taught early. The move to objects early is a simple continuation of this historical trend.

Others have objected to using "magic" in the form of provided classes. They want students to see and understand every part of their programs. But we have been using "magic" in the form of print statements for a long time! Why is the magic of a print statement acceptable but the magic of another predefined class is not? Furthermore, a significant skill in today's workplace is being able to use code resources produced by someone else. Why not do it from the beginning?

Summary

We have been extremely pleased with using the example of robots to teach object-oriented programming. We believe they give tremendous advantages when compared with the traditional approaches embodied in current (Java) textbooks.

Name Karel the Robot
Purpose To introduce important object-oriented concepts as early as possible so that students don't need to shift from one programming paradigm to another.
Prerequisite Knowledge None
Design Students use a predefined suite of objects (Karel the Robot and his world) to learn important O-O techniques from the beginning. These include object instantiation, method invocation, and extending classes with new methods. Traditional topics such as selection and iteration are discussed using the robot which gives visual feedback as to whether the programs are working correctly.
Usage This is the primary, perhaps only, example for the first 2-4 weeks of an introductory programming course.
Resources Karel++: A Gentle Introduction to the Art of Object-Oriented Programming by Joseph Bergin, et. al. is the current published source. I, together with Jack Rehder, have translated this book to Java for use at University of Waterloo. Our course website is at http://www.undergrad.math.uwaterloo.ca/~cs130/ and includes classes implementing the robot and its world as well as closed lab activities and assignments.

Author's Biography

Byron Weber Becker has been a faculty member in the Department of Computer Science, University of Waterloo since 1991. He has recently developed a CS1 course using Java. The course includes lectures, a more interactive time with students called "practicum", closed labs, and of course, assignments. The course is typically delivered to about 1000 students each year. Byron has also been active in reviewing textbooks, including the just released Computing Concepts with Java2 Essentials by Cay Horstmann and An Introduction to Computer Science Using Java by Kamin, Mickunas, and Reingold. He has also contributed to the Pedagogical Patterns project (#56).

Byron earned a BA (Mathematics) from Goshen College, a small liberal arts college in Goshen, Indiana, in 1983. He earned a MMath (Computer Science) from University of Waterloo in 1989. Work experience outside of academia has included computer support for manufacturing and insurance, as well as advanced text searching with Open Text, Inc.