Friday, November 01, 2019

Magic 8 Ball and Projects From Real Life

There has been a 12 inch tall Pez dispenser on my desk for a couple of years now. It’s a great visual aid for talking about stacks. Today I got a Magic 8 Ball as well. It turns out that a lot of my students were unfamiliar with this staple toy of my youth. Since I really like using the Magic 8 ball program as a demonstration I decided I really had to get a physical ball.

At this point in the semester we have covered loops and arrays and it is time to make programs more interesting by adding more data. I want to use something that makes sense and is not just a demo without meaning or purpose. Having code you can have some fun with is always a plus as well.

I went to Wikipedia and found the classic 8 ball answers and placed them in a text file. My demo reads the file into an array when the program loads. Once the array is built clicking a button displays a random answer. It’s pretty simple to code and we code it together as a class. Students love asking it questions when we’re done.

Usually I have students add their own answers to the text file and then modify the program to handle the new set of answers. We also talk about different ways this could be coded. What if we didn’t know how many answers/lines were in the file? Would be use a different loop and/or a different array type like an arrrylist? What options (methods or functions) should we use to provide the most flexibility?

This also provides students with some sample code to look at when I assign larger projects and expect them to do much more on their own. Is this a good project? Magic 8 ball says “Yes definitely.”

No comments: