C# – A Class of Its Own

As many people know, after the hard push to make Red Jade available to everyone, I’m on a bit of a writing hiatus. My current endeavor has been coding. Yep, I’m learning once again how to be a computer programmer.

Commodore 64When I was a little tyke, my brother was in a computer class in school learning concepts in programming on a Commodore 64. The programs used BASIC language and on a car trip up to Massachusetts to visit our grandparents, my brother pulled out a notebook in the back of the station wagon and he showed me what he had been learning.

It didn’t take me long before I was enamored with IF/THEN statements, FOR/NEXT loops, and PRINT and INPUT commands to name just a few. I wanted to learn more so I practiced. And we got books and magazines and I learned more. In third grade I was troubleshooting computers in school (in particular this one printer, but it sounds so much more exciting saying it the other way). In fourth grade I was pretty comfortable making sprites and in fifth grade I showed the life cycle of a caterpillar using those sprites and moving them onscreen.

For home I made programs to celebrate holidays and one to keep track of bowling scores for the family, which was really hard on the C64 because file reading and writing was very particular. In seventh grade my science teacher hired me to write a program for her to keep a record of the track team’s times. And in high school, my computer teacher watched what I did with Mad Libs to ensure no words broke off a screen line and he started giving me other things to work on, like making tessellations and a banking app so he could easily calculate compound interest.

You might have thought I would have become a computer programmer. But even though I loved it, it had never occurred to me to program much in college. Well, there was that one class I took on fractals and I dove back into my roots in QBasic which was similar enough to my skill set for me to pick up. Other languages were way too foreign for me and the burgeoning chaos of life at the time kept me from learning more.

Scroll ahead a few (ahem) years and I’m all interested again. Kevin is a software engineer with two impressive programs out in Apple’s App Store. He has picked up a few new languages in the time I’ve known him from Xamarin to Swift to F#. It was motivation for me to try my hand at learning C#.

I’ve tried it before, actually. It hasn’t worked. I just couldn’t understand the different syntax, which is the least of all the things that are different. But I wanted to give it another try. Kevin got me a book, Head First C#. It’s a different kind of book than I’m used to but it helped me get through some of the stumbling blocks. I am almost halfway through the book with a lot more to do but I decided it was time to take what I knew and work on a project of my own. (Or two.)

Inspired by Disney’s Wii game Guilty Party (2010), I wanted to create a similar game that had you find clues that let you narrow down and eliminate a list of suspects until you find the guilty party. It’s a crazy ambitious project, I know. But at first I just wanted to work out the logic of how to deal with checking a clue (“the culprit is tall”) against a set of suspects and remove people who were short.

Catch the CulpritWell it grew from there because I can’t ever be simple. Soon I created a map in a Windows Forms Application, numerous clues, a whole user interface and so on. It’s a big undertaking that deserves its own post with more detail. On September 15, I made a note to myself with the idea of a suspect class but I had no idea how to work with it. Well now I have a fully functioning program.

I did have Kevin do a code review with me. We spent a whole Saturday afternoon on it and it was great. Here’s this master programmer looking over my code and then looking at me, not quite believing I had actually created it. And overall the code was pretty clean! He tidied a bunch of things so I could see other ways of doing things and he helped me with a few roadblocks I had (and others I’ve gotten into since).

So now I’m turning it over to you. This link will give you access to the game. Be aware though that it is an .exe file and your firewall may not let you download it. Also it will not run on iOS so save yourself the aggravation. You need a PC to try it and I guess it may work on an Android. I haven’t quite worked out every bug so if you try it and find any please tell me what you were doing and what happened. 🙂 If you have other ideas, tell me those too! https://www.dropbox.com/s/r2j4o6u7h45tfg0/CatchTheCulprit.exe?dl=0

Heat Equation CalculatorA bit simpler, today I made an applet to calculate heat equations. I teach them to my honors classes and I thought an applet like this would help them to practice. They can make up their own numbers and the check their work. It still needs bug testing too but I think I ironed out most of the issues. It also must be run on a PC. https://www.dropbox.com/s/v0ngx5gdh1ajfce/Heat%20Equation%20Calculator.exe?dl=0

