《斯坦福大学开放课程编程方法》讲义Word文档格式.docx
- 文档编号:13244893
- 上传时间:2022-10-08
- 格式:DOCX
- 页数:13
- 大小:23.58KB
《斯坦福大学开放课程编程方法》讲义Word文档格式.docx
《《斯坦福大学开放课程编程方法》讲义Word文档格式.docx》由会员分享,可在线阅读,更多相关《《斯坦福大学开放课程编程方法》讲义Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
(likeahurdlesrace,butwitharbitrarilylargehurdles)where:
Karelstartsatposition(1,1),facingEast.
Thesteeplechaseisguaranteedtobe9avenueslong.
Therecanbearbitrarilymanyhurdlesthatcanbeofarbitrarysize,located
betweenanytwoavenuesintheworld.
Karelshould"
jump"
eachhurdleoneatatime.
Forexample,ifyouweretoexecutetheSteepleChaseprogram,youwouldsee
somethinglikethefollowingbefore-and-afterdiagram:
Before
After
BelowistheprogramlistingoftheSteepleChaseprogram,whichprovidesanexample
ofvariouscontrolstructuresinKarel,programdecomposition,andcomments(including
thespecificationofpre-andpost-conditions).
–2–
/*
*File:
SteepleChase.java
*-----------------------
*Karelrunsasteeplechasetheis9avenueslong.
*Hurdlesareofarbitraryheightandplacement.
*/
importstanford.karel.*;
publicclassSteepleChaseextendsSuperKarel{
*Torunaracethatis9avenueslong,weneedtomove
*forwardorjumphurdles8times.
publicvoidrun(){
for(inti=0;
i<
8;
i++){
if(frontIsClear()){
move();
}else{
jumpHurdle();
}
*Pre-condition:
FacingEastatbottomofhurdle
*Post-condition:
FacingEastatbottominnextavenue
*
afterhurdle
privatevoidjumpHurdle(){
ascendHurdle();
descendHurdle();
FacingEastimmediatelyabovehurdle
privatevoidascendHurdle(){
turnLeft();
while(rightIsBlocked()){
turnRight();
–3–
FacingEastaboveandimmediatelyafterhurdle
FacingEastatbottomofhurdle
privatevoiddescendHurdle(){
moveToWall();
none
Facingfirstwallinwhicheverdirection
*Karelwasfacingpreviously
privatevoidmoveToWall(){
while(frontIsClear()){
Creatingalineofbeepers
ConsidertheproblemofwritingamethodcreateBeeperLine,whichcreatesalineof
beepersbeginningatKarel’scurrentcornerandproceedingforwardtothenextwall.For
example,ifyouweretoexecutecreateBeeperLineinanemptyworld,youwouldsee
4
3
2
1
5
6
Theproblemisslightlyharderthanitlooks.Unlessyouthinkcarefullyaboutthe
problem,itiseasytofindyourselfmakingacommonprogrammingerrorthatkeepsthe
programfromworkingasyou’dlike.Forexample,wemightinitiallybeinclinedtosolve
theproblemasfollows:
privatevoidcreateBeeperLine(){
putBeeper();
–4–
Theproblemhere(asindicatedbythe"
bug"
picturenexttothecode),isthatKarelwill
notplaceabeeperonthelastcornerheencounters.Whenhereachesthefinalcornerof
therowhe'
sin,hisfrontwillnolongerbeclear,sothewhileloopwillimmediatelyexit
beforeabeeperisplacedonthatfinalcorner.Thisisperhapseasiertoseewhenlooking
attheKarel'
sworldafterheexecutesthecodeabove:
Notethatinthepictureabove,thereisnobeeperonthecornerthatKarelisstandingon.
Tosolvethisexampleofa"
fence-post"
problem(suchproblemsarefurtherdiscussedin
theKarelcoursereader),wemustmakeonemoreputBeeper()methodcallthancallsto
move(),asshownbelow:
Cleaningupscatteredbeepers
Unfortunately,sometimesKarel'
sworldgetsalittlemessy,withbeepersstrewnaroundat
variouscorners.WewanttohelpKarelcleanuphisworldbywritingaprogramthathas
Karelgothroughtheworldandpickupanybeepersthatmaybescatteredabout.We
assumethat:
Karelstartsatcorner(1,1)facingEast
EachcornerofKarel'
sworldmayeitherbeemptyorcontainatmostonebeeper,
andwhenKarelisdonethereshouldbenomorebeepersonanycorner
Karelcanfinishhistaskatanylocationandorientation
Ifyouweretoexecuteyourprogram,youwouldseesomethinglikethefollowingbefore-
and-afterdiagramonthenextpage.
–5–
BelowistheprogramlistingoftheCleanupKarelprogram.
CleanupKarel.java
*Karelstartsat(1,1)facingEastandcleansupany
*beepersscatteredthroughouthisworld.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 斯坦福大学开放课程编程方法 斯坦福大学 开放 课程 编程 方法 讲义