Programming MethodologyLecture06.docx
《Programming MethodologyLecture06.docx》由会员分享,可在线阅读,更多相关《Programming MethodologyLecture06.docx(26页珍藏版)》请在冰豆网上搜索。
ProgrammingMethodologyLecture06
ProgrammingMethodology-Lecture06
Instructor(MehranSahami):
Allrighty,welcomeback.Ifyouhaven抰turnedintheassignmentyetandyou,atsomepointwantto,turnitintothatboxupthere.
Soacouplequickannouncementsbeforewestart.Firstofwhich,firstofwhichisthequarterisalready1/5ofthewayover,right?
Aftertodayit抯liketwoweeksoftenweekareover,sothat抯hardtobelieve.
Butthere抯threehandoutsinthebackincludingyournextassignmentbecausethefunneverstops;whenoneassignment抯duethenextassignmentgoesout,andacoupleotherhandouts.Assignmentone,asyouknow,isduetodaysopleasedropitoffintheboxinfrontand,congratulations,you抣lall?
well,assumingyoudidn抰takealatedayyou抮eallprogrammers,right?
Becausehopefullyyoualldid,Karel,yougothimtorunaroundtheworldanddostuff.Hopefullyfiguredoutsomeinterestingoutrhythmsandnowyoucanturnitin.Question?
Student:
Ididn抰knowifyouneededahardcopy[inaudible].
Instructor(MehranSahami):
Unhuh,youwanttogetahardcopyinassoonasyoucan,likerightafterclass.Buthardcopiesareimportantbecausewewantyoutoturninbothbecauseweusetheelectricsubmissiontobeabletoactuallyrunitandyoursectionleadermakescommentsonthehardcopyandsoit抯importanttohaveboth.
Butjustbecauseyouasked?
Allrighty.SoIwanttotakeaquickpainpolebeforewestart.Solet抯actuallydiveintotherealsortofmeaningfulthings.Whatthepainpolereallyis?
rememberIaskedyoutothinkabouthowmuchtimeitactuallytookyoutodotheassignment?
SototalitupoveralltheKarelproblems;howmanytotalhours;thinkaboutit,ittookyoutoactuallydotheassignment.Right?
Andwe抮ejustgoingtogothroughanddoaquickshowofhands.
Howmanypeopleactuallygotthroughtheassignmentinzerototwohours?
Allright.Maybelikeacouplepeople.I抣lmakeasmallbar.Howabouttwotofour?
Afairnumber.Fourtosix?
That抯goodtosee.Sixtoeight;eighttoten;tento12;12to14;14to16;16plus?
Rockon.Thanksforadmittingit.It抯agoodtime.Hopefullyitwasagoodtime.
Butthat抯?
firstofall,onethingtonoteistheworldissurprisinglynormal.Right?
Likeeverythingintheworldisjustnormallydistributed,that抯justthewayitis.
Thesecondthingisthatcomputerprogramming,rightorsoftwareengineeringisaprettyhighvarianceevent,rightthatyoucangofromlessthantwohoursto16plus.Idon抰jokewhenIsayit抯actuallyveryhighvariance.Buthopefullywhatthisgiveyouisagain,whatweshootfor,right,isabouttenhoursperweekoutsideofclassfor,youknow,work.WeshootforhereandasImentioned,youknow,itlookslikeyou抮ealldoingrealwellbecauseyou抮esortofbelowtheaveragepoint.Thetruthofthematteriskindofasthequartergoeson,theassignmentstendtogetalittlebitharderwhichmeansyou抣lactuallyseethiscurvekindofmovedownalittlebitmoreintothatrange.Thisisgoodtimestoactuallyseeithererightnow.
Italsohopefullygivesyouachancetogageforyourselfhowyou抮edoingsortofrelativetoexpectations.Right?
Ifyou抮esortofdoingreal?
youknow,hopefullyyouputinyourcommentsanditwasgoodsoftwareengineeringandeverythingandI抦totallywillingtobelieveitwasandyoujustwroteyourcodeanditjustallworkedanditwasbeautifuland,sortof,ifyou抮edoneinthisend,aslongasyou抮estillfeelinglikeyou抮eunderstandingtheconceptsandyou抮epluggingaway,that抯importantandjustkeeppluggingandyouwilldojustfine,trustme.
TherehavebeentimeswhenIhavebeendownheremyselfanditwasn抰funwhenIwasthere,butyoujustkeeppluggingawayanditworksout.
Andbuttheimportantthingisifyouweresortofinaparticularrange,evenifyou抮einthisrangeandthingsjustworkedbutyoudidn抰understandwhy,that抯moredangerousthanbeinginthisrangeandunderstandingwhybecausealltheconceptsinthisclasswillbuildontopofeachother.Somakesureyouunderstandtheconceptsnotjustthat,ohKarelhappenedtodotherightthing.Yes,hegottotherightspotinthemiddleoftheworld,butnowhe抯justspinningaround.That抯fineifhe抯justspinningaround,hejustgotthatmiddlespot,right.We抮ekindofalock,likeyoujustthrowinenoughinstructionsuntilIkindofdidtherightthing;that抯notrealgoodunderstandingandyouwanttotalktome,talktoyoursectionleader,talktotheTAtotrytoclearthatup.
Allright,sowiththatsaidwe抮ejustgoingtodiveinbecausethere抯aquestionintheback.
Student:
Isitanhonorableviolationifyoulookatsomeoneelse抯codeonceyoualreadybothhandedyouassignmentsinandgottenitback?
Instructor(MehranSahami):
Onceyou抳egottenitbackandit抯alreadygraded,it抯finetoactuallybeabletolookatsomeoneelse抯codebecauseatthatpointyoucanjustkindof,youknow,shareideas.
Allright,anyotherquestions?
Allrighty,soacouplethingstocoverrealquickly.Lasttimewetalkedallaboutmethodsandsomemoreaboutobjects.There抯twothingsyoushouldknowintheprogramsthatyou抮egoingtobedoing,iswetalkedalittlebitaboutoneofthemlasttimeintermsofhowtogetinputfromtheuser.There抮ethesefunctionsthatyoushouldknowabout.
OneiscalledREADINTandthere抯somepromptinsidedoublequotesthatyougiveandwhatthatdoesisasktheuserbasicallyforanintegerandgivesyoubacksomevaluethatyoucansay,assigntoaninteger.There抯alsoaversionofthistogetdoubles,whichsurprisinglyenoughiscalledREDOUBLEandhasexactlysortofthesameproperties.Soit抯calledREDOUBLE;ithassomestringhereasit抯parameterorsometexthereinitsparameterinsidedoublequoteswhichitdisplaystothescreenandthengetsyoubackavaluewhichisadoubleoneyoucanassigntoadouble.Thosearejusttwothingsoffthebatthatyoushouldknowaboutbecausethat抯howyou抮egoingtogetinput,atleastforthetimebeing,fromtheuserinalotofcases.
Now,onethingyouwanttodoonceyouactuallygetsomeinputfromtheuseris,youwanttodosomemanipulationonitlikesomeexpressionsthatwetalkedaboutlasttime.Wetalkedaboutsomeofthedifferentoperatorslikeaddition,subtractionorunaryminus,it抯thesamesymbol,multiplication,divisionandmyfavorite,theremainder.Andsowetalkedaboutallthoseexceptforthislittleguylasttime.Alloftheoperatorskindofworkthewayyouwouldexpectthemto,okay.Andwe抣ltalkalittlebitmoreaboutdivisioninjustasecond.Theinterestingthingaboutdivision?
soallofthesethingsworkwithboth?
orIshouldsay?
alloftheseworkwithbothintegersanddoubles.Theremainder,aswetalkedabout,onlyworkswithintegers,rightbecauseitdoesn抰makesensetohavearemainderwhenyouhaverealvalues.
Thesethreeguysworkexactlythesameforintegersanddouble,justthewayyouwouldexpectaddition,multiplication,allthathappystuff,towork.Divisionkindofrearsitsuglyheadbecauseitactuallyworksslightlydifferentlyifyou抮edoingdivisionforintegersversusdoubles.Okay?
Thewholepointofthatis,ifyou抮edoingadivisionandthetwoargumentsthatyou抮edividing,rightifbothofthesethingsareintegers;inthiscaseIhaveintegerconstantwhichiswhatImean,thevalues,right.Ifbothoftheseintegers,whatitdoesisintegerdivisionwhichmeansitdoesthedivisionandthrowsawayanyremainder.Sowhatyougetbackisaninteger.So5dividedby2whentheseareintegersgivesyoubacktheValue2.Thatlittleremainderthingisjustgone.Ifyouwanttogettheremainderyouusethisguy.Okay?
Ifeitheroneoftheseparticularvalueshappenstobearealvalue,likeadouble,thenitwilldoreal-valuedivisionandgiveyoubackarealvalue.Soifyouhappentodivide5,evenif5isaninteger,bytheValue2.0andsoitknowsit抯arealvaluebecauseit抯gotadecimalpointinit,thiswillgiveyouback2.5asadoubleandsoyoucanassignthattoadouble.Okay?
Soifeitheroneoftheargumentsisadouble,yougetreal-valuedivision;ifthey抮ebothintegers,yougetbacktheintegerportion.Unhuh?
Student:
I抦alittleconfusedaboutthedouble;thedoubleisjustarealnumber?
Instructor(MehranSahami):
It抯justarealnumber.Yes.
Soanotherthingthatkindofcomesupwhenyoudoexpressions?
yeah,sometimesyou抮etakingnotesandyoujustdon抰know;it抯likecandyrainingfromthesky.
Theotherthingtokeepinmindisjustlikearithmetic,sometimesyouwantoperatorstoevaluateindifferentorder.There抯anorderprecedentforhowthesethingsactuallyevaluateincaseyouhavetohavesomebighonkingexpression.Theorderofprecedentisyoucanhaveparentheses.Parenthesesarethehighestprecedent.Thatmeansyouevaluateeverythinginparenthesesfirst,thenmultiplication,divisionandtheremainderoperatorhavethesamelevelofprecedents.Andsoifyouhavemultipleofthem;they抮eevaluatedfromlefttorightandthenadditionandsubtraction.Again,ifyouhavemultiple,evaluatelefttoright.
Soit抯justlikeregularrulesofprecedentinalgebra,whichhopefullyyou抮efamiliarwith,buttomakethatconcretelet抯sayyouhavesomeintegerXandwesayXequals1plus3times5dividedby2.Howdoesthatactuallyevaluate?
Wellfirstofall,wesaydowehaveanyparens?
Nowedon抰haveanyparens.Thatwouldbethehighestlevelofprecedence.Youcanalwaysforcesomethingtoevaluatemorehighlybyputtingitinparens.Sotheseguysareallatthesamelevel,soweevaluatelefttoright.Sowecomeacrossandwesayhere,here抯multiplication,weevaluatethisthing