Docsity

Code HS Answers: Lessons 1-20 - Karel Programming Exercises and Solutions, Exams of Software Development

Toggle sidebar
bg1
Code HS Answers: Lessons 1-20 100% pass
Newest
Which II!is II!a II!valid II!Karel II!command?
move; II!
MOVE
move(); II!
move() II!- II! II !Correct II!Answers II!move();
II!What II!is II!a II!street II!in II!a II!Karel II!world? II!- II! II!Correct II!Answers II!Row
II!What II!is II!an II!avenue II!in II!a II!Karel II!world? II!- II! II!Correct II!Answers II!Column
II!If II!Karel II!starts II!at II!Street II!1 II!and II!Avenue II!3 II!facing II!East, II!what II!street II!(row) II!and II!avenue II!(column) II!will II!Karel II!be II!on
II!after II!this II!code II!runs?
move();
move();
move();
turnLeft();
move(); II!- II! I I!Correct II!Answers II!Street II!2 II!Avenue II!6
II!If II!Karel II!is II!facing II!North II!and II!the II!code II!
turnLeft();
turnLeft();
runs; II!which II!direction II!is II!Karel II!facing II!now? II!- II! II !Correct II!Answers II!South
II!How II!many II!times II!should II!Karel II!turn II!left II!in II!order II!to II!turn II!right? II!- II! II !Correct II!Answers II!3
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Code HS Answers: Lessons 1-20 - Karel Programming Exercises and Solutions and more Exams Software Development in PDF only on Docsity!

Code HS Answers: Lessons 1- 20 100% pass

Newest

Which II!is II!a II!valid II!Karel II!command? move; II! MOVE move(); II! move() II!- II! II!Correct II!Answers II!move(); II!What II!is II!a II!street II!in II!a II!Karel II!world? II!- II! II!Correct II!Answers II!Row II!What II!is II!an II!avenue II!in II!a II!Karel II!world? II!- II! II!Correct II!Answers II!Column II!IfII!Karel II!starts II!at II!Street II! 1 II!and II!Avenue II! 3 II!facing II!East, II!what II!street II!(row) II!and II!avenue II!(column) II!will II!Karel II!be II!on II!after II!this II!code II!runs? move(); move(); move(); turnLeft(); move(); II!- II! II!Correct II!Answers II!Street II! 2 II!Avenue II! 6 II!IfII!Karel II!is II!facing II!North II!and II!the II!code II! turnLeft(); turnLeft(); runs; II!which II!direction II!is II!Karel II!facing II!now? II!- II! II!Correct II!Answers II!South II!How II!many II!times II!should II!Karel II!turn II!left II!in II!order II!toII!turn II!right? II!- II! II!Correct II!Answers II! 3

II!What II!can II!be II!used II!toII!teach II!Karel II!to II!turn II!right? II!- II! II!Correct II!Answers II!functions II!Which II!function II!will II!teach II!Karel II!how II!toII!spin II!in II!a II!circle II!one II!time? function II!spin() II!{ II!turnRight(); } function II!spin() II!{ II!turnLeft(); II!turnLeft(); II!turnLeft(); II!turnLeft(); } function II!spin() II!{ II!turnLeft(); II!turnLeft(); } function II!spin() II!{ II!move(); II!move(); II!move(); II!move(); } II!- II! II!Correct II!Answers II!function II!spin() II!{ II!turnLeft(); II!turnLeft(); II!turnLeft(); II!turnLeft(); }

makeSide(); function II!makeSide(){ II!move(); II!putBall(); II!move(); II!putBall(); II!move(); II!putBall(); } goHome(); function II!goHome(){ II!turnRight(); II!move(); II!putBall(); II!move(); II!putBall(); II!move(); II!putBall(); II!turnLeft(); II!turnLeft(); II!turnLeft(); II!move(); II!move(); II!move(); II!turnLeft(); II!turnLeft(); }

