karel
- 格式:docx
- 大小:19.30 KB
- 文档页数:6
ch1-ch31. you’ll get to pratice using the eclipse editor and the debugger from the very begining of the course.2. problem solving is the essence of programming, the rules are just a minor concern along the way.3. by starting with Karel, you can concentrate on solving problems from the very begining.4. the importance of pratical experience :1> programming is very much a learn-by-doing activity.2> “get your hands dirty”.Assignmet 1problem 1捡报纸1 import stanford.karel.*;23 public class CollectNewspaperKarel extends SuperKarel {456 public void run() {7 moveToNewspaper();8 pickupNewspaper();9 returnStart();1011 }1213 //move to the newspaper14 private void moveToNewspaper() {15 move();16 turnRight();17 move();18 turnLeft();19 move();20 move();21 }2223 //pick it up24 private void pickupNewspaper() {25 pickBeeper();26 turnAround();27 }2829 //return to its starting point30 private void returnStart() {31 move();32 move();33 turnRight();34 move();35 turnLeft();36 move();37 }38 }problem 2修复柱子1 * File: StoneMasonKarel.java2 * --------------------------3 * The StoneMasonKarel subclass as it appears here does nothing.4 * When you finish writing it, it should solve the "repair the quad"5 * problem from Assignment 1. In addition to editing the program,6 * you should be sure to edit this comment so that it no longer7 * indicates that the program does nothing.8 */910 import stanford.karel.*;1112 public class StoneMasonKarel extends SuperKarel {1314 public void run() {15 while (frontIsClear()) {16 checkForStones();17 for (int i=0; i<4; i++) {18 move();19 }20 }21 checkForStones();22 }2324 private void checkForStones() {25 turnLeft();26 while (frontIsClear()){27 fillStone();28 move();29 }30 fillStone();31 turnAround();32 while (frontIsClear()) {33 move();34 }35 turnLeft();36 }3738 private void fillStone() {39 if (noBeepersPresent()) {40 putBeeper();41 }42 }4344 }problem 3棋盘算法:曲线递进。
1 * File: CheckerboardKarel.java2 * ----------------------------3 * When you finish writing it, the CheckerboardKarel class should draw4 * a checkerboard using beepers, as described in Assignment 1. You5 * should make sure that your program works for all of the sample6 * worlds supplied in the starter folder.7 */8910 /*本程序目的是让卡尔在空白棋盘上放棋子。
并符合上图的规律。
*/11 import stanford.karel.*;12 public class CheckerboardKarel extends SuperKarel {13 /* 放完自己站的第一行棋子,面朝上,14 如果头顶没墙,就向上一步,开始放置向左的棋子,放完面朝上,头顶有墙,行动结束。
15 放完头顶没墙,开始放置返回的棋子。
返回后头顶有墙,行动结束。
16 否则,再重新开始。
*/17 public void run(){18 putBeeper();19 putOneLineBeeper();20 turnLeft();21 while(frontIsClear()){22 putAPairBeeper();23 turnLeft();24 putOneLineBeeper();25 turnRight();26 if(frontIsClear()){27 putAPairBeeper();28 turnRight();29 putOneLineBeeper();30 turnLeft();31 }32 }33 }3435 //成对放置一行棋子36 private void putOneLineBeeper(){37 while(frontIsClear()){38 putAPairBeeper();39 }40 }41 //一黑一白成对放置棋子的42 private void putAPairBeeper(){43 if (beepersPresent()){44 move();45 }46 else {47 move();48 putBeeper();49 }50 }51 }problem 4找中间点算法:先布满整行(除了首末位),再逐个捡起两端的方块,找到中间位,放置一个方块。
1 /*2 * File: MidpointFindingKarel.java3 * -------------------------------4 * When you finish writing it, the MidpointFindingKarel class should5 * leave a beeper on the corner closest to the center of 1st Street6 * (or either of the two central corners if 1st Street has an even7 * number of corners). Karel can put down additional beepers as it8 * looks for the midpoint, but must pick them up again before it9 * stops. The world may be of any size, but you are allowed to10 * assume that it is at least as tall as it is wide.11 */12 /*13 * put one beeper at the middle point of the 1st street.14 */15 import stanford.karel.*;1617 public class MidpointFindingKarel extends SuperKarel {1819 public void run() {20 putOneLine(); //fill the 1st street with beepers except 1st and the last avenues.21 turnBack();22 //pick up beside beepers23 while (beepersPresent()) {24 pickBesideBeeper();25 move();26 }27 //put one beeper at the middle point28 turnBack();29 putBeeper();30 }3132 private void putOneLine() {33 move();34 while (frontIsClear()) {35 putBeeper();36 move();37 }38 }3940 private void pickBesideBeeper() {41 while (beepersPresent()){42 move();43 }44 turnBack();45 pickBeeper();46 }4748 private void turnBack() {49 turnAround();50 move();51 }52 }。