So that’s what I’ve been up to. I do miss my characters and the worlds I love so I’ll get back there!

Coding for Kids

Photo Courtesy of Unsplash.comMy new hobby is still going strong but I do miss writing. I’m still a beginner at writing code in C#, technically, but I think I’d be more accurately tagged as an intermediate programmer. I’ve said it before: Kids need to learn coding skills. I thought it would be a good idea to try to reach the younger population but in a different way than I’ve seen. Kids don’t want to read a textbook. Well, what if there was a coding manual that taught the basic concepts through a story?

I don’t know if this plan is going to work but it’s worth an effort. It’s right up my alley, too. I’m taking a tricky piece of science and explaining it through analogy. 

Last night I wrote the first three chapters. After each chapter, I plan to create a worksheet of exercises related to the information in the chapter. At the end of a unit, I’ll put more complex worksheets that tie together the concepts from the unit. 

Coding is best learned by coding. Sure. But I’m starting here at the base level to reach kids who’ve never programmed before. I also want this to be accessible to colleagues who’ve never programmed either. Let’s be honest, it’s easy to get in deep to coding jargon but overlook the explanations. I want to fill in that gap that I’ve seen. As I’m learning to do more complex tasks, I need an explanation for what and why and how. Following the code is fine for me in most cases, but Kevin showed me something the other day that still has me confused. It’s actually what prompted this writing venture. 

So I don’t know if this is a good path to take or not, but it’s getting my typing hands back at the keyboard for storytelling. And later this year when my research class is finished with their assignments and we have some time for another challenge, maybe I’ll give this a try with them. 

But in the meantime, what do you think?
 

 

Day 1: Binary

“Moooooooom! Cody’s doing it again!” 

Mom’s voice echoed from the kitchen. “Cody, stop counting in binary numbers!”

Cody grinned and he made his way to the kitchen see his mom, with his sister, Jade, following him. “I don’t see what’s wrong with it,” he said. “We have light switches that either go on or off. That’s all I’m really doing.”

Jade shook her head. “I don’t get it, you know. We’re playing a game of Go Fish and when he asks for a seven—”

Cody interrupted, “Zero-one-one-one.”

His sister slapped his shoulder in annoyance. “There he goes again!”

“Mom,” Cody explained, “it’s how computers use numbers. They flip switches on and off—one or zero—and when they put it all together they understand everything! I think we all need to learn a bit more about it.”

Mom grabbed a pad from the counter and put it in front of Cody. Jade groaned because she knew what coming next… a lesson.

On the right side of the page, Cody wrote the number 1. To its left he wrote 2. To its left he wrote 4, then 8. “These are all powers of 2,” he said sagely. “I’m listing them from right to left because it’s just like how we read numbers. Like the number 2017… the smallest part of it, the ones digit, is on the right.”

“Yawn!” Jade said aloud.

Cody ignored the comment and pointed back to the list he had made: 8 4 2 1. “Each one of these is like a switch. If it’s on then we put a 1 under it and if it’s off then we put a zero. Using only these numbers, how could you make them add up to seven?”

Mom figured it out quickly but she waited for Jade to give it a try. Even though Cody’s sister was giving him a hard time, she knew Cody had some interesting things to say. Not that she’d ever admit that to him directly! Jade looked at the numbers that were on the page and she figured it out. “If you add 4 and 2 and 1 then you get seven.”

Cody smiled. “Right! And in this case, we don’t need the 8.” He put a zero under the 8. “We do need the 4, 2, and 1, so…” He drew a 1 under each of those numbers. “Because those numbers are switched on then the computer would add them all together and make seven. But in binary it’s written as 0111.”

“Why bother with the zero in front?” Jade asked. “We don’t say zero-eleven dollars.”

