Friday, October 1, 2010

More About Data And A Bit About Methods

In my last post I had something called 'a'. 'a' was a variable. In computing, unlike math, a variable is not a name for an unknown quantity, instead in denotes a particular location in the computers memory. What a variable can contain depends on the the language you are programming in.

In strongly typed languages, such as C and Java, you tell must declare your variables, telling the computer what kind of thing it will contain before you use it. You can also define a variable when you declare it, setting it's initial contents to what you want it to be. For example:
int a = 23;
Now, in computer hardware numbers that humans would handle similarly can be handled very differently. Int means that the variable should contain an integer, a number with no fractional part. If the number must be capable of holding a fractional part, it should be declared as float or a double if you are using C or Java.

One can also declare variables to hold text data as well numbers. In C one would do so with the lines like:
char C = 'a';
or
char C[10] = "Told ya!\0";
Now we come to something more interesting. Why did I put '[10]' in the second line and '/0' at the end of "Told ya!\0"?

In the second example C is an array, chunk of several consecutive memory locations. The [10] tells the C compiler to reserve enough memory for ten alphanumeric characters. The '\0' represents a null character necessary to tell some C functions that the character string is complete. The use of such special values was commonplace in the late 1960s when C was invented but these days is considered an obsolete approach.

So we have arrays, yay! Now what good are they actually? Well, with arrays one can program something like:

/* Toy example, first 100 Fibbonaci numbers */

int fibs[100];
int index;
fibs[0] = 0; /* C array indices start with 0 not 1 */
fibs[1] = 1;
for(index = 2 ; index <= 100 ; index = index + 1) { fibs[index] = fibs[index - 1] + fibs[index -2]; }



The stuff inside the "/* */" delimiters are comments, bits of text met to be read by humans that computer removes before processing the actual program code. By using arrays a programmer can elegantly have a computer repeated perform the same operation on large blocks of data.

But what if several different types of data should go together. In modern terms you use something called an object. Here's one, declared using Java rather than C syntax since plain, vanilla C, while still excellent for some purposes, doesn't really have objects.

public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
};


Once you've defined a PetRecord, you can declare variables, including arrays of that type.
To access individual fields of a PetRecord, you'd write code like:

PetRecord Fluffykins;
Fluffykins.UpdateRabiesDue(Calendar.getInstance());

What in bleep are things with parentheses on the end? Getowner() and UpdateRabiesDue() are methods called functions in C. In Java every method is attached to a certain object of class of objects. This isn't the case in C or even C++ where standalone methods, called functions can be declared. What's special about methods is that the same method can be called with different parameters. For instance if Fluffykins is given to Mrs. Jones, because it urinated on the carpet too much at its old house you can call "Fluffykins.SetOwner("Jones");".

Now you may have noticed that the methods in my PetRecord class had the word "public" as part of their declarations. What exactly is that about? My next post will discuss the notions of scope and encapsulation.

No comments:

Post a Comment