Вы находитесь на странице: 1из 4

AP Computer Science A Karel J Robot Review Sheet Chapters 5-6 (I an!

"oops#

Name: ________________________ Date: _____________ Period: _____

1. "ist the $ primitive pre!icates %rom the Robot class that can be use! in an &i%' clause : an()eepersIn)eeper)a*(# %acin*+orth(# ne,t-oARobot(# %acin*South(# ne,t-oA)eeper(# %acin*.ast(# %rontIsClear(# %acin*/est(# 2. Rewrite the %ollowin* section o% co!e to simpli%( it : if (frontIsClear()) { move(); } else { turnLeft(); move(); } i% ( 0 %rontIsClear(## 1 turn"e%t(#2 3 move(#2

. Rewrite the %ollowin* section o% co!e to simpli%( it : if (fa!in"#est()) return false; else return true; return 0%acin*/est(#2

$. /hen is each o% the %ollowin* iteration structures use!% a. while & In!e%inite loop to per%orm instructions an in!e%inite number o% times (until a certain test %ails# '. %or & 4e%inite loops to per%orm instructions an e,plicit number o% times !. ((rue ) alse) -he choice o% which loop construct to use (which tool# is an arbitrar( !ecision le%t up to (ou an! (our pre%erence5 It !epen!s on the re6uirement5 *. "ist the 7 steps %or buil!in* a while loop5 Also8 in section 6568 what are the 9 thin*s we shoul! A"/A:S chec; when writin* an( loop to help veri%( its correctness . Step <) I!enti%( what is true when the loop is %inishe! (this is alwa(s easier than !eterminin* what is %alse while it is still e,ecutin*# Step 9) =se the opposite %orm o% step <>s result as the ?test@ %or the loop5 :ou Aust nee! to ne*ate the entire thin*5 :ou ma( nee! to use Bor*an>s "aw to !o this5 Step C) Ba;e pro*ress towar! the *oal (completion o% the loop# within the loop Step 7) 4o whatever is re6uire! be%ore an!Dor a%ter the loop to ensure that we solve the *iven problem Chec; <) Show that the metho! wor;s correctl( when the initial situation results in the test bein* %alse5 Chec; 9) /e must show that each time the loop bo!( is e,ecute!8 the robot>s new situation is a simpler version o% the ol! situation5 )( simpler8 we mean that the robot now has less wor; to !o be%ore %inishin* the loop5

+ri"inal do!ument ,rovided '- ....a,Com,uter/!ien!e.!om and modified '- 0r. /mit1 for 1is s,e!ifi! !lass

2. /hat are the two possible outcomes when a robot o% the Robot class e,ecutes the %ollowin* metho! (assumin* (ou !o not ;now what the worl! loo;s li;e#% Assume that %ace.ast is !e%ine! correctl(5 ,u'li! void "o3ast() { fa!e3ast(); .1ile (fa!in"3ast()) { move(); } } AnswerE -he robot either runs into a wall or it is in an in%inite loop

F5 /rite the correct s(nta, %or the selection an! iteration control structures below5 :ou shoul! use *eneric statements such ?test@ an! ?instructions@ a) i% ( ?test@ # 1 ?instructions@ 3 else 1 ?instructions@ 3 ') %or ( int ?initialiGe-variable @2 ?test@2 ?increment-variable@ # 1 ?instructions@ 3 !) while ( ?test@ # 1 ?instructions@ 3 4. Rewrite the %ollowin* section o% co!e to simpli%( it : if(frontIsClear()) { if (ne5t(o67o'ot()) { move(); } else { move(); turn7i"1t(); } } else { if (ne5t(o67o'ot()) { +o simpli%ication5

+ri"inal do!ument ,rovided '- ....a,Com,uter/!ien!e.!om and modified '- 0r. /mit1 for 1is s,e!ifi! !lass

move(); } else { ,ut8ee,er(); turn7i"1t(); } } 9. #rite a met1od to ,erform t1e follo.in" lo"i!: Bove %orwar! to a street corner8 pic; up all beepers on the street corner8 then turn o%% . public voi! pic;=p)eepers(# 1 move(#2 while ( ne,t-oA)eeper(# # 1 pic;)eeper(#2 3 turnH%%(#2 3 1:. #rite a se!tion of !ode in t1e /mart8ot ro'ot !lass to ,erform t1e follo.in" lo"i!: I% the robot is %acin* east or north8 then this robot shoul! turn west an! move %orwar! until it runs into a wall or a boun!ar(. i% ( %acin*.ast(# II %acin*+orth(# # 1 %ace/est(#2 while ( %rontIsClear(# # 1 move(#2 3 3 11. /rite a %or loop to pic; up %ive beepers in a strai*ht line . %or ( int iJ<2 i?J52 iKK # 1 pic;)eeper(#2 move(#2 3 12. 4escribe the %ollowin* terms: a. +ame the three )oolean operators: 0 (not#8 LL (an!#8 II (or# '. +este! instructions: I instruction neste! within the -M.+ or ."S. clause o% another I instruction5 !. Pre!icate: )oolean metho! (i5e5 %rontIsClear(# # d. .,ecution e6uivalence: -wo %ra*ments o% co!e have e,ecution e6uivalence i% the( per%orm e,actl( the same in all situations5
+ri"inal do!ument ,rovided '- ....a,Com,uter/!ien!e.!om and modified '- 0r. /mit1 for 1is s,e!ifi! !lass

e. )ottom %actorin*: actor out the common instructions %rom the bottom o% the -M.+ an! ."S. clause an! place these instructions imme!iatel( below the I instruction5 f. -op %actorin*: actor out the common instructions %rom the top o% the -M.+ an! ."S. clause an! place these instructions imme!iatel( be%ore the I instruction5 +oteE -op %actorin* !oes not alwa(s wor;5 ". Short-circuit evaluation: .,amples o% short-circuit evaluation are as %ollowsE i5 I% (ou have test that has an LL operator an! the %irst boolean e,pression is %alse8 then (ou !o not have to chec; the secon! boolean e,pression (since the test is automaticall( %alse#5 ii. I% (ou have test that has a II operator an! the %irst boolean e,pression is true8 then (ou !o not have to chec; the secon! boolean e,pression (since the test is automaticall( true#5 1. -M.+ clause: Instructions that *et e,ecute! when the I instruction>s test is -R=.5 (within brac;ets imme!iatel( a%ter the i% reserve! wor!#5 i. ."S. clause: Instructions that *et e,ecute! when the I instruction>s test is A"S.5 (within brac;ets imme!iatel( a%ter the else reserve! wor!#5 ;. %or loop: "oop use! to e,ecute a set o% instructions a !e%inite number o% times5 <. while loop: "oop use! to e,ecute a set o% instructions an in!e%inite number o% times (while a speci%ie! con!ition is true#5 l. int variable: Inte*er variable8 such as &i' use! in a %or loop5 m. iKK an! i-- : iKK means to increment i b( < (iJiK<2#5 i-- means to !ecrement i b( < (iJi-<2# n. In%inite e,ecution: /hen the instructions within a loop e,ecute without the loop en!in*5 o. ence post problem: I% we or!er %ive %ence sections8 we nee! si, %ence posts5 -his is a situation where we must !o somethin* either be%ore or a%ter the loop to solve the *iven problem8 since the instructions in the loop !o not entirel( solve the problem %or either the %irst time throu*h the loop or the last time throu*h the loop5 ,. )e(on!-the-horiGon situation: Situations that are le*al8 but coul! cause problems5 An e,ample is when we tr( to create a bo, that is ne,t to one o% the boun!aries5 rien!l( remin!er: In order to 1el, ensure t1e readin" of t1e !1a,ter= I usuall- ,ut a >uestion or t.o from t1e readin" (.1i!1 I didn?t !over in !lass) on t1e test. :&)

+ri"inal do!ument ,rovided '- ....a,Com,uter/!ien!e.!om and modified '- 0r. /mit1 for 1is s,e!ifi! !lass

Вам также может понравиться