“That’s true. But we’re playing Go Fish and the cards go up to ten. So we would need to use that 8 sometimes. I’m keeping four places to be consistent.”

Jade looked at the list on the notepad: 8 4 2 1. “One-zero-one-zero. That would make it 8 plus 2 to give us ten, wouldn’t it? We wouldn’t need the 4 or 1.”

Cody beamed. “You got it! That’s binary!”

Mom was relieved that the argument was already over but she decided to push the conversation a little further. “I can see that 8 plus 4 plus 2 plus 1 equals fifteen. What if you have a larger number than that?”

“It’s base 2. That means each value here is 2 to an exponent. 20 = 1. 21 = 2. 22 = 4. 23 = 8. Then, 24 would be 16.”

Jade looked at it carefully. “That’s silly. You’re just doubling each number as you go.”

Cody agreed. “It’s kind of the same thing, yes. That’s math for you. That exponent—that number that’s up on top—tells you how many times to multiply the 2 together. So 24 is really saying 2 times 2 times 2 times 2.”

Jade frowned. “But why 2? Why not something more useful?”

“It goes back to that on/off thing I was saying before. Like a light switch, each bit can only be on or off. It’s only two possible choices. You can’t have the switch on halfway.”

“Well, you kind of can,” Jade argued.

“Not really. Sure, we can try to slide the switch up slowly, but at some point it clicks and the light goes on. There isn’t a half-on option. Either it’s on or it isn’t.”

Mom smiled, “Unless you have a dimmer switch.”

Cody nodded his head. “Yep, but that’s not the kind of switch I’m talking about here. That would make the computer process a lot more complicated!”

“It’s bad enough as it is!” Jade complained with a wink. “But really, Cody, I get it now. I guess binary really isn’t so bad after all.”

“Cool. Want to get back to our game of Go Fish?”

“Sure. But let’s play for best 011 out of 101.”

“Best 3 out of 5 it is!”

 

Day 2: For Loops

“Yo, Cody!” called Martin. “Let’s play a game of hide-and-seek!”

Cody and his friends gathered in the playground where many hiding places existed. Martin offered to be the first hunter, so he counted down from ten to one while everyone else scampered away, diving behind slides and sneaking behind trees. When Martin finished the count, he ran around the playground, seeking out his friends and tagging one. They played a few rounds until at last Cody was tagged.

“Ok,” Cody said. “My turn to count.” He turned around and shouted out loudly, “Nine!”

“Wait!” Lonna interrupted. “You have to count down from ten!”

“I am!”

“But you started at nine! That’s one tick less than anyone else is counting!”

“It’ll be ten ticks, don’t worry. I’m not trying to pull a fast one.”

Skeptical, Lonna shook her head and scanned the area for a hiding place that was closer than her originally planned spot. “We’ll see…”

Off Cody counted, “Nine! Eight! Seven!” He heard his friends sprinting away, grunting and hurling themselves to the grass. Some even tossed rocks here and there to distract Cody’s attention so he’d think someone went off to the other side.

“Six! Five! Four!” More hurried steps thundered on the ground behind him. Everyone seemed worried that he wasn’t going to count ten ticks.

“Three! Two!” He heard someone moan in frustration. It sounded like Lonna, as if she hadn’t found a spot yet. Cody laughed to himself and then finished his count.

“One! Zero! Ready or not, here I come!” Off he went, seeking out his friends, peering into the wooden pirate ship, glancing over at the monkey bars, and jogging toward the swings. He spotted Alex in a large plastic climbing tube and he dove inside and nabbed him.

Caught, Alex freed himself from the tube and called out, “I’ve been tagged! Come out, come out, let’s start again!”

As they all gathered together, Lonna turned to Cody. “I guess you really did count ten things, but why did you start at nine?”

Cody held up his hands and counted off his fingers, wiggling each one as he did so. “Zero, one, two, three, four,” on his left hand. “Five, six, seven, eight, nine,” on his right hand.

“Nobody starts at zero,” Martin said.

