Lesson Plan VVBasic B - Lesson 6

Overview

Lesson 6 has two themes. One is program design: how computer scientists go about planning a program before they start coding. The second theme is built-in functions, specifically Random, ToUpper, and ToLower.

Introductory

The first part of the Introductory section uses the popular hangman game to give the students a sense of how a longer program is created. Here's how the session might go, with student responses in bold. Parts of the program design you write on the board are in this font. (Steps are preceded with numbers to show their order, but you shouldn't write the numbers on the board.)

Does everyone know how to play hangman?
Yes!
Let's pretend I'm the computer and you are the programmers, and you want to write a program to tell me how to play hangman. To start, we'll just do the part where the computer has a secret word or phrase and you the human have to guess it. OK, what's the first thing the computer has to do?
Choose a word.
Right.
    1. Choose a word.
Now what? (Students will probably be guessing letters.)
Write an underline for each letter.
Right.
    2. Write underlines.
Now what?
Guess a letter!
No, now the computer has to ask you to guess a letter.
(blank stares)
What command do we use to get the user to put in information to the computer?
INPUT!
Good, how about this?
    3. INPUT "What is your letter?" Letter
Now let's run the program so far. (Choose a word or phrase (maybe "central processing unit"), write the underlines on the board.) When we get to INPUT, I the computer will speak the prompt. What is your letter? (Call on one of the madly waving hands.)
E
Now what does the computer do with your letter?
See if it is in the word.
OK
    4. IF Letter is in secret word THEN

    10. ENDIF

What do we do if the letter is in the word?
Write it on the underlines.
OK
    5.   PRINT "Your letter is in the word."
    6.   Put letter on underlines

(Write the E on the underlines.) Now what?
Ask again!
How can we make the computer go back and ask again?
(nervous fidgiting)
Here's a hint: reeeee....
REPEAT!
Great, where should we put REPEAT?
Before the INPUT
Good.
    2.5 REPEAT
And if we have REPEAT, then we need . . .
ENDREPEAT!
And where does that go?
At the very end.
Good.
    11 ENDREPEAT
