


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























In II!the II!following II!code: var II!ball; function II!start(){ II!ball II!= II!new II!Circle(20); II!add(ball); II!setTimer(draw, II!20); } function II!draw(){ II!ball.move(2, II!2); } When II!will II!the II!function II!draw II!be II!called? II!- II! II!Correct II!Answers II!Every II! 20 II!milliseconds II!Which II!statement II!will II!call II!a II!function II!animate II!every II! 50 II!milliseconds? II!- II! II!Correct II!Answers II!setTimer(animate, II!50); II!9.1.5 II!Crazy II!Ball II!- II! II!Correct II!Answers II!var II!RADIUS II!= II!100; var II!circle; function II!start(){ II!circle II!= II!newII!Circle(RADIUS); II!circle.setPosition(getWidth()/2, II!getHeight()/2); II!add(circle); II!setTimer(crazyBall, II!100); } function II!crazyBall(){ II!var II!x II!= II!Randomizer.nextInt(RADIUS, II!getWidth()-RADIUS); II!var II!y II!= II!Randomizer.nextInt(RADIUS, II!getHeight()- II!RADIUS);
II!circle.setPosition(x,y); II!circle.setColor(Randomizer.nextColor()); } II!What II!does II!this II!program II!do? function II!drawCircle(x, II!y, II!radius, II!color){ II!var II!circle II!= II!new II!Circle(radius); II!circle.setPosition(x, II!y); II!circle.setColor(color); II!add(circle); } function II!start(){ II!setTimer(draw, II!50); } function II!draw(){ II!var II!color II!= II!Randomizer.nextColor(); II!var II!centerX II!= II!getWidth()/2; II!var II!centerY II!= II!getHeight()/2; II!var II!radius II!= II!100; II!drawCircle(centerX, II!centerY, II!radius, II!color); } II!- II! II!Correct II!Answers II!Draws II!several II!randomly II!colored II!circles II!at II!the II!centerII!of II!the II!canvas II!by II!drawing II! 1 II!every II! 50 II!milliseconds. II!Which II!statement II!would II!stop II!the II!timer II!in II!the II!following II!program? function II!drawCircle(x, II!y, II!radius, II!color){ II!var II!circle II!= II!new II!Circle(radius); II!circle.setPosition(x, II!y); II!circle.setColor(color); II!add(circle);
II!circle.setColor(Randomizer.nextColor()); II!} II!if(counter II!== II!MAX_RADIUS){ II!stopTimer(draw); II!} } II!9.2.6: II!Paint II!splatter II!- II! II!Correct II!Answers II!var II!CIRCLES_PER_SPLATTER II!= II!20; II!var II!MIN_RADIUS II!= II!5; II!var II!MAX_RADIUS II!= II!25; II!var II!DELAY II!= II!500; /* II!This II!programII!creates II!a II!paint II!splatter. II!* II!It's II!art. II!*/ function II!start(){ II!setTimer(splatter, II!500); } function II!drawCircle(color){ II!var II!paint II!= II!new II!Circle(Randomizer.nextInt(5,25)); II!paint.setColor(color); II!paint.setPosition(Randomizer.nextInt(0, II!getWidth()), II!Randomizer.nextInt(0, II!getHeight())); II!add(paint); } function II!splatter(){ II!var II!color II!= II!Randomizer.nextColor(); II!for(var II!i II!= II!0; II!i II!< II!20; II!i++){ II!drawCircle(color); II!} }
II!Why II!do II!we II!want II!our II!code II!to II!be II!"reusable"? II!- II! II!Correct II!Answers II!All II!of II!the II!above II!How II!can II!we II!make II!our II!code II!reusable? II!-II! II!Correct II!Answers II!All II!of II!the II!above II!Which II!function II!has II!better II!reusability? II!- II! II!Correct II!Answers II!function II!drawRectangle(x, II!y, II!width, II!height, II!color){ II!//Draw II!aII!rectangle II!using II!the II!parameters II!as II!properties II!... } II!9.3.5: II!Circle II!Wall II!- II! II!Correct II!Answers II!// II!Constants var II!RADIUS II!= II!20; var II!DELAY II!= II!40; // II!Define II!your II!global II!variables II!here var II!yPos II!= II!RADIUS; var II!xPos II!= II!RADIUS; var II!counter II!= II!0; function II!start(){ II!setTimer(draw, II!DELAY); } function II!draw(){ II!var II!circle II!= II!new II!Circle II!(RADIUS); II!circle.setPosition(xPos, II!yPos); II!if(counter II!% II! 2 II!==0){ II!circle.setColor(Color.red); II!} II!counter++; II!add(circle); II!xPos II!+= II!2*RADIUS;
II!stopTimer(draw); II!} } II!This II!is II!a II!Circle II!object II!on II!our II!canvas II!named II!ball. II!Point II!A II!is II!on II!the II!far II!left II!edge II!of II!the II!circle. II!How II!can II!we II!calculateII!the II!x-coordinate II!of II!point II!A? II!- II! II!Correct II!Answers II!ball.getX()II!- II!ball.getRadius(); II!Which II!statement II!should II!we II!use II!to II!determine II!ifII!ball II!is II!hitting II!the II!left II!edge II!of II!the II!window? II!- II! II!Correct II!Answers II!if(ball.getX() II!- II!ball.getRadius()II!<= II!0){ II!//ball II!is II!hitting II!left II!edge } II!9.4.4: II!Hotspot II!Ball II!- II! II!Correct II!Answers II!var II!ball; var II!dx II!= II!4; var II!dy II!= II!4; /* II!This II!programII!has II!a II!ball II!bounce II!around II!the II!screen. II!*/ function II!start(){ II!ball II!= II!new II!Circle(20); II!ball.setPosition(100, II!100); II!add(ball); II!setTimer(draw, II!20); } // II!Check II!if II!the II!ball II!has II!reached II!a II!wall. // II!Then II!move II!the II!ball II!in II!the II!correct II!direction. function II!draw(){ II!checkWalls(); II!switchColor(); II!ball.move(dx, II!dy); }
function II!checkWalls(){ II!// II!Bounce II!off II!right II!wall II!if(ball.getX() II!+II!ball.getRadius() II!> II!getWidth()){ II!dx II!= II!-dx; II!} II! II!// II!Bounce II!off II!left II!wall II!if(ball.getX() II!- II!ball.getRadius() II!< II!0){ II!dx II!= II!-dx; II!} II! II!// II!Bounce II!off II!bottomII!wall II!if(ball.getY() II!+ II!ball.getRadius() II!> II!getHeight()){ II!dy II!= II!-dy; II! II!} II! II!// II!Bounce II!off II!top II!wall II!if(ball.getY() II!- II!ball.getRadius()II!< II!0){ II!dy II!= II!-dy; II! II!} } function II!switchColor(){ II!if(ball.getX() II!>= II!getWidth()/2){ II!ball.setColor(Color.green); II!}else{ II!ball.setColor(Color.blue); II!} }
II!counter++; II!} II! II!// II!Bounce II!off II!left II!wall II!if(ball.getX() II!- II!ball.getRadius() II!< II!0){ II!dx II!= II!-dx; II!counter++; II!} II! II!// II!Bounce II!off II!bottomII!wall II!if(ball.getY() II!+ II!ball.getRadius() II!> II!getHeight()){ II!dy II!= II!-dy; II!counter++; II!} II! II!// II!Bounce II!off II!top II!wall II!if(ball.getY() II!- II!ball.getRadius()II!< II!0){ II!dy II!= II!-dy; II!counter++; II!} II!if(counter II!== II!MAX_BOUNCES){ II!stopTimer(draw); II!} } II!Suppose II!we've II!writtenII!a II!function II!drawCircle II!that II!we II!want II!to II!call II!every II!time II!the II!mouse II!is II!clicked. II!How II!can II!we II!do II!this? II!- II! II!Correct II!Answers II!mouseClickMethod(drawCircle); II!What II!is II!a II!callback II!function? II!- II! II!Correct II!Answers II!A II!function II!passed II!to II!an II!event II!handler II!that II!isII!called II!every II!time II!aII!certain II!event II!happens
II!9.5.5: II!Teleporting II!Ball II!- II! II!Correct II!Answers II!var II!ball; var II!dx II!= II!4; var II!dy II!= II!4; /* II!This II!programII!has II!a II!ball II!bounce II!around II!the II!screen. II!*/ function II!start(){ II!ball II!= II!new II!Circle(20); II!ball.setPosition(100, II!100); II!add(ball); II!setTimer(draw, II!20); II!mouseClickMethod(teleport); } function II!teleport(e){ II!ball.setPosition(e.getX(), II!e.getY()); II!ball.setColor(Randomizer.nextColor()); } function II!draw(){ II!checkWalls(); II!ball.move(dx, II!dy); } function II!checkWalls(){ II!// II!Bounce II!off II!right II!wall II!if(ball.getX() II!+II!ball.getRadius() II!> II!getWidth()){ II!dx II!= II!-dx; II!} II! II!// II!Bounce II!off II!left II!wall II!if(ball.getX() II!- II!ball.getRadius() II!< II!0){ II!dx II!= II!-dx; II!} II!
II!if(!paused){ II!checkWalls(); II!ball.move(dx, II!dy); II! II!} } function II!checkWalls(){ II!// II!Bounce II!off II!right II!wall II!if(ball.getX() II!+II!ball.getRadius() II!> II!getWidth()){ II!dx II!= II!-dx; II!} II! II!// II!Bounce II!off II!left II!wall II!if(ball.getX() II!- II!ball.getRadius() II!< II!0){ II!dx II!= II!-dx; II!} II! II!// II!Bounce II!off II!bottomII!wall II!if(ball.getY() II!+ II!ball.getRadius() II!> II!getHeight()){ II!dy II!= II!-dy; II!} II! II!// II!Bounce II!off II!top II!wall II!if(ball.getY() II!- II!ball.getRadius()II!< II!0){ II!dy II!= II!-dy; II!} } II!Which II!statement II!will II!call II!the II!function II!paint II!every II!time II!the II!mouse II!is II!moved? II!- II! II!Correct II!Answers II!mouseMoveMethod(paint);
II!Suppose II!we II!have II!a II!callback II!function II!paint II!that II!isII!called II!every II!time II!the II!mouseII!is II!moved. II!Which II!version II!of II!paint II!will II!draw II!aII!circle II!at II!the II!mouse's II!location? II!- II! II!Correct II!Answers II!function II!paint(e){ II!var II!circle II!= II!new II!Circle(20); II!circle.setPosition(e.getX(), II!e.getY()); II!add(circle); } II!9.6.5: II!CoordinatesII!- II! II!Correct II!Answers II!var II!coors; function II!start(){ II!mouseMoveMethod(newCoor); } function II!newCoor(e){ II!remove(coors); II!coors II!= II!new II!Text II!("("+ II!e.getX() II!+ II!", II!" II!+ II!e.getY()+")"); II!coors.setPosition(150, II!getHeight()/2); II!add(coors); } II!9.6.6: II!Target II!- II! II!Correct II!Answers II!var II!x; var II!horizontalLine; var II!verticalLine; var II!y; function II!start(){ II!mouseClickMethod(addBall); II!mouseMoveMethod(target); } function II!target II!(e){ II!remove(horizontalLine); II!remove(verticalLine);
12 II!function II!start(){ 13 II! 14 II!mouseDownMethod(down); 15 II!mouseDragMethod(drag); 16 II!} II!- II! II!Correct II!Answers II!Line II! 4 II!declares II!a II!new II!local II!variable II!var II!line. II!The II!local II!variable II!line II!in II!down II!is II!different II!from II!the II!global II!variable II!line II!in II!drag, II!but II!we II!need II!these II!functions II!to II!be II!affecting II!the II!sameII!line II!How II!do II!we II!fix II!this II!problem? II!The II!same II!code II!is II!shown II!again II!for II!reference: 1 II!var II!line; 2 II! 3 II!function II!down(e){ 4 II!var II!line II!= II!newII!Line(e.getX(), II!e.getY(), II!e.getX(), II!e.getY()); 5 II!add(line); 6 II!} 7 II!function II!drag(e){ 8 II!line.setEndpoint(e.getX(), II!e.getY()); 9 II! 10 II!} 11 12 II!function II!start(){ 13 II! 14 II!mouseDownMethod(down); 15 II!mouseDragMethod(drag); 16 II!} II!- II! II!Correct II!Answers II!Get II!rid II!of II!the II!varII!in II!Line II! 4 II!so II!that II!the II!line II!in II!down II!refers II!to II!the II!same II!global II!line II!in II!drag II!rather II!than II!a II!local II!variable. II!9.7.4: II!Leash II!- II! II!Correct II!Answers II!var II!BALL_RADIUS II!= II!30; var II!ball; var II!line;
function II!start(){ II!line II!= II!newII!Line(getWidth()/2, II!getHeight()/2,getWidth()/2, II!getHeight()/2); II!line.setColor(Color.black); II!add(line); II!ball II!= II!new II!Circle(BALL_RADIUS); II!ball.setPosition(getWidth()/2, II!getHeight()/2); II!ball.setColor(Color.yellow); II!add(ball); II! II!mouseMoveMethod(endPoint); II! } function II!endPoint(e){ II!ball.setPosition(e.getX(), II!e.getY()); II!line.setEndpoint(e.getX(), II!e.getY()); } II!We've II!written II!a II!function II!animate II!that II!moves II!aII!ball II!across II!the II!screen II!like II!this: function II!start(){ II!//add II!code II!here II!} II!function II!animate(e){ II!ball.move(5, II!5); II!} How II!can II!we II!set II!up II!animate II!to II!be II!called II!every II!time II!a II!key II!on II!the II!keyboard II!is II!pressed II!down? II!- II! II!Correct II!Answers II!function II!start(){ II! keyDownMethod(animate); II! } II!9.8.4: II!Basic II!Snake II!- II! II!Correct II!Answers II!var II!SNAKE_WIDTH II!= II!40; var II!SNAKE_HEIGHT II!= II!40; var II!SNAKE_COLOR II!= II!Color.green; // II!Constants II!to II!represent II!the II!directions var II!EAST II!= II!0; var II!SOUTH II!= II!1;
II!if(e.keyCode II!== II!Keyboard.UP){ II!direction II!= II!NORTH; II!} II!if(e.keyCode II!== II!Keyboard.DOWN){ II!direction II!= II!SOUTH; II!} } II!We II!want II!to II!write II!a II!function II!changeBall II!that II!moves II!a II!ball II!to II!aII!random II!location II!on II!the II!screen II!and II!changes II!its II!color. Which II!instructions II!can II!we II!insert II!at II!the II!//YOUR II!CODE II!HERE II!comment II!to II!change II!the II!position II!of II!ball II!to II!a II!random II!location, II!whileII!still II!keeping II!the II!entire II!ball II!on II!screen? II!- II! II!Correct II!Answers II!var II!x II!= II!Randomizer.nextInt(ball.getRadius(), II! getWidth() II!- II!ball.getRadius()); II!var II!y II!= II!Randomizer.nextInt(ball.getRadius(), II! getHeight() II!- II!ball.getRadius()); II! ball.setPosition(x, II!y); II!Which II!of II!the II!following II!are II!good II!examples II!of II!things II!that II!should II!be II!stored II!as II!global II!variables? I II!- II!A II!ball II!for II!a II!game II!that II!is II!used II!in II!multiple II!functions II II!- II!A II!counterII!that II!keeps II!track II!of II!how II!many II!times II!the II!user II!has II!clicked II!the II!mouse III II!- II!A II!for II!loop II!counter II!variable IV II!- II!The II!color II!of II!a II!rectangle II!that II!is II!only II!used II!in II!one II!function II!- II! II!Correct II!Answers II!I II!and II!II II!only II!In II!the II!following II!code function II!start(){ II! mouseClickMethod(clickHandler); II! } II! function II!clickHandler(e){ II!
//your II!code II!here } How II!can II!we II!get II!the II!graphical II!element II!at II!the II!location II!of II!the II!user II!click? II!- II! II!Correct II!Answers II!var II!elem II!= II!getElementAt(e.getX(), II!e.getY()); II!9.9.7: II!Click II!for II!Collision II!- II! II!Correct II!Answers II!var II!blueCircle; var II!redCircle; var II!RADIUS II!= II!25; var II!DX_RED II!= II!6; var II!DX_BLUE II!= II!4; var II!DELAY II!= II!40; var II!click II!= II!0; var II!paused II!= II!false; function II!start(){ II!blueCircle(); II!redCircle(); II!setTimer(collide, II!DX_BLUE); II!mouseClickMethod(collide); } function II!collide(e){ II!setTimer(blueCircle, II!DX_BLUE); } function II!pause(e){ II!if(paused){ II!paused II!= II!false; II!setTimer(collide, II!DX_BLUE); II!}else{ II!paused II!= II!true; II!stopTimer(collide)