Learning to Program for Kids of All Ages with REALbasicby Matt Neuburg
Computer literacy isn't just knowing how to copy a file, use a spreadsheet, or search the Internet; it's also knowing how to program. One needn't program at a professional level, any more than one needs to be a professional mechanic to drive a car; but never having programmed at all is like not even knowing why your car needs gas and oil. And no one needs computer literacy more than kids because without it, the computer is just a mysterious, smart-seeming television set. Learning to program, even just a little, shows kids what's really going on behind the screen. (Besides, it trains the mind, just as Latin was supposed to do when I was young.)
On the Macintosh, REALbasic is a terrific way for kids to learn to program, for these reasons:
You're making a real interactive application. You're not working on some dry, abstract problem; you're making windows with color graphics, text fields into which you type, and buttons to press.
It's easy. Drag a button into a window and presto, the window has a button. Now double-click the button and type in some code; when you run your application, that's the code that executes.
It's instantly rewarding. Kids like interactive feedback, and with REALbasic you get it immediately. Your application works even if it's just a window with a button and a text field, with no code. You can still press the button, type in the text field, move the window, and use the Copy, Paste, and Quit menu items. Your application keeps working as you give it code and make it do more. You always know you're accomplishing something, so it's always fun.
Basic is a good first language. Experienced programmers may scoff at the wordiness of If...Then...End, but it's a lot friendlier than C's curly braces or Logo's parentheses. Plus REALbasic code is automatically syntax-colored and pretty-printed, and there's lots of online help.
It's real programming. From basic branching and looping to high-level object orientation, REALbasic has it all. And you're always using these concepts, so you pick them up almost subliminally. This makes it easy to move to another language later on. I am much better than before at C++ and Java because REALbasic taught me all about classes, subclasses, virtual methods, polymorphism, constructors, destructors, and class interfaces, without my even realizing it.
The rest of this article outlines a strategy for introducing REALbasic to kids. I assume that you're the teacher and that you've already learned REALbasic, probably from my book, REALbasic: The Definitive Guide, 2nd Edition. On the other hand, we could also pretend that you're the "kid", no matter what age you are. In other words, if you've never tried programming, REALbasic could be a great way to give yourself a taste of it. You can probably tell that I've actually tested this little "curriculum" on some live kids, so I know it works. Remember to space things out; it might take a couple of weeks to get through everything described here.
REALbasic's version of the "Hello world" program is to put up a dialog box saying, "Hello, world!". It helps to add a little noise. So start with a program consisting of a button in a window, where the button has this code:
beep msgbox "Hello, world!"
It's exciting to run this and press the button a few times. Now immediately introduce the concept of a loop, and add repetition to the project: explain about variables and assignment; teach Dim and For; and turn the project into something that uses two For loops to beep five times and show the dialog three times. Kids will spontaneously extend this to something that behaves obnoxiously, such as beeping 100 times! You've got them hooked.
The most important string operations are the Mid and Len functions, so teach those, and work out how to use them in a For loop to cycle through the individual letters of a string one at a time. Make a program that sets a variable to a string, such as "Hello", and puts up a dialog for each letter of that string--a dialog that says "H", a dialog that says "e", and so on.
The limitation here is clearly that our word is hardcoded into the program. So instead, add an EditField control, explain how to get its contents with editfield1.text, and make a program where the user can enter any word in the field and the program will spell it out in dialogs one letter at a time.
Now reverse the loop so that the program spells out the word backwards. Next, instead of individual letters, have the program accumulate the letters in reverse order into a new string and put up just one dialog that spells backwards any word or expression the user enters into the field. Kids really enjoy making the computer spell their own and each other's names backwards, and so on.
Conditions and Functions
Next, let's teach the computer pig Latin. Start with a program that strips off the first letter of whatever the user enters in the text field, puts it on the end, adds "ay", and puts up a dialog showing the result.
That's not very sophisticated because we're not distinguishing vowels from consonants. So introduce the notion of a function, and explain about conditions with If. Now you can have the kids develop a function that says whether a letter is a vowel or a consonant. Modify the pig-Latin program so that it strips off all the initial consonants and puts them on the end before adding "ay". You can get even more sophisticated with uppercase, lowercase, and punctuation.
Time to drop word games and start playing with pictures. Demonstrate the Canvas control and show how easy it is to have it display simple geometric shapes such as squares, circles, and lines. Now send the kids off with graph paper and have them plan a little drawing, such as a stick figure of a person or animal, using the grid to figure out what values to feed to DrawOval, DrawLine, and so on. The kids usually find they've made some mistake the first time, so that the drawing comes out all wrong, usually with hilarious effect. Why shouldn't errors be fun too?
Next, let's animate the drawing just a little. Get the kids to think about the problem of making the stick figure "walk" by moving to the right a little bit every time a button in the window is pressed. Clearly the horizontal component of everything needs to be increased a little each time the button is pressed, so the code should be rewritten to add the value of a variable to the horizontal component.
But this can't really be a variable because we have to "maintain state"; something needs to remember the horizontal offset between presses of the button. So you'll add a property to the window. The button increases the value of this property, and refreshes the Canvas.
Next, make the animation a little more sophisticated by having the figure make some repetitive gesture while moving to the right. For example, with a stick figure of a person, the angle of the legs could shift a bit to make it look like the person is walking. This calls for more properties and more code and becomes quite an interesting project. Kids, of course, will surprise you with fun monstrosities such as people whose heads come off as they walk.
Now you might like to eliminate the repeated pushing of the button to generate the next "frame" of the animation. Instead, use a loop so that the animation will run itself. It will probably run too fast, so you'll have to add an inner loop that uses the Ticks function to pause and slow things down.
Another variation is to have the stick figure respond to the movement of the mouse. You can have the Canvas's MouseMove event watch the mouse's position and redraw the stick figure depending on where the mouse is, so it appears to follow the mouse.
Even though this isn't sophisticated, kids will spontaneously realize that this is really just what their favorite video game actually does. Having this kind of light bulb go on is what computer literacy is about.
It's good to end with a game. Kids always choose something way too ambitious; I like to use Tic Tac Toe. It's harder than you might expect. In the Appendix ("Growing an Application") of REALbasic: The Definitive Guide, 2nd Edition I provide a complete implementation, from start to finish, including several false starts and mistakes, so that you can see my actual thinking process when developing an application. That implementation is a little more sophisticated than I would use with children, but you can tone it down to make it suitable. The computer will be reacting to mouse clicks, and you'll probably need concepts like arrays, control arrays, and custom classes. Those are all educationally valuable.
On Beyond Zebra
This is a good place to end our introduction to programming because the concepts that have been explored--looping and branching, subroutines, messages to objects, string processing, graphics and animation, properties, arrays, and custom classes--are the important fundamentals. At this point, the student is either interested in pursuing programming further or not. If so, my book can be used to study REALbasic thoroughly. If not, at least we had a good time, and computers are no longer a mystery. No one who has spent time getting a taste of programming will see computers the same way again.