« September 2004 | Main | November 2004 »

October 27, 2004

Learning OOP in Flash: trouble with recursive method

Looking for a debugging puzzle? I'm stumped. When I try to use this method (a Class' method) to recursively call itself, I get a NAN returned instead of a number. Not only does it return NAN, but it keeps running down the code instead of exiting.

code and sample trace output in extended entry

Much of this should be looped, but I wrote it out to help me pinpoint the problem.

I want the numbers 1 through 4 to be distributed uniquely among 5 "rows". One row has it's own value, and we don't have to worry about that here.

First the method generates a random number between 1 and 4.

Then, it tests if this number is already in an array of numbers called _multiples. This information is saved as a local variable with true-false value for each "row".

Then, I loop through the numbers in _multiples, and if one matches my new number I'm supposed to start over again and call the method recursively. If my new number isn't in the _multiples array, then I add it and move on to the next row, returning the new number and exiting the method.

function getMultiple(i):Number{

trace("get multiple");

/*
Note to self: October 21, 2004
I'm getting an error with this when I test it:
When this function is recursively called, I end up with an NAN
for the ratio this is a multiple for.

It appears that the return statement is not causing an exit from
the function, as I would expect it to. Ugh!
*/

// get random number between 1 and 4, to use as multiple

var n = (Math.floor(Math.random()*4)) + 1;

// test if new number is already used as a multiple of base ratio,
// and get new nummber if needed

var tf0 = (n == _multiples[0]);
var tf1 = (n == _multiples[1]);
var tf2 = (n == _multiples[2]);
var tf3 = (n == _multiples[3]);
var tf4 = (n == _multiples[4]);

trace("row:" + i + " n:" + n + " m[0]:" + _multiples[0] + " n=m[0]? " + tf0);
trace("row:" + i + " n:" + n + " m[1]:" + _multiples[1] + " n=m[1]? " + tf1);
trace("row:" + i + " n:" + n + " m[2]:" + _multiples[2] + " n=m[2]? " + tf2);
trace("row:" + i + " n:" + n + " m[3]:" + _multiples[3] + " n=m[3]? " + tf3);
trace("row:" + i + " n:" + n + " m[4]:" + _multiples[4] + " n=m[4]? " + tf4);



if (tf0 == true){
trace("try again");
this.getMultiple(i);
} else if (tf1 == true){
trace("try again");
this.getMultiple(i);
} else if (tf2 == true){
trace("try again");
this.getMultiple(i);
} else if (tf3 == true){
trace("try again");
this.getMultiple(i);
} else if (tf4 == true){
trace("try again");
this.getMultiple(i);
} else {
// save number to array that holds all multiples

_multiples.push(Number(n));
return n;
}

}

Sample Trace Output:

case unique
get multiple
row:1 n:2 m[0]:undefined n=m[0]? false
row:1 n:2 m[1]:undefined n=m[1]? false
row:1 n:2 m[2]:undefined n=m[2]? false
row:1 n:2 m[3]:undefined n=m[3]? false
row:1 n:2 m[4]:undefined n=m[4]? false
get multiple
row:2 n:3 m[0]:2 n=m[0]? false
row:2 n:3 m[1]:undefined n=m[1]? false
row:2 n:3 m[2]:undefined n=m[2]? false
row:2 n:3 m[3]:undefined n=m[3]? false
row:2 n:3 m[4]:undefined n=m[4]? false
get multiple
row:4 n:1 m[0]:2 n=m[0]? false
row:4 n:1 m[1]:3 n=m[1]? false
row:4 n:1 m[2]:undefined n=m[2]? false
row:4 n:1 m[3]:undefined n=m[3]? false
row:4 n:1 m[4]:undefined n=m[4]? false
get multiple
row:5 n:3 m[0]:2 n=m[0]? false
row:5 n:3 m[1]:3 n=m[1]? true
row:5 n:3 m[2]:1 n=m[2]? false
row:5 n:3 m[3]:undefined n=m[3]? false
row:5 n:3 m[4]:undefined n=m[4]? false
try again
get multiple
row:5 n:4 m[0]:2 n=m[0]? false
row:5 n:4 m[1]:3 n=m[1]? false
row:5 n:4 m[2]:1 n=m[2]? false
row:5 n:4 m[3]:undefined n=m[3]? false
row:5 n:4 m[4]:undefined n=m[4]? false
correct row: 3
ratio1 : 2,2
ratio2 : 3,3
ratio3 : 4,5
ratio4 : 1,1
ratio5 : NaN,NaN

