Karel The Robot Learns JAVA

所需积分/C币:10 2012-08-05 20:56:02 8.54MB PDF
9
收藏 收藏
举报

Karel The Robot Learns JAVA
Chapter 1 Introducing Karel the robot In the 1970s, a Stanford graduate student named rich Pattis decided that it would be easier to teach the fundamentals of programming if students could somehow learn the basic ideas in a simple environment free from the complexities that characterize most programming languages. Drawing inspiration from the success of Seymour Papert's LOGO project at MIT, Rich designed an introductory programming environment in which students teach a robot to solve simple problems. That robot was named Karel after the Czech playwright Karel Capek, whose 1923 play R.U.R.( Rossum's Universal Robots) gave the word robot to the English language Karel the robot was quite a success. Karel was used in introductory computer science courses all across the country, to the point that richs textbook sold well over 100,000 copies. Many generations of Cs106A students learned how programming works by putting Karel through its paces. But nothing lasts forever. In the middle of the 1990s, the simulator we had been using for Karel the robot stopped working. We were, however, soon able to get a version of Karel up and running in the Thetis interpreter we were using at the time. But then a year ago, cs 106A switched to Java, and Karel again vanished from the scene. For the last three quarters, the hole in the curriculum left by Karels departure has been competently filled by Nick Parlante's Binky world, but it seems about time to bring Karel back. The new implementation of Karel is designed to be compatible with both Java and the Eclipse programming environment, which means that you'll get to practice using the Eclipse editor and debugger from the very beginning of the course What is karel Karel is a very simple robot living in a very simple world. By giving Karel a set of mmands, you can direct it to perform certain tasks within its world. The process of specifying those commands is called programming. Initially, Karel understands only a very small number of predefined commands, but an important part of the programming process is teaching Karel new commands that extend its capabilities When you program Karel to perform a task, you must write out the necessary commands in a very precise way so that the robot can correctly interpret what you have told it to do. In particular, the programs you write must obey a set of syntactic rules that define what commands and language forms are legal. Taken together, the predefined commands and syntactic rules define the Karel programming language. The Karel rogramming language is designed to be as similar as possible to Java so as to ease the transition to the language you will be using all quarter. Karel programs have much the same structure and involve the same fundamental elements as Java programs do. The critical difference is that Karel's programming language is extremely small, in the sense that it has very few commands and rules. It is easy, for example, to teach the entire karel language in just a couple of hours, which is precisely what we do in cs106A. At the end of that time, you will know everything that Karel can do and how to specify those actions in a program. The details are easy to master. Even so, you will discover that solving a problem can be extremely challenging Problem solving is the essence of programming, the rules are just a minor concern along the way In sophisticated languages like Java, there are so many details that learning these details often becomes the focus of the course. When that happens, the much more critical issues of problem solving tend to get lost in the shuffle. By starting with Karel, you can concentrate on solving problems from the very beginning. And because Karel encourages imagination and creativity, you can have quite a lot of fun along the way Kare’ s world Karels world is defined by streets running horizontally(east-west) and avenues running vertically(north-south). The intersection of a street and an avenue is called a corner Karel can only be positioned on corners and must be facing one of the four standard compass directions (north, south, east, west). A sample Karel world is shown below. Here Karel is located at the corner of lst Street and lst Avenue facing east Several other components of Karels world can be seen in this example. The object in front of Karel is a beeper. As described in Rich Pattis's book, beepers are"plastic cones which emit a quiet beeping noise. "Karel can only detect a beeper if it is on the same corner. The solid lines in the diagram are walls. Walls serve as barriers within Karel,s world. Karel cannot walk through walls and must instead go around them. Karels world is always bounded by walls along the edges, but the world may have different dimensions depending on the specific problem Karel needs to solve What can Karel do? When Karel is shipped from the factory, it responds to a very small set of commands nove( Asks Karel to move forward one block. Karel cannot respond to a move()command if there is a wall blocking its way turnLeft( Asks Karel to rotate 90 degrees to the left(counterclockwise) pickBeeper() Asks Karel Lo pick up one beeper Irom a corner and stores the beeper in its beeper bag, which can hold an infinite number of beepers. Karel cannot respond to a pickBeeper() command unless there is a beeper on the current corner putBeeper() Asks Karel to take a beeper from its beeper bag and put it down on the current corner. Karel cannot respond to a putBeeper() command unless there are beepers in its beeper bag The empty pair of parentheses that appears in each of these commands is part of the common syntax shared by karel and Java and is used to specify the invocation of the command. Eventually, the programs you write will include additional information in the space between the parentheses, but such information is not part of the Karel's primitive world. These parentheses will there ore be em apty in standard Karel programs, but you must remember to include them nonetheless It is also important to recognize that several of these commands place restrictions on Karel's activities. If Karel tries to do something illegal, such as moving through a wall or picking up a nonexistent beeper, an error condition occurs. At this point, Karel displays an error message and does not execute any remaining commands Karel's commands however. cannot be executed on their own before karel can respond to any of these commands, you need to incorporate them into a Karel program You will have a chance to see a few simple Karel programs in Chapter 2, but before doing So, it is useful to make a few general remarks about the programming philosophy ly that underlies this particular implementation of the karel programming language Karel and the object-oriented paradigm When Karel was introduced in the 1970s, the prevailing approach to writing computer programs was the procedural paradigm. To a large extent, procedural programming is the process of decomposing a large programming problem into smaller, more manageable units called procedures that define the necessary operations. Although the strategy of breaking programs down into smaller units remains a vital part of any style of programming, modern languages like Java emphasize a different approach called the object-oriented paradigm In object-oriented programming, the programmers attention shifts away from the procedural specification of operations and focuses instead on modeling the behavior of conceptually integrated units called objects. Objects in a programming language sometimes correspond to physical objects in the real world, but just as often represent more abstract concepts. The central feature of any object-real or abstract -is that it must make sense as a unified whole One of the primary advantages of the objecl-oriented paradigm is thal il encourages programmers to recognize the fundamental relationship between the state of an object and its behavior. The state of an object consists of a set of attributes that pertain to that object and might change over time. For example, an object might be characterized by its location in space, its color, its name, and a host of other properties. The behavior of an object refers to the ways in which that object responds to events in its world or commands from other objects. In the language of object-oriented programming, the generic word for anything that triggers a particular behavior in an object is called a message(although it generally seems clearer to use the word command in the context of Karel). The response to a message typically involves changing the state of an object. For example, if one of the properties defining the state of an object is its color, then it would presumably respond to a setcolor(BLUE) message by changing its color to blue. In many ways, Karel represents an ideal environment for illustrating the object- oriented approach. although no one has actually built a mechanical implementation of Karel, it is nonetheless easy to imagine karel as a real-world object. Karel is, after all, a robot, and robots are real-world entities. The properties that define karels state are its location in the world, the direction it is facing, and the number of beepers in its beeper bag. Karel,s behavior is defined by the commands to which it responds: move( turnleft(, pickBeeper(), and putBeeper(). The move()command changes Karels location, turnLeft() changes its direction, and the remaining two affect both the number of beepers in Karel's bag and the number of beepers on the current corner The Karel environment also provides a useful framework for defining one of the central concepts of object-oriented programming. In both Karel and Java, it is essential to differentiate the notion of an object from that of a class. The easiest way to understand the distinction is to think about a class as a pattern or template for objects that share a common behavior and collection of state attributes. As you will see in the next chapter the word Karel in a Karel program represents the entire class of robots that know how to respond to the move(), turnLeft(),pickBeeper(), and putBeeper() commands. Whenever you have an actual robot in the world, that robot is an object that represents a specific instance of the karel class. Although you wont have occasion to do so in CS 106A, it is possible to have more than one instance of the Karel class running in the same world. Even when there is only a single robot, however, it is important to remember that object and class are different concepts and to keep those ideas straight in your mind The importance of practical experience Programming is very much a learn-by-doing activity. As you will continually discover in your study of computer science, reading about some programming concept is not the same thing as using that concept in a program. Things that seem very clear on the page can be difficult to put into practice Given the fact that writing programs on your own and getting them to run on the computer are essential to learning about programming, it may seem surprising to discover that this book does not include much discussion of the hands-on aspects of using Karel on your computer. The reason for that omission is that the steps you need to run a Karel program depend on the environment you're using. Running Karel programs on a Macintosh is somewhat different from running it under Windows. Even though the programming environment you use has a great deal of influence on the nitty-gritty details you need to run programs, it has no intluence whatsoever on the general concepts. This book describes the general concepts; the details pertinent to each platform will be distributed as handouts during the course The fact that this book omits the practical details, however, should in no sense be interpreted as minimizing their importance. If you want to understand how programming worKs even in an environment as simple as that provided by Karel-it is essential to get your hands dirty"and start using the computer. Doing so is by far the most effective introduction into the world of programming and the excitement that it holds Chapter 2 Programming Karel In its new object-oriented implementation, the simplest style of Karel program consists of a definition of a new Karel class that specifies a sequence of built-in commands that should be executed when the program is run. A very simple Karel program is shown in Figure 1 The program in Figure 1 is composed of several parts. The first part consists of the following lines: File: BeeperPickingKarel java The BeeperPickingkarel class extends the basic Karel class ★byde£ Liing a"run" method with three commands.四hese commands cause Karel to move forward one block, pick up a beeper, and then move ahead to the nextcorner These lines are an example of a comment, which is simply text designed to explain the operation of the program to human readers. Comments in both Karel and Java begin with the characters / and end with the characters */ Here, the comment begins on the first line and ends several lines later. The stars on the individual lines that make up the text of the comment are not required but make it easier for human readers to see the extent of the comment. In a simple program, extensive comments may seem silly because the effect of the program is obvious, but they are extremely important as a means of documenting the design of larger, more complex programs The second part of the program is the line import stanford, karel.*i This line requests the inclusion of all definitions from the stanford. karel library. This Figure 1. Simple Karel example to pick up a single beeper File: BeeperPickingkarel jav The BeeperPickingkarel class extends the basic Karel class by defining arun method with three commands. These commands cause Karel to move forward one block, pick up a beeper, and then move ahead to the next corner import stanford. karel. *i public class BeeperPickingKarel extends Karel i public void run(t movei pickBeeper(i Move library contains the basic definitions necessary for writing Karel programs, such as the definitions of the standard operations move() and pickBeeper(). Because you always need access to these operations, every Karel program you write will include this import command before you write the actual program e final part of the Karel program consists of the following class definition public class BeeperPickingKarel extends Karel public void run( move(; pickBeeper() move(i To understand this definition, it is useful to look more carefully at its structure. The definition of the Beeperpickingkarel class consists of the line beginning with public class and encompasses every thing between the curly brace at the end of that line and the corresponding closing brace on the last line of the program. The single line that introduces the new class is called the header of the definition the code between the braces is called the body. In programming, it is often very useful to think about a particular detinition and its body as separable ideas. In this example, the definition of BeeperPickingkarel has the following form, where the entire body of the definition has been replaced by a box that you can put out of your mind for the moment public class BeeperPickingkarel extends Karel t body of the class definition The header line at the top tells you quite a bit about the BeeperPickingKarel class, even before you have looked to see what the body contains. The key new concept in the class header is embodied in the word extends, which is used in both karel and java to indicate that a new class is an extension of an existing one. Here, the class header line indicates that BeeperPickingKarel is an extension of the standard Karel class imported from the stanford. karel library In object-oriented languages, defining a new class by extension means that the new class (here, BeeperpickingKarel)builds on the facilities provided by the existing class (in this case, Karel). In particular, the fact that it extends Karel guarantees that the new BeeperPickingKarel class will have the following properties 1. Any instance of the class BeeperPickingkarel is also an instance of the class Karel Any instance of the class Karel represents a robot that lives in a world of streets avenues, beepers, and walls whose state consists of its location, direction, and the number of beepers in its bag. Because BeeperpickingKarel is an extension of Karel, you know that an instance of BeeperPickingKarel will also be a robot that lives in the same type of world and has the same state properties 2. Any instance of the BeeperPickingKarel class will automatically respond to the same commands as an instance of the Karel class. because every robot in the Karel class knows how to respond to the commands move(), turnLeft(), pickBeeper() and putBeeper(, it follows that a instance of BeeperpickingKarel will understand that same set of commands In other words, the new BeeperPickingKarel class automatically acquires the state attributes and the behavior of the Karel class from which it is derived. The process of taking on the structure and behavior of the parent class is called inheritance. When a class is delined by extension, the new class is said Lo be a subclass of the original. In this example, BeeperPickingKarel is therefore a subclass of Karel Symmetrically, Karel is said to be a superclass of BeeperPickingKarel. Unfortunately, th is terminology can be confusing for new programmers, who are likely to make the intuitive inference that a subclass is somehow less powerful that its superclass when in fact the opposite is true. A subclass inherits the behavior of its superclass and can therefore respond to the entire set of commands available to that superclass. A subclass however, usually defines additional commands that are unavailable to the superclass Thus, the typical subclass actually has more functionality than the class from which it was derived. This idea is expressed much more clearly by the notion of extension:a subclass extends its superclass and can therefore add new capabilities to it Now that you have some idea about what class extension means it now makes sense to look at the body of the BeeperPickingkarel class. That body consists of the followin lines: public void runi movei pickBeeper(i movei These lines represent the definition of a new method, which specifies the sequence of steps necessary to respond to a command. As in the case of the BeeperpickingKarel lass itself, the method definition consists of two parts that can be considered separately The first line constitutes the method header and the code between the curly braces is the method body. If you ignore the body for now the method definition looks like this public void run(( body of the method definition The first two words in the method header, public and void, are part of Java's syntactic structure, and you should pretty much feel free to ignore them at this point. The next word on the header line specifies the name of the new method which in this case is the method run. Defining a method means that the new Karel subclass can respond to a new command with that name. The built-in Karel class responds to the commands move ( turnLeft(),pickBeeper(),and putBeeper(); a BeeperPickingKarel responds to that same set of commands plus a new command called run. The run command plays a special role in a Karel program. When you start a Karel program in the eclipse environment, it creates a new Karel instance of the appropriate subclass, adds that Karel to a world that you specify, and then issues the run command. The effect of issuing that command is defined by the body of the run method which is a sequence of commands that the robot will execute in order. For example, the body of the run method for the BeeperPickingKarel class is move(i pickBeeper(; move(i

...展开详情
试读 40P Karel The Robot Learns JAVA
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
Karel The Robot Learns JAVA 10积分/C币 立即下载
1/40
Karel The Robot Learns JAVA第1页
Karel The Robot Learns JAVA第2页
Karel The Robot Learns JAVA第3页
Karel The Robot Learns JAVA第4页
Karel The Robot Learns JAVA第5页
Karel The Robot Learns JAVA第6页
Karel The Robot Learns JAVA第7页
Karel The Robot Learns JAVA第8页

试读结束, 可继续读4页

10积分/C币 立即下载 >