Programming MethodologyLecture02.docx
《Programming MethodologyLecture02.docx》由会员分享,可在线阅读,更多相关《Programming MethodologyLecture02.docx(17页珍藏版)》请在冰豆网上搜索。
ProgrammingMethodologyLecture02
ProgrammingMethodology-Lecture02
Instructor(MehranSahami):
Alrighty,welcomebacktoCS106A.Ifyou'restuckintheback,justcomeondown,haveaseat.Originally,Ithoughtmaybewewouldhaveslightlyfewerpeopletodaythanlasttime,butthatappearsnottobethecase.Sowhilewe'rewaiting,everyonelovesbabies,soIdecidedtoput?
that'sKarel,theRobot,theearlydays.
No,thisactually?
myson,andyeah,Iknow.He'salittlebitoldernowbut,like,he'sgottheselittlerobotpajamasandherunsaroundallthetime.SoI'mlike,oh,it'sKarel,TheRobot,andmywifelooksatmelike,it'syourson.Butthat'sawholedifferentissue.
Anyway,afewadministrativeannouncementsbeforewestart.Acouplethings,therearefourmorehandoutstoday,justbecausewelikeconsistency.Fourlasttime,there'sfourthistime.They'reallintheback.Ifyoudidn'talreadypickthemup,youcanpickthemupafterclass,buttheyhavealltheinformationaboutdownloadingEclipse,whichistheenvironmentthatyou'regoingtouseforprogramminginthisclass,bothforKarelandforJava.
ThereisalsoaspecialhandoutintherejustonusingKarel,soittalksabouthowKarelworksintheEclipseenvironment,andsoyoucangetallsetupwiththat.Andsoaftertoday,you'llknowhowKarelworksandyou'llhavetheenvironmenttouseit.Sosurprisinglyenough,youalsogetyourfirstassignmenttoday,whichisactuallyintwoparts.
Sotheassignment,therealassignmentistheprogrammingpart,whichisdueonFridayofnextweek,October5th.Thereisalsoanemailparttoit,wheretheemailpartis,weaskallofyoukindfolkstosendanemailtoBen,theheadTA,myself,andalsoyoursectionleader.SoBenandIgetawholebunchofmail,yoursectionleaderwillhopefullygetalittlebitlessmail.
Butyouwon'tactuallyknowyoursectionleaderuntilafteryougotoyourfirstsectionnextweek,whichiswhythatpartoftheassignmentisnotactuallydueuntilbasicallyoneminutebeforemidnightonSunday.SoIdidn'tmakeitmidnight,justbecausethenthere'salwaysconfusion,midnight,whichnight?
So11:
59p.m.onSunday,October7thiswhenyoushouldsendthatemail.
That'snotreallythecriticalpartoftheassignment.Thecriticalpartoftheassignmentistheprogrammingproblems,whichareactuallyalldueinclassonFriday.Okay,andthenthere'sonelastassignment,oronelasthandout,whichisabouthowyouactuallysubmityourworkinthisclass.You'llsubmityourworkbothelectronicallyandinhardcopy.Theelectroniccopy,sothatyoursectionleadercanrunitandverifyit.Thehardcopyissothattheycanactuallywritecommentsonit,andthenyou'lldointeractivegradingwiththemaswell.
Acoupleotherquickannouncements.Thewebsite,incaseyouweren'thereonMondayandyoudon'tknowwhathandoutsI'mtalkingabout,andyoudon'tknowwheretogetthem,gototheclasswebsite,cs106A.stanford.edu.TherewillbeelectroniccopiesinPDFformatofallthehandoutsthere,soyoucangetcaughtupiftodayisyourfirstday.
Youalsoneedtosignupforasection.Aswementionedlasttime,sectionsignupsstarttomorrow,5:
00p.m.Signupearlyifyouwanttohavethemostflexibilityintermsoftime,becauselookaround,there'sawholebunchofpeopleinthisclass.It'sgoingtofillupquickly.Soifyouhaveconstraints,signupquickly,andtheplaceyousignupisCS198,notCS106A,butCS198.stanford.edu/section.The198folksarethefolkswholovinglyrunallofthesectionsandcoordinatethewholeprogram.
Last,butnotleast,justawordonthereadingsthatIdidn'tmentionlasttime.Onthesyllabus,you'llnoticethatprettymuchforeveryday,ormostdaysinthequarter,thereissomesortofreadingassociatedwithit.That'sthereadingassignmentthatyoushouldhavedonebythatday'slecture,becausethat'swhatwewillcoverinthatday'slecture.
SofortodayitshouldbethefirstthreechaptersoftheKarelbook,orthecoursereader.Andifyou'relike,ohmyGod,I'malreadythreechaptersbehind,don'tworry,it'slike20pagesorsomething.It'sprettylightweight,butyoushoulddothereadingsbythattimeinclass.Soanyquestionsaboutanythingwecoveredlasttime,whichismostlylogistics,oranyofthesortofadministrivia?
Allrighty,thenlet'sgetstartedontherealcontent.Soyourememberfromthetimebefore,wetalkedalittlebitaboutKarel,theRobot,andhereisKarelinthatworldthatIshowedyoubefore.Andtherewereavenuesthatrunnorth-south,andstreetsthatruneast-west,andlittlebeepersintheworld.AndKarencanfacedifferentdirections,andthere'swalls.
Andsonow,wewanttothinkabouthowdoweactuallyprogramKarel.Howdowegetthislittleguytodosomethinginterestingintheworld?
Okay,anditturnsouttherearefourcommandsthatKarelunderstands,okay,andthoseareprettystraightforward.
Soheretheyare.You'regoingtogetallofKarel'svocabularyin,like,oneminute.There'sacommandcalledmove,andmovebasicallymovesKarelonespotforwardinthedirectionhe'sfacing.Soifhe'soncornerone,one,andhemovesonespotforward,he'sfacingeast.Sohe'llmoveovertotwo,one,basically.Soonecornerforwardinthedirectionhe'sfacing.
Karelalsostartedbeingagooddemocrat,knowshowtoturnleft.Soheturnsleft.ThisisalowercaseT,thisisanuppercaseL.Soturningleftchangeshisdirectionby90degreesinthelefthandofwhicheverwayhe'sfacing.Soifhe'sfacingeastnow,andheturnsleft,hewillbefacingnorth.Andthenhecanturnleftagainandhe'llbefacingwest.
Okay,now,question?
Student:
[Inaudible]?
Instructor(MehranSahami):
Thereisnospacehere.Thesearealloneword.Goodquestion,andIloveitwhenyoumakeiteasy.Allright,sobesidesturningleft,there'salsoB-personKarel'sworld,andifhecouldn'tdoanythingwithbeepers,theywouldn'tbeinteresting.Sohecanpickupbeepers,whichinterestinglyenoughiscalledpickBeeper,againlowercaseP,uppercaseB,alloneword,andthereisputBeeper.
AndwhatthesedoispickBeeper,ifKarelhappenstobeonacornerthathasaBeeperonit,hepicksupthatBeeper,andstuffsitinhisBeeperbag,inwhichcasethecountinhisBeeperbaggoesupbyone.Orifit'sinfinite,itremainsinfinite,becausehe'sgotarealbigBeeperbag,andsometimeshecaninfinitelymanyBeepersinthere.It'sjust?
wecantalkabouttheinfinityofKarel'sBeeperbagssomeothertime,butifyou'reinterested,it'sfascinating.
AndhecanalsoputBeeper,whichmeanshecangotoacornerandbelike,"Hey,I'mjustfeelinghappy.I'mgoingtoputaBeeperdown,say,onthatcornerrightthere."Exceptyoucan'tthrowthebeepers,right.Karel'skindoflimited.He'sgot,youknow,ifyoulookathimhe'sgotnoarms.He'sgotlegs.Noarms,sobasicallyallhecandoishesortoflikedropsthelittleBeeperwherehe'sat.Sothisputsitonthecornerthathe'sat.
Andthesethingsarewhatwerefertoasmethods.Okay,methodsarebasicallysomeinstructionthatwecancall,okay,oruse,likemove,orturnleft,orpickBeeper,orputBeeper.AndwhatwesayisKarelrespondstothismethod.Whatwe'redoingiswe'reinvoking,orwe'recallingaparticularmethodonKarel,andhetakessomeaction,whichisbasicallywhatthatmethodspecifiestodo.
Okay,soifwekindofthinkaboutthisprogram,orifwekindofthinkaboutthisworld,maybewewantKareltodosomething.Sohere'stheinitialconfigurationoftheworld.MaybewhatwewantKareltodoispickupthisbeeper,dropitoffatthiscorner,andendupatthiscornerfacingtotheeast.Andyoucanthinkabouthowwemightdothatwithsomeoftheinstructionswehave.
Sowhatwewantthefinalconfigurationoftheworldtolooklike,andI'lljustputKarelatblindingspeed.YoucanactuallycontrolKarel'sspeedherewiththislittleslider.SoI'mgoingtojustshowyoutheendconfigurationbyrunningKarelsofastyoucan'tevenseeit.It'sjust?
he'sblindinglyfast.He'sjustthatgood,okay.
Andsothat'swhatwewanttogetto.Noticehe'sstillfacingeast,buthe'spickedupthatbeeperthatwasonthatcornerattwo,one,andmoveditovertothecorneratfour,two.Okay,andsohowmightwedothis?
Right,soonethingwecouldconsideris,whatwastheinitialstateoftheworldagain?
SoI'mjustgoingtorunthisprogramagain,andhereIhaveaclipsetup,whichisyourhandoutnumberfiveexplainshowtogetthis.ButifIwanttorunaprogram,thereisthesetwolittlerunningpersonicons,orasyoumightnotice,weevenhaveourownmenuinEclipse,becauseStanford'sjustthatmuchfun,okay.
SoundertheStanfordmenu,wehavethesetwooptionsforrun.Importprojectisexplainedinthehandout,iswhatyou'lluse.We'llgiveyousomeinitialstuffforKarelthatyou'llstartwith,andyou'lluseimportprojecttogetitintoEclipse.Butonceit'sthere,wecanrunit,andsowhatwe'regoingtodoissayrunthisparticularKarelprogram.Soit'sget?
andwe'llgothroughallthis?
it'sgetandgo.
AndthisisourfirstKarelprogramthatwewanttorun,andit'ssortof,here'stheinitialworldagain,allbigandhappy.SoI'mgoingtoshrinkthisdownalittlebit,sowecanseeitoverhereontheside,whilewethinkaboutwhatcommandsKarelwouldneedtoexecutetokindofgettothatfinalstatewejusttalkedabout.
Andoverhere,happilyenough,letmeresizealittlebit,isourfirstfile,whichisemptyrightnow,thatwe'regoingtowriteourfirstKarelprogram.SowhatcommandsmightweconsiderKarelactuallydoingfromthelistyouhavehere,tosortofaffectw