The Candidates on Science Issues

Not to be left behind by the AAAS, Nature has also published the results of questions they sent to the candidates for the upcoming election: news@nature.com - specials - us election

Scroll down the page for the nifty flash app they built to display the questions and answers. Well done. It's nice to be able to toggle between each candidate's answers.

Species of humans newly discovered

Anyone intereseted in Archeology will find this interesting. A new species of humans has been discovered

... on the remote Indonesian island of Flores just 18,000 years ago.

...one metre tall, with grapefruit-sized skulls. These astonishing little people, nicknamed 'hobbits', made tools, hunted tiny elephants and lived at the same time as modern humans who were colonizing the area.

Read more on Nature's website.

Genetic Algorithms using Perl

Teodor Zlatanov has written a whole bunch of articles for IBM developerWorks 'Cultured Perl' series. He just wrote the 3rd installment of a series on writing Genetic Algorithms using Perl (the first of which was written in 2001 and the second in 2002!).

The articles are:
Genetic algorithms applied with Perl, aug 2001: "Create your own Darwinian breeding grounds"
Genetic algorithms, the next generation, oct 2002: "Advanced examples of genetic algorithms with Perl"
Genetic algorithms simulate a multi-celled organism, oct 2004: "Create life, or something like it"

These articles might be of interest to anybody who is into Perl and biology.

October 26, 2004

Searching for Bob Crimi

For the past few days, I've been trying to track down the contact information for a science illustrator named Bob Crimi. Google turned up many examples of his fine work, but no contact information. There's a webpage for a painter named Bob Crimi, but without any mention of science illustration. I also contacted the editorial offices of the Scientist and of Nature, in which his illustrations are published, but they didn't have contact information for him either. So, now I'm trying a little experiment. Are blogs powerful enough to get his attention and will we then contact me?

The illustration, by Bob Crimi, that caught my attention is about halfway down the page of this article on olfactory sensory neurons. His use of color is fantastic!

Bob, if you're out there, please contact me.

October 22, 2004

VRML...not quite dead yet.

When SGI dropped VRML, years back, it looked like the end of VRML.

Well, somebody's got to be using VRML because ParallelGraphics is still making VRML authoring tools.

I have a soft spot for VRML, and would love to see a revival.

ParallelGraphics (a 3D VRML company)

Help, when is a number a Number?

So, I'm debugging some code...and trying something I haven't used before. I've used my_array instanceof Array, and it works just fine. But when I try to use num instanceof Number, not so good. Does anyone know why this code gives me a "false":


var n:Number = 3;

trace(n);

var isnum = n instanceof Number;
trace("n a number " + isnum);

October 20, 2004

Wayback Machine

What has the index page of GalaxyGoo looked like over the last few years? Take a look with the Internet Archive Wayback Machine.

October 18, 2004

Using post-it notes to teach about movie clips in Flash

When I'm learning or teaching something, I like to get visual. Tangible is great, when possible.

Today, I was trying to convey the beauty of nested movie clips. Frances is working on the prototype of the cell learning tool, and she's creating the parts of the cell as movie clips. Each organelle has it's own movie clip, and some of them are nested. We were discussing the benefits of this, when I saw an opportunity to explore one of the benefits of the movie clip: you can create a prototype that you can just plop finished graphics into later.

