【笔记】Stanford OpenCourse—CS106A:Programming Methodology—001

Karel the robot learns java

ch1-ch3

1. 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 1

problem 1

捡报纸

  
    
1 import stanford.karel. * ;
2
3   public class CollectNewspaperKarel extends SuperKarel {
4
5
6 public void run() {
7 moveToNewspaper();
8 pickupNewspaper();
9 returnStart();
10
11 }
12
13 // move to the newspaper
14   private void moveToNewspaper() {
15 move();
16 turnRight();
17 move();
18 turnLeft();
19 move();
20 move();
21 }
22
23 // pick it up
24   private void pickupNewspaper() {
25 pickBeeper();
26 turnAround();
27 }
28
29 // return to its starting point
30   private void returnStart() {
31 move();
32 move();
33 turnRight();
34 move();
35 turnLeft();
36 move();
37 }
38 }

problem 2

修复柱子

  
    
1 * File: StoneMasonKarel.java
2 * --------------------------
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 longer
7 * indicates that the program does nothing.
8 */
9
10   import stanford.karel. * ;
11
12   public class StoneMasonKarel extends SuperKarel {
13
14 public void run() {
15 while (frontIsClear()) {
16 checkForStones();
17 for ( int i = 0 ; i < 4 ; i ++ ) {
18 move();
19 }
20 }
21 checkForStones();
22 }
23
24 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 }
37
38 private void fillStone() {
39 if (noBeepersPresent()) {
40 putBeeper();
41 }
42 }
43
44 }

problem 3

棋盘

算法:曲线递进。

  
    
1 * File: CheckerboardKarel.java
2 * ----------------------------
3 * When you finish writing it, the CheckerboardKarel class should draw
4 * a checkerboard using beepers, as described in Assignment 1 . You
5 * should make sure that your program works for all of the sample
6 * worlds supplied in the starter folder.
7 */
8
9
10   /* 本程序目的是让卡尔在空白棋盘上放棋子。并符合上图的规律。 */
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 }
34
35 // 成对放置一行棋子
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.java
3 * -------------------------------
4 * When you finish writing it, the MidpointFindingKarel class should
5 * leave a beeper on the corner closest to the center of 1st Street
6 * (or either of the two central corners if 1st Street has an even
7 * number of corners). Karel can put down additional beepers as it
8 * looks for the midpoint, but must pick them up again before it
9 * stops. The world may be of any size, but you are allowed to
10 * 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. * ;
16
17 public class MidpointFindingKarel extends SuperKarel {
18
19 public void run() {
20 putOneLine(); // fill the 1st street with beepers except 1st and the last avenues.
21 turnBack();
22 // pick up beside beepers
23 while (beepersPresent()) {
24 pickBesideBeeper();
25 move();
26 }
27 // put one beeper at the middle point
28 turnBack();
29 putBeeper();
30 }
31
32 private void putOneLine() {
33 move();
34 while (frontIsClear()) {
35 putBeeper();
36 move();
37 }
38 }
39
40 private void pickBesideBeeper() {
41 while (beepersPresent()){
42 move();
43 }
44 turnBack();
45 pickBeeper();
46 }
47
48 private void turnBack() {
49 turnAround();
50 move();
51 }
52 }

你可能感兴趣的:(programming)