“Lots of people start at zero!” Cody said cheerfully. “Do you have a bank account? It started at zero didn’t it? How much food was in your belly before breakfast? None. We start at zero all the time. A car? You don’t usually climb in when it’s moving.”

“Well, I do!” Alex laughed.

“It’s an interesting point,” Lonna noted. “But your finger starts at one, not zero.”

Cody shrugged. “It depends on how you look at it. The thing is, computers default to starting at zero. It’s their beginning spot. Like the bank account thing. Start at zero and it’s all up from there.”

“You and your computer speak,” Martin smirked.

“Hey, they’re all around us. It’s a good idea for us all to learn a bit about them, I think.”

Lonna nodded. “I agree. But are you saying computers can’t count from 1 to 10?”

“Of course they can! You’d have to tell it to. But in many programming languages, they’d start counting from zero. Then they’d do something and then count up by one for as long as they’re less than ten, for instance. It gives the same result, but it’s thinking about it differently.”

Alex looked down at Cody’s hands. “So it’s kind of like you were counting your fingers from zero, then after you noted how many were up, you raised one. Then your number of visible fingers went up by one. And then you raised another until you ran out of fingers to raise up.”

“Precisely. And in computing language, that would be called a ‘for’ loop.”

Peter piped up then, “What’s a ‘for’ loop?”

Cody dropped down to his knees and grabbed a twig. He drew a set of symbols and words in the dirt:

for (int i = 0; i < 10; i++;)

{

    RaiseAnotherFinger( );

    Smile( );

}

His friends gathered closer, wondering what on Earth he had drawn and whether maybe he had hit his head or something. “Let me break it down,” Cody offered. “That word ‘for’ is a keyword. That tells the computer it’s about to go into a loop. Now, it needs a set of rules to follow and those rules are in the parentheses after ‘for’.”

“I’m counting my fingers.” He holds up his hand again with no fingers raised. “I’m starting from zero and I’m going to count in whole numbers. Right?”

“Yeah, sure,” Alex said.

“Ok, this part here, the ‘int’ says I’m using whole numbers. I’m using the letter ‘i’ because it’s sort of a default that means ‘iterator’ or counter. And I’m starting from the computer’s default of counting from zero.”

Lonna made an “ah” sound. “So then if you wanted the computer to start counting from the number 1, you could have put i = 1 instead.”

“That’s right!” Cody agreed. “But for other things, the computer is still going to start from zero, so it’s better to just get used to using zero also. Anyway, the i = 0 means I’m starting with zero fingers up. The semi-colon in C# is like a period in an English sentence. It completes a thought.”

Peter pointed to the next part: i < 10. “That looks like math. ‘i’ is less than ten.”

“Yep, you’ve got it. That part right there is the condition of the loop. As long as that statement is true, then the loop keeps going.”

“Well this part looks weird,” Martin said, pointing to the i++. “Who puts two plus signs together?”

Cody smiled. “It’s a shorthand for C#. It means to increase the counter by one. Now, the funny thing is, it doesn’t actually do this part of the line until after it finishes what’s in the loop itself.”

“Wait,” Lonna interrupted. “What if you wanted to count by 2? Would it be i+++ or i++++?”

Cody shook his head. “The syntax for it looks very different than what you’d see in math class. It’s: i = i + 2.”

“That’s impossible!” Peter argued. “No number is equal to itself plus two!”

“True and that’s the part that’s different in computers. The computer is solving the part on the right first. So it takes ‘i’ and it adds 2 to it. Then it changes the value of i to take over that answer. It’s like if you make a mistake and you erase it and write a new answer. The old one is gone and you have a new one. But in this case, it’s not a mistake. It’s how number variables work.”

Tricia, who had been watching quietly this whole time pointed to the drawing Cody had made in the ground. “What’s with these squiggly lines?”

“Those things { } are called braces, but a lot of people call them squiggly brackets. Here, they define a code block for the ‘for’ loop. The loop does everything inside of those braces and when it’s done, it goes back to the ‘for’ line and it increases the counter.”