I took a piece of paper and called it my stage. On my desk, I've got post-it notes of various sizes and colors. I took the larger notes and started giving them names and stuck them to the stage paper. Then I took smaller notes, and gave them names and placed them on the larger notes. When I replaced smaller notes and even removed larger notes on the page, Frances had an "ah-ha!" moment. Gotta love post-it notes.

October 12, 2004

Cell Project Update

Frances administered the quiz on Monday, to two sessions of seventh graders. Today we met and went over the results. We knew there would be a performance gap (test scores less than perfect), but we were still surprised. The students had been tested on the material only two weeks before, yet students that had scored well on the official test didn't even pass our quiz. Was the quiz harder? Nope, the questions were almost identical. Does this mean that the students are not retaining the information after the exam? Very likely. Frances will be posting more about this in the project blog.

We also brainstormed ideas for the learning tool. How can we make the material more tangible to the student? Our time is limited, so rather than cover both animal and plant cells we're focusing on the animal cell for this version. We're going to animate the functions of each organelle, in addition to providing labels and text explanations, when the user clicks on a part of the cell. This version will basically be a prototype of a larger project we'll be working on over the next several months.

I've uploaded some new documents for the project, including the quiz.

Don't end up with RavioliCode!

As a sort of warning towards people starting out with OOP and trying to turn every little tidbit of a possible object into a (sub)class, I would like to say this:

Don't end up with RavioliCode.

For those familiar with the well-known SpaghettiCode nightmare, RavioliCode is the exact opposite and can be an equally big nightmare. Too many (sub)classes are confusing and you'll end up looking for the right (sub)class more than anything else. Use common sense :)

Ofcourse, there's also LasagnaCode.

I don't know how Italian cuisine got pulled into coding, but if anybody else has any programming terms using some kind of cuisine, let us know! After all, not everybody is into pasta.

October 11, 2004

Learning OOP in Flash, part 2

After a lot of reading and thinking, I'm giving this another shot. I'm thinking out loud--trying to build a clear mental picture of object oriented programming in Flash and how it compares with traditional procedural programming.

With object oriented programming's classes and subclasses, there are really two things going on. Firstly, a subclass expands the code of its base class. Secondly, the subclass describes a more specialized object than its base class does.

Let's say we want our project to generate a random fraction. No problem. We just write a class that generates two random numbers and returns them in an array: one number for the numerator(top) and one for the denominator(bottom). Great, now we've got random fractions whenever we want them. But what if we get a fraction like 4/6? We may want to simplify that to 2/3.

If I were writing this procedurally, I'd write a subroutine to simplify the fraction and call it from within the routine for generating a random fraction. My routine would be dependent on the subroutine,and if I needed both simplified and "raw" fractions, I'd have to write two separate routines.

With OOP, I can write a general class, and call it from within a more specialized class. But I can also call it from anywhere. The general case is no longer dependent on the specific case. Pretty cool.

