Lesson 9

Realistic Tire Movement - Variables

Pre-Requisites:Lessons 6, 7

Topics:

  1. Object sub-part movements
  2. Variables
  3. Binary Search

Introduction:

In the previous lesson, we make the front tires turn appropriately when we turn the car. Now we will use a variable to control the orientation of the tires and another variable to control how quickly they turn. We will learn how to use variables and touch on binary search.

Demonstration:

For this lesson, we’re going to start with our monitors off.

Variables: You may remember that we told the tires to turn ¼ revolution in 4 different places. Now we need to find a smaller value that looks better, and we're going to use trial and error to find it. With our code the way it is now, we would have to change ¼ revolution to be a new number 4 times each time we want to test a new number. Computers have something called variables that can make this easier.

A variable is used to remember a piece of information. These are used all the time. For example, if you log on to a website, the website knows who you are because it stores that information in a variable. If you pick a music file on your computer and play it, a variable is used to store which file you want to play. When you log off or you pick a different piece of music, the variable gets a new value. In this lesson you're going to use variables and add them to your program.

Since you are the programmer, you'll give each variable a name that makes sense to you. Then every time you want to use the variable, you refer to it by name. The computer will look up the variable name and substitute the value stored in the variable, like the website username and the music file.

Let's pretend that I’m writing a program that addresses you by your name. I would create a variable called NAME to store a name, and then write a few methods that use it.

Write on board:

NAME = Albert Einstein

[Brag]

[AskName]

When my program starts, it is going to think your name is “Albert Einstein,” because that is what the variable “NAME” has stored in it. At the beginning, when I press the “Brag” button, my program will say, “Albert Einstein, you are an amazing scientist.” Well, my program is smart, so it is can also ask what your name is. Let’s try this as a class.

Press the “AskName” Button, say: What is your name? (Raise your hands; can someone give me their name?)

Update board:

NAME = <class member response>

Did you see how when you told the computer (me) your name, it stored that information in the variable called “NAME”? Now let’s press the “Brag” button. (Press the button, say “so-and-so, you are an amazing scientist.”) Let’s try it again. I’ll call “AskName” again. (Do that, rewrite NAME). Now I’ll call “Brag” again. (ask students for what “Brag” will say) See how that works? We’ll do this one more time. (Repeat again for yet another student.) Does everyone see how my program uses the variable “NAME” to repeat your name? (wait for responses) Good, let’s talk about what it's doing.

When I wrote “Name = Albert Einstein” at the beginning, I set what is called the “initial value” of the variable NAME to “Albert Einstein”. An initial value is whatever value the variable has when the program starts. While the program was running, each call to the “AskName” method changed the value of NAME to the name you gave me. That set the current value of NAME. When we called the “Brag” method , it used the current value of NAME. If we started the program over, we’d start with Albert Einstein again. Now we’re going to use this in Alice.

Add Variables:

Okay, turn your monitors back on, and let’s go back to our previous lesson. What did we just add to our racecar world program? (turning the tires when the car turns) How far did the tires turn? (Did it look kind of silly? It went really far, right?) We’re going to tweak that, but we’re going to be efficient about it. We’re going to use a variable to control how the tires turn so we only have to make one change instead of a lot.

We will use a variable called “tireTurnAngle” to control how far the tires turn. We will set the initial value before we run the world. Unlike my example earlier we will NOT update this variable at runtime, because we’re going to test for a good value ahead of time.

Create a new variable: Select “convertibleCorvette” in the object tree and then click on the “Properties” tab. Click “Create new variable” and type “tireTurnAngle”. For the type click “number” and for the value type “0.25”. That series of actions created the variable and set its initial value. Now to use it we can drag its name to where we want it to be used. In the “tiresLeft” and “tiresRight” methods “0.25” revolutions appears twice in each method. Replace “0.25” by dragging the variable “tireTurnAngle” on top of it.

Run the world and make sure everything still works. The tires still turn too far. To try a smaller turn select “convertibleCorvette” in the object tree and then click on the “Properties” tab. Click on the value “0.25” next to “tireTurnAngle” and click on “other”. Try typing a smaller number. A good way to start is by picking a number half the size of the previous attempt “0.125” works. We talked about this last time. It's called “binary search”. You can try it for a minute, and then we'll do one more thing.

Create a new variable: Now we're going to do the same thing for how long it takes for the tires to turn. We're going to call this variable “tireTurnTime”. Since it's also used just for the car, we'll create it in the car, too.

Select “convertibleCorvette” in the object tree and click on the “Properties” tab again. Click “Create new variable” and type “tireTurnTime”. For the type click “number” and for the value type “0.5” for half a second. That series of actions created the variable and set its initial value. Now to use it we can drag its name to where we want it to be used. In the “tiresLeft” and “tiresRight” methods “0.5” seconds appears twice in each method, in the durations. Replace “0.5” by dragging the variable “tireTurnTime” on top of it.

Run the world to see if the tire movement is good enough. If it isn’t try picking new values for the angle and the time until you are satisfied.