Monday, January 11, 2010

Programming: Java: Tutorials: Constructors, Sub-Routines, and Functions

 Certainly, I'd be glad to clarify for you.

When you have a "Sub-Routine" (pocket of code which just runs [fire and forget]) you aren't receiving anything back. You are just saying "go off and do this so I don't have to write out that code again".

A Function is like a Sub-Routine in the sense that it is a section of code that you can keep calling over and over again, accept that it also returns a value.

This can be seen in the code like so.

A sub-routine would be... (For a theoretical class called dog)

public void Bark() {
  System.out.println("Woof! Woof!!!");
}

Notice how seeing as we don't want to retrieve a value from the Bark() method we write Void in its header.

Now, lets say we wanted a Function to tell us how old the dog was... (Note "DogAge" is an integer variable)

public int getAge() {
  return DogAge;
}

Notice how because we want to retrieve an integer we place int instead of Void in the header. This makes the Function return an integer, which we give to it using the "return" command.

The difference is that if we wanted to make the dog bark we would literally type...

myDog.Bark();

and be done with it. But with a function we have to store the value it returns. Seeing as our Function in this case returns an integer, we store it like so...

int Age =  myDog.getAge();

Now we have created a new integer variable called age and given it a value from our Dog class.

But don't be fooled, a Function doesn't just have to return a pre-existing variable. Before we write...

return VAR;

at the end of the function, we can put any calculations we want.

This has been a very drawn out explanation. But don't worry it will become clear in time.

All you need to know about constructors and why I commented on them not "even" having Void in their header was that they are unique, and therefore easy to spot and manipulate.

//Beginning of the Dog Class
public class Dog() {

  //The variables to store information about the dog
  int DogAge;
  String Name;


  //Default Constructor
  public Dog() {
    DogAge = 5;
    Name = "Saxon";
  }


  //2nd Con. for if we want to specify an Age
  public Dog(int Age) {
     DogAge = Age;
     Name = "Saxon";
   }


  //3rd Con. for if we want to specify a Name
  public Dog(String name) {
     DogAge = 5;
     Name = name;
   }

  //4th Con. for if we want to specify a Name and Age
  public Dog(int Age, String name) {
     DogAge = Age;
     Name = name;
   }


  //A Sub-Routine to make the dog Bark
  public void Bark() {
    System.out.println("Woof! Woof!!!");
  }


  //A Function to retrieve the dogs Age
  public int getAge() {
    return DogAge;
  }


}

This sample class shows the fundamental differences between Constructors, Sub-Routines, and Functions.

I hope this helps.

Programming: Java: Tutorials: A Guide to Class Architecture

Java is a type of language known as OO or Object-Oriented. OO Programming differs from other types because it allows the programmer to write less code, which in turn is more efficient code.

As we know, in java a class is written in a .java file. Inside this file we denote the beginning of our class with a line similar to the one bellow.
public class Cat {
This line would create a public class named Cat. That would mean a class that any other class can access. We'll get more into that later.

Inside the class tags, if this is our main class, we have the main() method. This is the method which runs when the program starts.

public void main(String[] args) {
So, what have we done so far? We have made a .java file called Cat.java, which contains a public class name called Cat. Also we have a main method within our class.

So your thinking "So what?", that this is nothing new. Well, that may be but the class we have created has a major flaw. It's static!

Static vs. Non-Static

A Static class means class which just IS. I can't be recreated. It just runs. Values can be applied to it, and drawn from it, but you cannot make multiple instances of it.

What we need is a way to make each version of our class different. What we need is a Constructor!

Constructors

A constructor allows us to create multiple versions of our class in memory, each with different values.

The constructor is a sub-routine which is public, has no return statement (not even void), and has the same name call as the name of the class. In our case a constructor would look like this.

public Cat() {
This would be a "Default Constructor". The reason it gets this title is because it accepts no inputs from the class creating it. All it does is create a new instance of the Cat class using what ever values are already stored within it by default.

Also, like all other sub-routines we can have multiple versions of it, each with different parameters. Like so.

//Default Con.
public Cat() {
  s = 10;
  a = 5;
}

//Secondary Con.
public Cat(int Size) {
  s = size;
  a = 5;
}

//Thir-dary Con.
public Cat(String name) {
  s = 10;
  n = name;
}

//Fourth-dary Con.
public Cat(int Size, String name) {
  s = size;
  n = name;
}

See, by creating a constructor for each possible contingency we allow the Cat class to work under any circumstance.

Now from another class we could create multiple instances of the Cat Class, each with different values like so.

Cat c1 = new Cat();
Cat c2 = new Cat(5,"Dilbert");
Cat c3 = new Cat(7);
Cat c4 = new Cat("Dill");

or we could create an Array of Cat like so.

Cat[] cats = new Cat[4];
for (int i = 0; i < cats.size; i++) {
  cats[i] = new Cat((int)(i,"Cat: #" + i);
}

Sadly...

Sadly the powers that be at VHS aren't exactly "fond" of me posting how to's on my blog here so that's all I'm going to go over. This is the basics of the Class system, there isn't too much else to it accept for practice. Good luck!