Terrific. Now we loop back up to the INPUT, and "What is your letter?"
A
The computer tests if "Letter is in secret word" true, and it is, so the computer prints "Your letter is in the word" and writes an A here.
(Continue until a student guesses a letter that's not in the secret word. If necessary, ask a student to choose an unlikely letter as a special favor.)
Z
Now the test "Letter is in secret word" is false, so we need to have what?
An ELSE.
Right.
    7. ELSE
    8.   PRINT Letter " is not in the word."
    9.   Draw the next body part.

Did we forget something?
The gallows.
Draw the gallows -- where should that go? After the REPEAT?
No, before the REPEAT
Like this?
    2.5 Draw gallows
Yes!
(Continue until students start guessing the entire word or phrase.) You're yelling at me, but I'm just a computer. Does a computer hear you when you yell at it?
No.
Given the program we have so far, there's no way to tell when the game is over and the player has won or lost. What would we have to add to this program?
If you guess 10 wrong letters, the hangman is complete and you lose.
Very good.
If the complete word is filled in, then you win.
Bingo!

As a wrap-up, you can point out what was written on the board is a sketch or a design for a program -- it's not ready to type into the keyboard yet. But this is often how computer scientists go about putting together a program. They run through it in their minds and identify all the necessary steps.

The second part of the introduction is about functions. Students have probably seen functions in the math class. In VVBasic (as in math) a function has a name, a left parenthesis, some parameters, and then a right parenthesis. The function does some operation based on the parameters. The Random number takes the value of a random whole number between its first and second parameters, inclusive. (Ask the students to define random.) ToUpper changes the letters in its parameter to be all upper case. ToLower is similar, but changes the letters to lower case. The examples will illustrate:

VVBasic commands Output
PRINT Random(1, 10) 3
PRINT Random(1, 10) 9
PRINT Random(1, 10) 4
TEXT Name = "McCarthy"
PRINT Name " " ToUpper(Name) " " ToLower(Name)
PRINT Name
McCarthy MCCARTHY mccarthy
McCarthy

The last example above shows that the ToUpper and ToLower functions don't change the value of their arguments.

Programming Lab

As always, point the browser at http://vvbasic.csed.org to begin.

  1. Enter this program, which uses the Random function:
    REPEAT
      PRINT Random(1, 100)
    ENDREPEAT
    
    Press the Run button. Do the numbers look pretty random? It may be hard to tell. After a while, click the Stop button.

  2. Perform an experiment to see if Random(1, 2) is equally likely to choose 1 or 2. Type in this program:
    NUMBER Trials = 0
    NUMBER OnesCount = 0
    NUMBER TwosCount = 0
    NUMBER R
    REPEAT
      LET R = Random(1, 2)
      IF R = 1 THEN
        LET OnesCount = OnesCount + 1
      ELSE
        LET TwosCount = TwosCount + 1
      ENDIF
      Let Trials = Trials + 1
      IF Trials = 100 THEN
         BREAK
      ENDIF
    ENDREPEAT
    PRINT OnesCount  " 1's and "  TwosCount  " 2's."
    
    Run the program. Did you get an equal number of 1s and 2s ? If not, is that a problem? Run the program several times. Change the program to have 1000 trials instead of 100.

  3. Type in and run this program:
    TEXT Name
    REPEAT
      INPUT "What is your name? " Name
      PRINT "Turn off the TV, " Name
      PRINT "I said NOW, " ToUpper(Name)
    ENDREPEAT
    
    Sound like anyone you know? What happens if you use ToLower instead of ToUpper ?

Concluding Questions and Answers

Show the class this short program:
PRINT Random(1, 6) + Random(1, 6)
Ask and discuss the following questions (answers in italics):
  1. What numbers might be PRINTed by this program?
    2 through 12.
  2. If the plus sign were changed to a minus, would the program always print 0?
    No.
  3. Does this program remind you of anything from Monopoly?
    Rolling the dice.
  4. Are all the numbers 2 through 12 equally likely to be printed?
    No. There's only one way of "rolling" 2 (1 + 1) and six ways of rolling 7 (1+6, 2+5, etc.), so 7 is six times as likely as 2.

Evaluation Activity

Ask the students to write a program that will print out the name of a playing card, selected at random. For instance, it might print 6 of clubs or king of diamonds. This is a challenging task, and students may need some or all of the following hints:
  1. Use two numeric variables, one of which is assigned a random number between 1 and 13 (for the rank). The other is assigned a random number between 1 and 4 (for the suit).
  2. Based on the value of the second variable, assign a value to a text variable, based on this scheme: 1 = diamonds, 2 = hearts, 3 = spades, 4 = clubs.
  3. The rank variable can be printed directly when its value is between 2 and 10. Otherwise, it has to be converted, so that 1 = ace, 11 = jack, 12 = queen, 13 = king.
Here's one of many possible solutions:
NUMBER Value = Random (1, 13)
NUMBER SuitNum = Random (1, 4)
TEXT Suit
IF SuitNum = 1 THEN LET Suit = "Hearts" ENDIF
IF SuitNum = 2 THEN LET Suit = "Diamonds" ENDIF
IF SuitNum = 3 THEN LET Suit = "Spades" ENDIF
IF SuitNum = 4 THEN LET Suit = "Clubs" ENDIF

IF Value = 1 THEN
  PRINT "Ace or " Suit
ENDIF
IF Value >= 2 AND Value <= 10 THEN
  PRINT Value " of " Suit
ENDIF
IF Value = 11 THEN
  PRINT "Jack of " Suit
ENDIF
IF Value = 12 THEN
  PRINT "Queen of " Suit
ENDIF
IF Value = 13 THEN
  PRINT "King of " Suit
ENDIF