斯坦福大学公开课:编程方法学ASSIGNMENT1
- 格式:doc
- 大小:45.50 KB
- 文档页数:7
斯坦福大学公开课:编程方法学
ASSIGNMENT1
这个是根据
Assignment #1: Email and Karel the Robot
Karel problems due: 1:15pm on Friday, April 12th ,2013
自己写的代码,没有做过优化,这次的作业中Problem3的代码有问题,只能实现在大多数的世界中使用,在1 X N这种会失效还没有修改,刚接触这门课程,还是挺有趣的,卡雷尔机器人程序。
我先声明了一个通用的类, 然后再做的作业.
import stanford.karel.*;
public class 通用 extends SuperKarel{
public void moveBackToWall(){
turnAround();
while(frontIsClear()){
move();
}
}
public void moveToWall(){
while(frontIsClear()){
move();
}
}
public void moveBackward(){
turnAround();
move();
turnAround();
}
public void turnBack(){
turnLeft();
turnLeft();
}
}
problem1
/*
* 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.
*/
public class CollectNewspaperKarel extends 通用 {
public void run(){
moveToWall();
turnRight();
move();
turnLeft();
move();
pickBeeper();
turnBack();
move();
turnRight();
moveToWall();
turnLeft();
moveToWall();
putBeeper();
turnBack();
}
} problem2
public class UnitedNationsKarel extends 通用 {
public void run(){
while(frontIsClear()){
if(beepersPresent()){
pickBeeper();
buildHouse();
}
if(frontIsClear()){
move();
}
}
}
private void buildHouse(){
getReady();
build();
finish();
}
private void getReady(){
moveBackward();
turnLeft();
}
private void finish(){
turnLeft();
}
private void build(){
buildPart();
nextToWork();
buildPart();
getNext();
buildPart();
}
private void buildPart(){
putBeeper();
move();
putBeeper();
move(); putBeeper();
}
private void nextToWork(){
move();
turnRight();
move();
turnRight();
}
private void getNext(){
turnAround();
nextToWork();
}
}
problem3
/*
* 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.
*/
public class CheckerboardKarel extends 通用 {
public void run(){
while(leftIsClear()){
while(frontIsClear()){
putRow();
}
moveToNextStreet();
}
while(frontIsClear()){
putRow();
}
checkBeeper();
}
private void putRow(){
putBeeper();
if(frontIsClear()){ move();
}
if(frontIsClear()){
move();
}
}
private void moveToNextStreet(){
checkBeeper();
moveBackToWall();
moveForPutRow();
}
private void checkBeeper(){
moveBackward();
if(beepersPresent()){
}else{
move();
putBeeper();
}
}
private void moveForPutRow(){
turnRight();
if(beepersPresent()){
move();
turnRight();
move();
}else{
move();
turnRight();
}
}
}
problem4
/*
* 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.
*/
public class MidpointFindingKarel extends 通用 {
public void run(){
fullOfBeepers();
while(beepersPresent()){
changeForTake();
takeOneBeeper();
}
turnAround();
move();
}
private void fullOfBeepers(){
putBeeper();
while(frontIsClear()){
move();
putBeeper();
}
}
private void changeForTake(){
moveToWall();
turnAround();
}
private void takeOneBeeper(){
for(int i=0;i<15;i++){
if(beepersPresent()){
}else{
move();
}
}
move();
checkBeeper();
}
private void checkBeeper(){
if(beepersPresent()){
moveBackward();
pickBeeper();