Karel

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What does the mystery function do? function mystery() { while (noBallsPresent()) { move(); } }

Karel moves until it is on a ball.

What is the proper format for a single line comment in Karel?

// This is a comment

Say Karel is on a location with one tennis ball. After the following code runs, how many tennis balls will there be at that location? for (var i = 0; i < 3; i++) { if (ballsPresent()) { takeBall(); } else { putBall(); putBall(); } }

1

Why do we use functions in Karel programs?

Break down our program into smaller parts/ Avoid repeating code/ Make our program more readable.

How can we teach Karel new commands?

Define a new function.

Say you want to write a program to have Karel put down 300 tennis balls. Which control structure would you use?

For loop

In the following code, what would be a good Postcondition to write? function getOnTop() { turnLeft(); move(); turnRight(); }

Karel ends one spot above a tennis ball facing East.

Karel starts at Street 1, Avenue 1, facing East in a 5x5 world. What will happen after this code runs? move(); move(); turnRight(); move();

Karel will crash into a wall

If Karel starts at Street 1 and Avenue 1, facing East, where will Karel be, and what direction will Karel be facing after running the following code? (Assume the world is 10x10 in size) move(); turnLeft(); putBall(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft();

Street 1, Avenue 3, Facing North

Karel starts at Street 1 and Avenue 1, facing East. After calling the stairStep function twice, where will Karel be and what direction will Karel be facing? (assume this is a SuperKarel program and the world is 10x10 in size) function stairStep() { move(); turnLeft(); move(); turnRight();

Street 3, Avenue 3, Facing East

What's wrong with this code? function start() { move(); go(); go(); } function go() { move(); move(); } function go() { move(); move(); }

The go function has been defined twice

What makes the following command an invalid Karel command? turnleft();

The l should be a capital L

Karel starts at Street 1, Avenue 1, facing East in a 5x5 world. What will happen after this code runs? move(); putball(); move(); move(); move(); move(); move();

This code won't run because of a syntax error.

In the following code below from the Cleanup Karel example, what is the purpose of If Statement #2? // This program has Karel walk down the // row and clean up all of the tennis balls // on the way. function start() { while (frontIsClear()) { // If statement #1 if (ballsPresent()) { takeBall(); } move(); } // If statement #2 if (ballsPresent()) { takeBall(); } }

To pick up the ball that is in the last spot, if there is one.

What is the purpose of using a for loop in code?

To repeat something a fixed number of times.

How can we improve the following program? function start() { move(); move(); move(); move(); move(); move(); move(); move(); move(); }

Use a for loop to repeat the move command

What condition should be used in this while loop to get Karel to pick up all the tennis balls on the current location? while (________) { takeBall(); }

ballsPresent()

What is the correct way to define a turnRight function in Karel?

function turnRight() { turnLeft(); turnLeft(); turnLeft(); }

How many total times will Karel move in this program? function start() { move(); for (var i = 0; i < 5; i++) { move(); putBall(); } }

9

What is top down design?

A way of designing your program by starting with the biggest problem and breaking it down into smaller and smaller pieces that are easier to solve.


Kaugnay na mga set ng pag-aaral

MGT: Final (Ch. 10,11,12,13,14,16)

View Set

Pressure-Volume Loops of the Left Ventricle

View Set

History 50 Multiple Choice Questions

View Set

Algebra 2: 7.05 Quiz: Coterminal Angles

View Set