OCW编程方法学 Assignment1

虽说黑程序员的帖子也看了不少,但一直觉得编程对于自己来说最多不过是一件锻炼思维和脑力的活动,过去也有为了考试而学习的经验,终究没对编程产生多大兴趣,果然看了这个Stanford《编程方法学》OCW之后对编程的看法有一点点转变。所以也试着把自己写的assignment1贴出来看看吧。

首先是第一题

/*
* File: CollectNewspaperKarel.java
* --------------------------------
* At present, the CollectNewspaperKarel subclass does nothing.
* Your job in the assignment is to add the necessary code to
* instruct Karel to walk to the door of its house, pick up the
* newspaper (represented by a beeper, of course), and then return
* to its initial position in the upper left corner of the house.
*/

import stanford.karel.*;

public class CollectNewspaperKarel extends Karel {
public void run(){

// You fill in this part
moveToDoor();
pickNewspaper();
moveBack();
}
private void moveToDoor(){
moveToWall();
turnSouth();
while(leftIsBlocked())
move();
turnEast();
move();
}
private void pickNewspaper(){
pickBeeper();
}
private void moveBack(){
turnWest();
move();
turnNorth();
moveToWall();
turnWest();
moveToWall();
turnEast();
}
private void turnNorth(){
while(notFacingNorth())
turnLeft();
}
private void turnSouth(){
while(notFacingSouth())
turnLeft();
}
private void turnEast(){
while(notFacingEast())
turnLeft();
}
private void turnWest(){
while(notFacingWest())
turnLeft();
}
private void moveToWall(){
while(frontIsClear())
move();
}
}

接着是第二题

/*
* File: CheckerboardKarel.java
* ----------------------------
* When you finish writing it, the CheckerboardKarel class should draw
* a checkerboard using beepers, as described in Assignment 1. You
* should make sure that your program works for all of the sample
* worlds supplied in the starter folder.
*/

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

// You fill in this part

public void run(){
putBeeper();
while(frontIsClear()||(facingEast()&&leftIsClear())||(facingWest()&&rightIsClear()))
putBeeperRow();

}
private void putBeeperRow(){
if(beepersPresent()&&frontIsClear())
putBeeperOnPattern();
else{
if(frontIsClear()){
move();
putBeeper();
putBeeperOnPattern();
}
}
moveUp();
}
private void putBeeperOnPattern(){
while(frontIsClear()){
move();
if(frontIsClear()){
move();
putBeeper();
}
}
}
private void moveUp(){
if(facingEast()&&leftIsClear()){
turnLeft();
if(beepersPresent()){
move();
turnLeft();
}
else{
move();
putBeeper();
turnLeft();
}
}
else if(facingWest()&&rightIsClear()){
turnRight();
if(beepersPresent()){
move();
turnRight();
}
else{
move();
putBeeper();
turnRight();
}
}
}
}

第三题

/*
* File: StoneMasonKarel.java
* --------------------------
* The StoneMasonKarel subclass as it appears here does nothing.
* When you finish writing it, it should solve the "repair the quad"
* problem from Assignment 1. In addition to editing the program,
* you should be sure to edit this comment so that it no longer
* indicates that the program does nothing.
*/

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

// You fill in this part
public void run(){
while(frontIsClear()){
turnNorth();
repairColumn();
moveToNextColumn();
}
turnNorth();
repairColumn();
}
private void turnNorth(){
while(notFacingNorth())
turnLeft();
}
private void turnEast(){
while(notFacingEast())
turnLeft();
}
private void turnSouth(){
while(notFacingSouth())
turnLeft();
}
private void moveToNextColumn(){
for(int i=1;i<=4;i++) move();
}
private void repairColumn(){
while(frontIsClear()){
if(noBeepersPresent())
putBeeper();
move();
}
if(noBeepersPresent())
putBeeper();
turnSouth();
while(frontIsClear())
move();
turnEast();
}

}

第四题

/*
* File: MidpointFindingKarel.java
* -------------------------------
* When you finish writing it, the MidpointFindingKarel class should
* leave a beeper on the corner closest to the center of 1st Street
* (or either of the two central corners if 1st Street has an even
* number of corners). Karel can put down additional beepers as it
* looks for the midpoint, but must pick them up again before it
* stops. The world may be of any size, but you are allowed to
* assume that it is at least as tall as it is wide.
*/

import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {

// You fill in this part
public void run(){
fullRow();
while(beepersPresent()){
pickEdge();
}
findCenter();
}
private void fullRow(){
while(frontIsClear()){
putBeeper();
move();
}
turnAround();
while(frontIsClear())
move();
turnAround();
}
private void pickEdge(){
pickBeeper();
move();
do{
move();
} while(beepersPresent());
turnAround();
move();
if(beepersPresent())
pickBeeper();
move();
}
private void findCenter(){
if(facingEast())
putBeeper();
else{
turnAround();
move();
putBeeper();
}
}

}

第四题的方法十分巧妙,不知道能不能看懂。

“OCW编程方法学 Assignment1”的2个回复

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注