When the subclass (let's call it "simpleFraction") is called, it executes code from the base class as well as the subclass. I'd call that expansion of the class' code. From the object model, the object created from the subclass is a subset of objects that could be created from the base class. For example, a class that generates random fractions may return 4/6 and 2/3, but a subclass that simplifies those results would only return 2/3.

By the way, the comments on my first post on this topic have been very illuminating.

October 10, 2004

Flash of Brilliance--Flash cards for kids

Flash of Brilliance -- Flash cards for kids

Contact

It may take some time, but I do work my way down the very long and ever growing to-do list for GalaxyGoo. Today, I got around to adding a contact form in the side bar of the blog index page. It's a no-frills widget, but it's been working well on other pages of the site.

October 08, 2004

Upcoming Sally Ride Science Festivals

I just saw a poster for a local Sally Ride Science Festival, and thought some of the GalaxyGoo audience might like to know about it. Here's the next four scheduled festivals:

October 10, 2004
North Carolina State University, Raleigh

October 17, 2004
Stanford University, CA

November 20, 2004
University of California, Irvine

January 29, 2005
Brevard Community College, Cocoa, FL

IBM's ' Statistical programming with R'

R is a language and environment for statistical computing and graphics. It is also free and open source. And it is also a very impressive piece of software which has a healthy community behind it and the code-archive called CRAN has many gems.

IBM is doing a 3-part series of articles on the usage of R called 'Statistical programming with R'. At the moment 2 of the articles have been put online:
Part 1. Dabbling with a wealth of statistical facilities
Part 2. Functional programming and data exploration
Part 3 will focus on finding patterns in large data sets.
Each article ends with an extensive list of resources and links (not just R-related!), also not to be missed.

These can serve as a perfect introduction to R and statistical software in general. And who knows... Doesn't a Flash front-end to R sound really nice? :) It doesn't sound impossible...

October 06, 2004

FlashGoddess at NASA

Jenny Mottar is working at NASA headquarters, developing touch screen interfaces and exhibit designs. From what I've seen of the screen shots, I'd really like to see more. Read her interview on FlashGoddess: Jenny Mottar Spotlight

Learning OOP in Flash

My quest for OOP nirvana continues. Today, I pulled out an old java book and read the chapter on OOP. It helped a lot, because it was written for an audience with experience in C. Synapses have been firing like crazy, and I'm starting to get more of a deep understanding of how class an subclass really work in OOP.

One statement that really rang bells for me was that using classes is "a way of giving user-defined types the same privileges as types that are built in to the language."

I have a long way to go, but I feel like I'm making progress.


I still have problems with the terminology of superclass and subclass. I found these definitions enlightening:

"class = a data type

extend = to make a new class that inherits the contents of an existing class.

superclass = a parent or "base" class. Superclass is a very poor choice of name as it wrongly suggests the parent class has more than the subclass.

subclass = a child class that inherits, or extends, a superclass"

~Peter van der Linden, just Java, 1997

October 05, 2004

Linkup Central

This looks very useful: Linkup Central. Perhaps we'll give it a try for our next event.

~via Social Twister

The Cell Project

The Cell Project has begun in earnest. On Monday, Frances gives the pre-assessment quiz to 7th grade classes. We're eager to see the results.

October 04, 2004

That which we call a subclass, by any other name...

.. would make more sense.

ActionScript is not my first "programming language", and a lot about actionScript (and Flash in general) has confused me over the years. The whole subclass and superclass is a case in point.

It's the use of "sub" that I find distracting. "Sub" usually implies a smaller part of a bigger body. In my first programming classes, a subroutine was used to encapsulate the code of a larger routine. In most of what I've read on OOP in ActionScript, it seems almost as if "sub" is being used in a taxonomical sense, since most examples are fairly taxonomical. For example, there may be a class for "vehicle" and a subclass of "car", which extends the vehicle class. So, while car is a subset of all vehicles, is the car class really a subset of all vehicle classes?

I could be completely wrong on this, but I like to think of a superclass as a "base class" and subclass as an "expanded class".

For the sake of clarity, I've rewritten the tootsie roll example ;-)

Let's say that tootsie rolls are the base class. Add a hard candy shell and a stick, and we have an expanded class. Hmmm...I must be hungry.

Time for me to get something to eat, and think more about this later.

October 01, 2004

How Do You Test Your Code?

I've been working with Flash for a long time, and I've accrued some habits for testing and debugging as I develop in Flash. Yes, I like to work in the Flash IDE. I use trace a lot. But I also use a technique I started during my very early explorations with Flash: in project prototypes, I output to text fields on the stage during testing. I can visually lay out elements that I want to track with various user interactions that way. My teachers and professors drilled into me the impulse to constantly test my code. Write a line, test it.

Flash developers are a diverse and creative lot. How do you test and debug?