25% developed

Kojo explorations/Commands and programs

From Wikibooks, open books for an open world
Jump to navigation Jump to search

This activity involves the following:

  • Learning about commands, actions, and programs.
  • Learning to draw lines using the turtle.
  • Learning the clear, forward, and right commands.
  • Exploring the ideas of unit length, distances, and right angles, and using them to make a square geometrical figure.
  • Using the Kojo error recovery feature.

Step 1[edit | edit source]

Type in the following code within the Script Editor and run it:

forward(100)

Q1a) What does the turtle do? Does it move? By how much? In what direction?

Step 2[edit | edit source]

Clear the line made on the Drawing Canvas in the previous step by right-clicking on the Canvas and pressing Clear. Then delete the text in the script editor by pressing the Clear Editor toolbar button.

Now type in the following code and run it:

showAxes()
forward(200)

Q2a) What do you think the showAxes command does? Does it show you the unit of length (called a pixel) used for drawing on the turtle canvas? 10

Q2b) What do you think the forward command does? What does the input to the command specify? The input to the forward command is the number that you write within round brackets after the command, e.g., forward(100) has 100 as its input.

Note – when you are asked to figure out what a command does, feel free to fire up a Kojo Scratchpad (using the File -> New Kojo Scratchpad menu item) to experiment with different inputs to the command. A Kojo Scratchpad is an instance of Kojo for doing “rough work” and figuring out things – as you work on an activity inside Kojo.

Note – The Turtle Commands Quickref chapter contains descriptions of commonly used turtle commands. You should go over to that chapter and validate your understanding of a command after you figure out what it does.

Step 3[edit | edit source]

Clear the Drawing Canvas and Script Editor. Then type in the following code and run it:

right()

Q3a What do you think the right command does?

Step 4[edit | edit source]

Clear the Script Editor (but not the drawing canvas). Then type in the following code and run it:

clear()

Q4a) What does the clear command do?

Step 5[edit | edit source]

Clear the Script Editor. Then type in the following code and run it. But first guess (before running the code) what figure is made by this program:

clear()
forward(100)
right()
forward(100)
right()

Q5a) How is it useful to have the clear command as the first line of your program?

Step 6[edit | edit source]

Clear the Script Editor. Then type in the following incorrect code and run it:

clear()
forwardx(100)

Q6a) What does Kojo tell you (in the output pane)? Observe the kind of message that Kojo shows you when you give it an incorrect command to run.

Q6b) Using this message, can you determine (and go to) the line in your program that has the problem? Hint – click on Locate error in script in the Output pane.

Self Exploration[edit | edit source]

Play with the clear, forward, and right commands before you move on to the exercise. Deliberately make a few mistakes (misspelled commands, missing round-brackets) and then try to fix the mistakes with the help of the Kojo error messages.

Theory[edit | edit source]

A program is a series of instructions for the computer.

These instructions can be of a few different kinds. The first kind of instruction (the kind that you have seen in this activity) is a command. A command makes the computer carry out an action (like moving the turtle forward) or indirectly affects future actions (like setting the pen color).

Actions are effects produced by your program that you can see, hear, etc. They result in outputs from your program.

Commands can take inputs. These inputs let you control what the command does, e.g., the input to the forward command lets you control the length of the line that the turtle draws. A command with no inputs always does exactly the same thing.

Exercise[edit | edit source]

Write a program to make the following figure: Sides of the square – length = 100 pixels.

Exercise square
Exercise square