Lonna bit her lip then said, “Ok, you start with no fingers up. You go into this loop. These instructions say to raise another finger, then smile. Then it goes up here to the i++ and it adds one. You’re in the next round of your loop and you already have one finger up. So it loops again. You raise another finger. You smile again. Then the counter goes up to two.”

“Yes,” Cody said. “And when it hits 10, that’s not less than 9 any more so the loop is done and it jumps out to go after the ending brace }. If there was more code there, it would do more stuff.”

“So the counter is counting the fingers?” Peter asked.

“Well, no. I’m just using that as an example you can follow. The counter, i, is actually counting the number of times it goes through the loop. It doesn’t actually care about fingers or smiles or anything that’s in the braces. It just wants to loop until it’s done.”

“What if you want to count down, like we’re doing before we hunt?”

Cody used the twig to draw a new loop in the dirt:

for (int count = 10; count > 0; count–;)

{

    ShoutNumber(count);

    WaitAFewSeconds(5);

}

Seek(friends);

The group looked at the new code and followed it through with Cody’s help. Alex started by saying, “Oh here it starts at ten!”

Tricia spoke next. “And I guess the count– is like the i++ but it’s counting down by one instead of counting up.”

Lonna pointed at the ShoutNumber line. “Here, you put something in the parentheses.”

“It’s called a parameter,” Cody explained. “Don’t worry about that for now, but you can probably guess what it would do.”

“I’d say that it tells you to shout out whatever number the count is at that point.”

“Correct!”

Alex focused on the command after the final brace. “Oh I see, then here you seek out the friends once the loop is done!”

Martin looked up at the sky and commented, “Speaking of done, it’s getting late. We should all get home before it’s dark.”

“Hey wait!” Lonna said excitedly, grabbing the stick from Cody and drawing something new on the ground. “Does this make sense?”

for (int day = 0; day < 7; day++;)

{

    WakeUp( );

    HaveBreakfast( );

    GoToSchool( );

    SeeFriends( );

    HaveDinner( );

    GoToSleep( );

}

“That’s great!” Cody said excitedly.

“Except for one thing,” Tricia noted, pointing at the third line under the open curly brace. “You have us coming to school seven days a week!”

 

Day 3: If Else

Dinner time with Cody was always interesting. He spent a lot of time turning basic actions into computer language, trying to explain things to his family as he went. He did this at lunchtime too with his friends. They didn’t mind too much. It was eccentric, but everyone felt like they understood Cody more and more.

Dad called Cody and Jade to the table. “It’s dinner time!” The two kids gathered around and sat down, taking in the sights. Mom and Dad had worked together to make a chicken cutlet feast with rice, corn, and salad too.

“What would you like first?” Dad asked.

Cody grinned. “If there’s butter and salt, then I’ll have corn. If there’s only butter, then I’ll have rice. If we don’t have either, then I’ll have salad.”

“Here we go,” Jade muttered.

“It’s basic decision-making!” Cody said.

Mom didn’t waste any time pulling out a notepad for their excited child. “Do tell, son.”

“Well, it’s really simple, actually. Computers can use an ‘if’ statement to test to see if something is true, like ‘is there any butter?’ If it’s true, then do something, like ‘take the rice’. And if the condition is not true—so if there isn’t any butter—then you can have the computer stop checking for things or give it an alternate task to do.”

“So if there was no butter or salt, you don’t have to have salad?” Jade asked.

“Right. Let’s start just looking at butter for rice or having salad.”

“Aw, no corn?” Dad asked with a smirk.

“I love corn! But I’ll come back to it,” Cody promised. “Here, look:”

if (butter == true)

{

    TakeRice( );

}

“This one’s simple. If there is butter, then I’m taking rice. It doesn’t matter if there’s salt, corn, or salad, or anything else. Butter? Yes? Then rice, definitely.”

Jade saw that there was indeed some butter on the table, so she grabbed the bowl of rice and served herself some. “What about your salad thing?”

