Thursday, October 7, 2010

How to actually make things run

My example code has been in C and Java, however I've not said how to get a computer to run either a C or a java program.

To run java code you first place it in a file with the extension ".java" on the end.
This file must contain one and only one class declared public and that class must contain a method named main which is declared like this:

public static void main(String args[])

The file also must contain a package declaration at the top like:

package MySillyJavaPackage;

Once you have these, you call the java compiler, either through using the build menu item in eclipse or on the command line. If you use the command line, you type "javac yourfile." If your program compiles without errors, the compiler will produce a directory with the name declared in your package declaration. Inside this directory their will be a file with the extension ".class" and the same base name as your original file. To run it, type "java PackageName/yourfile." Do not use any extension with name of the file you want to run.

With C you must have a main function. As pure C is not object oriented the main function is not part of any object. The incantation for compiling your file varies. If you are using GNU C on the command line, it is gcc filename. If your file is error free the compiler will produce a file called "a.out" in the same directory your source file was in. To run it, just type "./a.out" and away you go.

Now you may wonder what good all this does, after all I've given no way to get output from your code. I've delayed this longer than I should have because I wanted to discuss library routines and input and output at the same time.

To have your program print anything, you need to include or import prewritten code that does output for you. How does that code work? It directly manipulates the hardware interfaces connected to your screen and keyboard. Writing it is both highly specialized and fairly tedious for most people, which is why if you aren't an embedded systems hacker you usually don't bother with it.

Anyway the library used for input and output in C is called stdio. To gain access to it you use a preprocessor directive. The particular directive is

#include<stdio.h>

Which means get the file called "stdio.h" from one of a few standard places. You can use this include other libraries such as the C Math library. Included in stdio are definitions for the C functions printf and scanf. Printf is used like

printf("%d silly dogs played with the ball\n",&dogs);

The first argument to the printf function is called the format string. %d means insert an integer value into the format string. The "&" in front of the variable dogs, means that we are passing it's address rather than it's value into printf, another example of a C pointer.

In Java output is a little easier. Instead of using a format string you would just type

System.out.println(dogs," silly dogs played with the ball\n");

The "\n" used in both examples is just a sign for a carriage return.

Now, while raw text output is perfectly good for some things like program logs, most programs written today have other, more intuitive, ways of interacting with a user. How to make a GUI, a graphical user interface will be discussed in my next post.

Monday, October 4, 2010

Who can see data?

Now one way to program in C is to put all the data outside of your methods, called functions in plain vanilla C. To do this C you write a bunch of declarations like:

int age;
double temperature;
char Name[20] = "Maria\0";

By doing this you are declaring what are called global variables. These can be accessed by any function declared in the same file of program code and ,if certain conventions are followed, also by functions declared in other files. At first glance this seems like a good idea, as it easy to pass data between your functions, but it has pitfalls. Without going over the code with a fine tooth comb it's impossible for someone reading your code to know if a particular function alters a particular global.

One way to deal with this is to declare local variables:

int AverageThem(int a, int b)
{
int c;
c = (a + b)/2;
return c;
}

In this pointless snippet of code a,b, and c are all local variables
in AverageThem. Outside of AverageThem those particular storage locations are not associated with the names a,b, and c or the type int. In fact a,b, and c are recreated every time the computer executes the AverageThem function. In C you can declare local variables in every block. A block is anything between two curly "{}" brackets so

main()
{
int a = 2;
{ int a = 3;
printf("%d\n",a);
}
printf("%d\n",a);
}

First prints a "3" and then a "2."

Now suppose you have a different function:

GreetUser(char *Greeting)
{
static char CurrentGreeting[50];
if (Greeting != NULL) {
strcpy(CurrentGreeting,Greeting);
}
puts(CurrentGreeting);
}


Because this function will behave badly if Greeting is longer than fifty characters or is not terminated by a NULL character don't copy it. However, it illustrates the use of the keyword static what static does is cause the value stored in the string CurrentGreeting to persist between in invocations of GreetUser and only change if a non-null pointer is past to the function.

What is this pointer thingy which hasn't been mentioned before? It's a variable which, instead of storing a numerical, character, or object value, stores the location of that value. In C an array is equivalent to a constant pointer. While pointers are useful, and are used a lot C programs, I intend to use C examples to illustrate programming with C-like languages in general so I won't go into C pointers in detail. Puts and strcpy are simply C library functions. They, again, have certain problems and would probably not be used by a contemporary programmer, I'm just using them here for simplicity's sake.

Now object oriented languages have a different approach to deciding who can see information and who can't. While they still have local,global,and static variables. The creator of an object gets to pick what fields and methods are visible to other code. To do this a C++ or Java programmer uses the keywords private,public, and protected.

For example in my last post I created class PetRecord

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);
};

The public modifier means that any method could contain a call GetOwner and the other methods I defined. It doesn't whether that method is part of class PetRecord or not. The data fields, however aren't declared public. Therefore they can only be seen by methods that are part of the same Java package as PetRecord. If you don't want anything outside of PetRecord to see them a better choice might be to declare them private.

I have no static methods or fields in the version of PetRecord above. Let's add one.

public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
static integer PetCount = 0;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
public PetRecord(String theOwner,String theName) {
Owner = theOwner;
Name = theName;
PetCount = PetCount + 1;
}
};

The method I added, called a constructor initializes a PetRecord at the time one is created. The variable PetCount is also incremented. Why do this? Because PetCount is a static variable there is one PetCount shared between all instances of the PetCount class. By incrementing it, we can keep a count of the number of pets we have.

There are also a protected access specifier in Java and C++, but it isn't used as much as public or private.

In my next post I'm going to back up from the micro-level stuff I've been discussing and talk about how you can actually get programs to compile and produce output.

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.