II!2.5.4: II!Pancakes II!with II!Start II!- II! II!Correct II!Answers II!function II!start(){ II!move(); II!makePancakes(); II!move(); II!move(); II!makePancakes(); II!move(); II!move(); II!makePancakes(); II!move(); } function II!makePancakes(){ II!putBall(); II!putBall(); II!putBall(); } II!2.6.4: II!The II!Two II!Towers II!- II! II!Correct II!Answers II!function II!start(){ II! II!move(); II!turnLeft(); II!makeTower(); II!move(); II!turnRight(); II!move(); II!move(); II!turnRight(); II!move();

II!turnAround(); II!putBallsInRow(); } // II!Puts II!down II!four II!balls II!in II!a II!row function II!putBallsInRow(){ II!move(); II!putBall(); II!move(); II!putBall(); II!move(); II!putBall(); II!move(); II!putBall(); } function II!makeDiagonal(){ II!turnRight(); II!move(); II!turnRight(); II!move(); II!putBall(); II! II!turnLeft(); II!move(); II!turnRight(); II!move(); II!putBall();

II!turnLeft(); II!move(); II!turnRight(); II!move(); } function II!turnRight(){ II!turnLeft(); II!turnLeft(); II!turnLeft(); } function II!turnAround(){ II!turnLeft(); II!turnLeft(); } II!2.6.6: II!Reflection: II!Top II!Down II!Design II!- II! II!Correct II!Answers II!Top II!down II!design II!is II!a II!way II!of II!designing II!your II!program II!by II!starting II!with II!the II!biggest II!problem II!and II!breaking II!it II!down II!intoII!smaller II!and II!smaller II!pieces II!that II!are II!easier II!toII!solve. II!2.7.4: II!The II!Two II!Towers II!+ II!Comments II!- II! II!Correct II!Answers II!/* II!* II!Karel II!is II!going II!to II!make II!towers II!*/ II! II!function II!start(){ II! II!move(); II!turnLeft(); II!makeTower();

II! } II!Which II!answer II!choice II!describes II!the II!game II!with II!the II!lowest II!level II!of II!abstraction? II!- II! II!Correct II!Answers II!First, II!Steph II!Curry II!flexed II!his II!left II!and II!right II!quad II!muscles II!at II!the II!exact II!same II!time, II!then II!he II!flexed II!each II!muscle II!in II!his II!fingers... II!Karel II!needs II!toII!pick II!up II!a II!pile II!of II!tennis II!balls. Which II!of II!the II!following II!start II!functions II!solves II!the II!problem II!at II!the II!highest II!level II!of II!abstraction? II!- II! II!Correct II!Answers II!function II!start() II!{ II!moveToPile(); II!takePile(); } II!2.8.4: II!Abstracting II!Your II!Day II!- II! II!Correct II!Answers II!I II!made II!this: II!Its II!dumb II!don't II!blame II!me. II!(Basic II!101)

  • II!I II!slipped II!on II!my II!shoes.
  • II!I II!first II!had II!toII!put II!on II!socks. II!Then II!I II!used II!my II!right II!hand II!toII!pull II!the II!tag II!of II!the II!shoe II!so II!it II!can II!slip II!on.
  • II!I II!first II!clapped II!down II!on II!the II!sock II!with II!both II!hands II!and II!gradually II!made II!it II!slide II!up II!my II!foot. II!Then II!I II!increased II!tension II!in II!my II!right II!hand II!soII!I II!can II!pull II!the II!tag II!of II!the II!shoe, II!when II!I II!got II!hold II!ofII!the II!shoe. II!I II!relaxed II!my II!fingers II!except II!my II!pointer II!finger. II!And II!I II!pulled II!on II!the II!tag II!gently II!soII!the II!shoe II!can II!slip II!on.
  • II!I II!played II!COD II!(Call II!ofII!Duty)
  • II!I II!first II!took II!out II!the II!xbox II!controller II!and II!then II!I II!booted II!the II!xbox. II!After II!that II!I II!started II!toII!play II!COD.
  • II!I II!first II!took II!the II!xbox II!controller II!off II!the II!counter II!with II!minimal II!effort II!of II!my II!muscles. II!Then II!I II!booted II!the II!xbox II!with II!my II!pointer II!finger. II!After II!that II!I II!sat II!down II!resting II!my II!glutes. II!Then II!I II!relaxed II!my II!fingers II!on II!the II!stick II!layout. II!And II!I II!started II!to II!play, II!after II!a II!while II!my II!fingers II!and II!my II!muscles II!started II!toII!get II!tense II!from II!all II!the II!action. II!2.8.5: II!Reflection: II!Abstraction II!- II! II!Correct II!Answers II!What II!is II!abstraction? II!Describe II!it II!in II!your II!OWN II!words. For II!instance II!if II!I II!use II!less II!abstraction II!it II!is II!going II!to II!be II!easier II!to II!understand. II!Less II!abstract. II!Like II!the II!art. II!But II!ifII!I II!use II!high II!abstraction II!then II!it II!is II!going II!toII!be II!harder II!toII!understand. II!Like II!it II!is II!just II!said,II!not II!explained.

