Review: Variables in Algebra

In algebra we use variables to represent values that can change. We give these variables names like x, y, and z.

Examples:

x = 3

y = mx + b

Variables in Scheme Programs

Variables in Scheme programs also represent values that can change. In programs:

Defining variables

To define a variable, use the define function. This function binds the name of the variable to its value.

Examples:

(define x 3)

(define speed 30)

(define nerd "Cool dude")

Program Exercise 1

The total cost of items can be found by multiplying the price of each item times the quantity:
total_cost = price × quantity

Objective: Find the total cost of 4 items if the price of each item is $10.25.

Use the Definitions Window to define variables in Racket. Follow these steps.

  1. Type (define price 10.25) in the Definitions window.
  2. Click Run
  3. In the Interactions window type price
  4. In the Definitions window type (define quantity 4) in the Definitions window.
  5. Click Run
  6. In the Interactions window type quantity
  7. In the Definitions window type (define total (* price quantity)) in the Definitions window.
  8. Click Run
  9. In the Interactions window type total
  10. Answer questions 1 - 3 in Exercise 3.1

Program Exercise 2

The make-color function will create any 24-bit RGB color. The arguments are the red, green and blue components of the color you want to make. For example, (make-color #xff #xff #x90) creates light yellow.

Objective: Define a variable for a new color. Then use that color to create a square.

  1. In the Definitions window define a new color:
    (define my-color (make-color #xff #xff #x90))
  2. Click Run
  3. In the Interactions window, create a square with your new color: (square 50 "solid" my-color)
  4. Change the color by changing the line you typed for step 1. Try three different values for R, G, and B.
  5. Click Run
  6. Create a new square in the Interactions window.
  7. Answer questions 4 and 5 in Exercise 3.1

Program Exercise 3

The overlay function will create a complex image by placing an image on top of another image.

Objective: Create a complex image by combining two figures.

  1. (define smallSquare (square 30 "solid" "red"))
  2. Click Run. Then, in the Interactions window, type smallSquare to verify the definition.
  3. (define mediumCircle (circle 50 "solid" "green"))
  4. Click Run. Then, in the Interactions window, type mediumCircle to verify the definition.
  5. (define myLogo (overlay smallSquare mediumCircle))
  6. Click Run. Then, in the Interactions window, type myLogo to verify the definition.
  7. Answer question 6 and 7 in Exercise 3.1.

Extended Program Exercise

Objective: Use variables to break complex tasks into smaller pieces. Create several pieces, and assemble them.

Type each of the following variable definitions in the Definitions window. Verify that is correct using the Interactions window. Remember to Click Run after each definition.

The following steps create a zombie eye.

  1. (define tinyBlackCircle (circle 5 "solid" "black"))
  2. (define smallRedCircle (circle 7 "solid" "red"))
  3. (define biggerBlackCircle (circle 9 "solid" "black"))
  4. (define biggerRedCircle (circle 11 "solid" "red"))
  5. (define muchBiggerBlackCircle (circle 15 "solid" "black"))
  6. (define zombieEye (overlay tinyBlackCircle smallRedCircle biggerBlackCircle biggerRedCircle muchBiggerBlackCircle))

Warning! The following steps create a zombie!

  1. (define twoZombieEyes (overlay/offset zombieEye 40 0 zombieEye ))
  2. (define zombieColor (make-color #x50 #x50 #x50))
  3. (define zombieHead (star-polygon 30 15 4 "solid" zombieColor))
  4. (define zombie (overlay/offset twoZombieEyes 0 20 zombieHead))
  5. Answer the remaining questions in Exercise 3.1.

Bonus. After completing the exercise, you may create a zombie attack.

  1. (define twoZombies (overlay/offset zombie 140 0 zombie))
  2. (define fourZombies (overlay/offset twoZombies 30 140 twoZombies))
  3. (define eightZombies (overlay/offset fourZombies 290 0 fourZombies))
  4. (define 16Zombies (overlay/offset eightZombies 600 0 eightZombies))
  5. (define zombieAttack (overlay/offset 16Zombies 0 300 16Zombies))
Computer Programming Unit 3.1 - Variables and Names