Tuesday, November 15, 2011

Choen the counterBot

I'm pretty excited... both my kids seem interested in computers and what you can do with them. My daughter, especially, is very interested in learning bits and pieces about programming. My son is interested, but not so much in the nuts & bolts of programming. He is interested in numbers and performance, though. They're both young, though... he's in 1st grade, and she's in 2nd.

She's expressed some interest in programming before. Last time we talked about application design... she got extremely ambitious and drew about twenty pages of "designs" (hand-drawn mock-ups of screens) for games that she wanted to create. (Essentially knock-offs of games she's played on favorite websites like PBS Kids and Pixie Hollow.)

More recently, she was using my computer, and discovered that when she's logged into my computer on her account, she has access to Visual Studio Express. When she asked what it was, I told her it was tools to write programs, and she just lit up. Immediately, she wanted to start working on a simple UI that mimics a math program she uses at school.

I started to realize, however, that she needs to learn more of the basics before we dive into a full fledged user interface. I started to think about some of the earliest programs I learned to write.

Back when I was in 4th grade, I got my hands on an Apple II, and one of the first programs I learned to write on it was a program that accepted two numbers (start and finish) and simply scrolled out a count between them.

Nowadays, we have object oriented programming, and C#, but I wanted to try to keep it simple and understandable for her. I decided that we would create a "counter-bot" class, and we would create an instance of it named whatever she wanted it to be named (She decided on Choen (pronounced more like Cohen)).

Today, we got the basics of the counterBot class written, and wrote a short program to create an instance of it, initialize it, and start counting, sending the numbers out to the screen. A countBot has beginAt, finishAt, countBy properties, and Start, Count, GetCurrentNumber and isFinished commands.

Here's the code:
counterBot.cs:
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Choen_the_counterBot
{
    class counterBot
    {
        public int beginAt;
        public int finishAt;
        public int countBy = 1;
        private int currentNumber;
    
        public void Start()
        {
            currentNumber = beginAt;
        }

        public int GetCurrentNumber()
        {
            return currentNumber;
        }

        public void Count()
        {
            currentNumber = currentNumber + countBy;
        }

        public bool isFinished()
        {
            bool result=false;
            if (currentNumber > finishAt)
            {
                result = true;
            }
            return result;
        }

    }
}

--------------------------------------------------------


and here's the console Main method in program.cs:
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Choen_the_counterBot
{
    class Program
    {
        static void Main(string[] args)
        {
            counterBot Choen = new counterBot();
            Choen.beginAt = 0;
            Choen.finishAt = 2000000;

            Choen.Start();
            DateTime startTime = DateTime.Now;
            
            while (!Choen.isFinished())
            {
                Console.WriteLine(Choen.GetCurrentNumber());
                Choen.Count();
            }
            
            Console.WriteLine("Finished counting from " 
                + Choen.beginAt.ToString() 
                + " to " 
                + Choen.finishAt.ToString()
                + " by " 
                + Choen.countBy 
                + "'s in " 
                + (DateTime.Now - startTime)
                       .Seconds.ToString() 
                + " seconds!");
      
            Console.ReadKey();
        }
    }
}


--------------------------------------------------------


I got a bit fancy and added the timer code to see how long it took. Together, my son, daughter, and I watched Choen the counterBot count out various scenarios, and got as ambitious as watching it count from zero to two million. In order to get my son's interest, I added the timer to see how long it took. We discovered that my computer can count from zero to two million, "out loud" (showing the numbers on the screen) in 52 seconds. If we hid the console writes, it could do it in less than a second.

Next time we play with it, we'll write a graphic UI (a windows form) and hook Choen the counter bot up to it.