“Sure, let me add that. If there isn’t any butter because Jade ate it all,” he added with a smile, “then I’ll have salad instead.”

if (butter)

{

    TakeRice( );

}

else

{

    TakeSalad( );

}

“There!”

“Wait a second!” Mom said. “You just say ‘if (butter)’. That makes no sense!”

“It’s shorthand. If the butter condition is true, then it means the ‘if’ statement is true so you don’t have to check if ‘butter’ is equal to ‘true’. You can’t always shorten things like that, though.”

Dad chuckled. “You’re shortening the shortening.”

Jade rolled her eyes. “Dad, you’re making butter jokes now?”

He winked. “You butter believe it!”

Before the silliness escalated further, Mom returned to Cody’s original decision-making example. “What about the corn?”

“Well, there are two ways to go about it. Let me show it to you the way I said it before. If there’s butter and salt then I’ll have corn. If there’s only butter, then I’ll have rice instead. Otherwise, it’s salad for me. The key here is that I’m only taking one side dish in this example.”

Dad chimed in, “So it’s going to be an unrealistic example then, because I know you’re going to end up with all three.”

Cody laughed. “Probably. But for now:”

if (butter)

{

    if (salt)

    {

        TakeCorn( );

    }

    else

    {

        TakeRice( );

    {

}

else

{

TakeSalad( );

}

Cody’s family looked over what he wrote and they talked about it as they tried to reason it out. Dad started it off. “If there is butter, then I go into this first set of braces and then we come up to a second ‘if’ statement.”

Mom added, “Then we check to see if there is any salt, and if so, then you have corn. But if there is no salt, then you go to the ‘else’ statement and take rice instead.”

Jade pondered the code next. “But I’m confused here. How does it know to go to the else with rice and not the else with salad?”

Cody pointed to the code. “See how these are indented? Those braces section off blocks of code. All the stuff that relates to having salt or not having salt is lined up under the ‘if’ statement for salt.” He took his napkin and placed it on his notes, covering up the right side. “Here I can hide all the code that relates to salt. That part is all for that ‘if’ block.”

“Oh, I see,” Jade said. “And the other ‘else’ statement happens if there was no butter.”

“Yeah. If butter is false, then it skips over the check for salt completely. It goes to the next block of code. It looks like this:”

if (Is this condition true?)

{

    Yes, it’s true, so do this;

}

else

{

    No, it wasn’t true, so do this instead;

}

Mom looked at the sets of code written so far. “Son, you said there were two ways to check for the salt and butter? What’s the other way?”

Cody turned to a new sheet and wrote quickly:

if (butter && salt)

{

    TakeCorn( );

}

else if (butter)

{

    TakeRice( );

}

else

{

    TakeSalad( );

}

“That double-ampersand && means that both butter and salt have to be true in order to take the corn. Otherwise it tests for the butter only. And if that’s not true either, then it goes to salad.”

Dad scratched his chin. “Then if there was salt but no butter, then the first condition would fail anyway because there would have to be both of them for it to be true?”

“Yep!”

Jade pointed to the command after TakeCorn. “You put the ‘if’ and the ‘else’ together here.”

“A slight shorthand, but yes. It says what it looks like… If the first thing wasn’t true, then see if this second thing is true. If it isn’t then keep going and do that other thing instead.”

“What if you leave off the last ‘else’?” Mom asked. She crossed it out and the code became:

if (butter && salt)

{

    TakeCorn( );

}

else if (butter)

{

    TakeRice( );

}

TakeSalad( );

Jade chimed in. “Then no matter what, he gets salad. But he might also get either corn or rice, but not both of those.”

“You got it!”

Dad took the notepad. “This is more accurate though:”

if (hunger > 1)

{

    TakeCorn( );

    TakeRice( );

    TakeSalad( );

}

else

{

    JustKiddingTakeAllTheFoodAnyway( );

}

Everyone had a good laugh at that, then they all started eating.