Example: Less II!-> II!Well II!I II!played II!GTA II! 6 II!today. II!I II!know II!it II!is II!not II!released II!yet, II!but II!I II!got II!to II!have II!a II!chance II!to II!play II!it II!on II!the II!Asus II!PC II!that II!has II!RTX II! 2080 II!ti II!graphics II!that II!are II!in II!sli. II!Meaning II!I II!got II!buttery II!smooth II!games, II!and II!lets II!not II!start II!talking II!about II!the II!4k II!resolution. II!Well II!I II!first II!was II!pumped II!toII!play II!a II!unreleased II!game. II!Second, II!I II!got II!handed II!the II!controller II!from II!my II!admin II!who II!was II!at II!the II!convention II!hosting II!the II!GTA II! 6 II!gameplay II!feed. II!Then II!I II!started II!toII!"grind" II!and II!play II!some II!missions. II!Soon II!my II!fingers II!went II!from II!relaxed II!toII!"gaming II!mode" II!and II!I II!felt II!as II!ifII!I II!was II!in II!the II!game. ~ II!This II!did II!not II!happen. II!:D II!2.8.6: II!Abstraction II!in II!Karel II!- II! II!Correct II!Answers II!Abstraction II!helps II!us II!write II!programs II!by: When II!we II!want II!to II!create II!something II!such II!as II!a II!function/command II!we II!need II!to II!explain II!it II!in II!detail. II!Meaning II!we II!need II!to II!add II!less II!abstraction II!to II!help II!us II!understand II!the II!command II!in II!a II!simpler II!way. II! Less II!Abstraction: II!Less II!abstracted II!- II!Abstract II!art. II!Less II!abstract II!more II!details. More II!Abstraction: II!More II!abstracted II!- II!Abstract II!art. II!More II!abstract II!less II!details. II!2.9.4: II!The II!Two II!Towers II!+ II!SuperKarel II!- II! II!Correct II!Answers II!function II!start(){ II!//Karel II!will II!build II!twoII!towers II!move(); II! II!turnLeft(); II! II!makeTower(); II!move(); II! II!turnRight(); II!move(); II! II!move(); II! II!turnRight(); II!move(); II! II!makeTower(); II! II!turnLeft(); II!turnLeft(); II!move(); II!

II!2.5.5: II!Digging II!Karel II!with II!Start II!- II! II!Correct II!Answers II!function II!start(){ II!move(); II!buryBall(); II!move(); II!buryBall(); II!move(); II!buryBall(); } function II!turnRight() II!{ II!turnLeft(); II!turnLeft(); II!turnLeft(); } function II!buryBall() II!{ II!move(); II!turnRight(); II!move(); II!move(); II!move(); II!putBall(); II!turnLeft(); II!turnLeft(); II!move(); II!move(); II!move(); II!turnRight(); II!move();

II!2.10.7: II!For II!Loop II!Square II!- II! II!Correct II!Answers II!function II!run(){ II! II!for(var II!i II!= II!0; II!i II!< II!4; II!i++){ II!putBall(); II!move(); II!turnLeft(); II!} } function II!start(){ II!run(); } II!2.10.8: II!Lots II!of II!Hurdles II!- II! II!Correct II!Answers II!function II!jumpHurdle(){ II!for(var II!i II!= II!0; II!i II!< II!5; II!i++){ II!move(); II!move(); II!turnLeft(); II!move(); II!turnRight(); II!move(); II!turnRight(); II!move(); II!turnLeft(); II!} } function II!start(){ II!jumpHurdle(); }

II!2.11.5: II!Is II!There II!a II!Ball? II!- II! II!Correct II!Answers II!function II!start(){ II! II!safeTakeBall(); II!putBall(); II!move(); } function II!safeTakeBall(){ II!if(ballsPresent()){ II!takeBall(); II!} } II!2.12.5: II!Right II!Side II!Up II!- II! II!Correct II!Answers II!function II!start(){ II!if(facingSouth()){ II!turnLeft(); II!}else{ II!turnRight(); II!turnRight(); II!} } II!2.13.4: II!Follow II!The II!Yellow II!Ball II!Road II!- II! II!Correct II!Answers II!function II!start() II!{ II!while II!(ballsPresent()) II!{ II!move(); II!} } II!2.13.5: II!Lay II!Row II!of II!Tennis II!Balls II!- II! II!Correct II!Answers II!function II!start(){ II!while II!(frontIsClear()) II!{

II!putBall(); II!move(); II!} II!putBall(); } II!2.13.6: II!Big II!Tower II!- II! II!Correct II!Answers II!function II!start(){ II!faceNorth(); II!buildTower(); II!putBall(); } II! //This II!function II!make II!Karel II!face II!north II!is II!Karel II!is II!facing II!west. function II!faceNorth(){ II!if(facingWest()){ II!turnRight(); II!} II!if(facingEast()){ II!turnLeft(); II!} II! II!if(facingSouth()){ II!turnLeft(); II!turnLeft(); II!} } //This II!function II!makes II!Karel II!put II!the II!tennis II!balls II!up II!to II!the II!top II!ofII!the II!"world". II! function II!buildTower(){ II!while(frontIsClear()){

II!move(); II!} II! II!turnLeft(); II!while(frontIsClear()){ II!if(rightIsBlocked()){ II!putBall(); II!move(); II!while(rightIsClear()){ II!move(); II!} II!} II!if(frontIsBlocked()){ II!putBall(); II!} II!} } II!2.16.4: II!Diagonal II!- II! II!Correct II!Answers II!//This II!program II!has II!karel II!lay II!a II!diagonal II!row II!of II!tennis II!balls. II!However, II!the II!indenting II!is II!all II!wrong. function II!start(){ II!while(frontIsClear()){ II!putBall(); II!move(); II!turnLeft(); II!move(); II!for(var II!i II!= II!0; II!i II!< II!3; II!i++){

II!turnLeft(); II!} II!} II!putBall(); } II!2.16.5: II!Staircase II!- II! II!Correct II!Answers II!/* II!This II!program II!creates II!a II!staircase II!from II!the II!first II!spot II!all II!the II!* II!way II!across II!the II!world II!for II!any II!sized II!world. II!* II!This II!program II!works, II!but II!its II!indentation II!is II!completely II!wrong. II!* II! II!* II!Run II!the II!program II!first, II!so II!you II!know II!what II!it II!does II!and II!don't II!break II!it. II!*/ function II!start(){ II!putBall(); II!while(frontIsClear()){ II!turnLeft(); II!while II!(ballsPresent()) II!{ II!move(); II!} II!turnRight(); II!move(); II!createStep(); II!} } function II!createStep() II!{ II!turnRight(); II!putBall(); II!while II!(frontIsClear()) II!{