《斯坦福大学开放课程:编程方法》讲义.docx_第1页
《斯坦福大学开放课程:编程方法》讲义.docx_第2页
《斯坦福大学开放课程:编程方法》讲义.docx_第3页
《斯坦福大学开放课程:编程方法》讲义.docx_第4页
《斯坦福大学开放课程:编程方法》讲义.docx_第5页
已阅读5页,还剩3页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Mehran SahamiCS 106AHandout #9September 28, 2007Example Karel ProblemsPortions of this handout by Eric RobertsRunning a steeple chaseIn class on Wednesday, we examined a SteepleChase program that allows Karel to run aSteeple Chase (like a hurdles race, but with arbitrarily large hurdles) where:Karel starts at position (1, 1), facing East.The steeple chase is guaranteed to be 9 avenues long.There can be arbitrarily many hurdles that can be of arbitrary size, locatedbetween any two avenues in the world.Karel should jump each hurdle one at a time.For example, if you were to execute the SteepleChase program, you would seesomething like the following before-and-after diagram:BeforeAfterBelow is the program listing of the SteepleChase program, which provides an exampleof various control structures in Karel, program decomposition, and comments (includingthe specification of pre- and post-conditions). 2 /* File: SteepleChase.java* -* Karel runs a steeple chase the is 9 avenues long.* Hurdles are of arbitrary height and placement.*/import stanford.karel.*;public class SteepleChase extends SuperKarel /* To run a race that is 9 avenues long, we need to move* forward or jump hurdles 8 times.*/public void run() for (int i = 0; i 8; i+) if (frontIsClear() move(); else jumpHurdle();/* Pre-condition:Facing East at bottom of hurdle* Post-condition: Facing East at bottom in next avenue*after hurdle*/private void jumpHurdle() ascendHurdle();move();descendHurdle();/* Pre-condition:Facing East at bottom of hurdle* Post-condition: Facing East immediately above hurdle*/private void ascendHurdle() turnLeft();while (rightIsBlocked() move();turnRight(); 3 /* Pre-condition:Facing East above and immediately after hurdle* Post-condition: Facing East at bottom of hurdle*/private void descendHurdle() turnRight();moveToWall();turnLeft();/* Pre-condition:none* Post-condition: Facing first wall in whichever direction* Karel was facing previously*/private void moveToWall() while (frontIsClear() move();Creating a line of beepersConsider the problem of writing a method createBeeperLine, which creates a line ofbeepers beginning at Karels current corner and proceeding forward to the next wall. Forexample, if you were to execute createBeeperLine in an empty world, you would seesomething like the following before-and-after diagram:4321Before4321After123456123456The problem is slightly harder than it looks. Unless you think carefully about theproblem, it is easy to find yourself making a common programming error that keeps theprogram from working as youd like. For example, we might initially be inclined to solvethe problem as follows:private void createBeeperLine() while (frontIsClear() putBeeper();move(); 4 The problem here (as indicated by the bug picture next to the code), is that Karel willnot place a beeper on the last corner he encounters. When he reaches the final corner ofthe row hes in, his front will no longer be clear, so the while loop will immediately exitbefore a beeper is placed on that final corner. This is perhaps easier to see when lookingat the Karels world after he executes the code above:Note that in the picture above, there is no beeper on the corner that Karel is standing on.To solve this example of a fence-post problem (such problems are further discussed inthe Karel course reader), we must make one more putBeeper() method call than calls tomove(), as shown below:private void createBeeperLine() while (frontIsClear() putBeeper();move();putBeeper();Cleaning up scattered beepersUnfortunately, sometimes Karels world gets a little messy, with beepers strewn around atvarious corners. We want to help Karel clean up his world by writing a program that hasKarel go through the world and pick up any beepers that may be scattered about. Weassume that:Karel starts at corner (1, 1) facing EastEach corner of Karels world may either be empty or contain at most one beeper,and when Karel is done there should be no more beepers on any cornerKarel can finish his task at any location and orientationIf you were to execute your program, you would see something like the following before-and-after diagram on the next page. 5 BeforeAfterBelow is the program listing of the CleanupKarel program./* File: CleanupKarel.java* -* Karel starts at (1, 1) facing East and cleans up any* beepers scattered throughout his world.*/import stanford.karel.*;public class CleanupKarel extends SuperKarel /* Cleans up a field of beepers, one row at a time */public void run() cleanRow();while (leftIsClear() repositionForRowToWest();cleanRow();if (rightIsClear() repositionForRowToEast();cleanRow(); else /* In rows with an even number of streets, we want* Karels left to be blocked after he cleans the last* row, so we turn him to the appropriate orientation.*/turnAround(); 6 /* Cleans up a row in whichever direction Karel is facing */private void cleanRow() if (beepersPresent() pickBeeper();while (frontIsClear() move();if (beepersPresent() pickBeeper();/* Reposition Karel at far East wall to face West on next row */private void repositionForRowToWest() turnLeft();move();turnLeft();/* Reposition Karel at far West wall to face East on next row */private void repositionForRowToEast() turnRight();move();turnRight();Doubling the number of beepers in a pileInterestingly enough, Karel can also do various kinds of math. For example, we can askKarel to double the number of beepers on some corner in the world. Wed like to write aprogram where there is a pile of some (finite) number of beepers on the corner directly infront of Karel. We want Karel to double the number of beepers in that pile and return tohis original location and orientation. You can assume that Karel begins with an infinitenumber of beepers in his beeper bag.For example, if you were to execute your program in the sample Before world shownbelow, you would expect to see the corresponding After world (note that the number ofbeepers on corner (2, 1) has been doubled):BeforeAfter 7 Below is the program listing of the DoubleBeepers program./* File: DoubleBeepers.java* -* Karel doubles the number of beepers on the corner directly* in front of him in the world. He then returns to his* original position/orientation.*/import stanford.karel.SuperKarel;public class DoubleBeepers extends SuperKarel public void run() move();DoubleBeepersInPile();moveBackward();/* For every beeper on the current corner, Karel places* two beepers on the corner immediately ahead of him.*/private void DoubleBeepersInPile() while (beepersPresent() pickBeeper();PutTwoBeepersNextDoor();MoveP

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论