Sunday, 18 August 2013

Factorial method.

One of the things we have been learning about in computer science at college is using methods in programming. Methods are an absolute god-send. Rather than having to write big long strings of code, instead you can group a small chunk together and give it a name, and then retrieve that small piece of code at any time with just one line.
Here is an example method.

int a_plus_b;

Public void add(int a, int b)
{
                a_plus_b = a + b;             
}

"Int a_plus_b" is a global variable, that means it can be used or changed at anytime, anywhere in the program. The bit between the curly brackets is the code that the method executes. ‘a’ and ‘b’ are the formal parameters of the method. "Public" means the method can be called from anywhere in the program. "Void" means the method doesn’t return a value.  "add" is simply the name of the method and this could be anything you like, it could be "pony" or "omnibus", but it's convention to call methods something to do with what they do. "Int a_plus_b" isn’t part of the method, but I had to declare that variable. "Int" just means that the variable type is a number. To implement or "call" this method you would write the following line:

add(2, 3);

Where 2 and 3 are the actual parameters you put into the method but these can be any number you want. So when you write “ add(2, 3); ” the value of “a_plus_b” becomes 5, similarly if you were to write “ add(18, 5) “, “a_plus_b” would be equal to 23.


This is a method I wrote to return the factorial of a number.
If you don't know what a factorial is, it is when you multiply a number by each number that comes before it. The symbol for factorial is '!'. So, 3! = 3x2x1 = 6. Similarly 5! = 5x4x3x2x1 = 120.

public int factorial(int a)                 //Method declaration. The input to this method must be
{                                                  // an integer (a)
int afact = 1;                                 //afact will be used to generate the factorial and will     
    //be equal to the factorial of a.
boolean neg = false;                      //Declaration of a Boolean variable to state if ‘a’ is positive
                                                    //or negative.
if(a < 0)                                        //If a is negative (less than zero) neg, which stands for                                                     //negative will be set to true.
    //else it will stay as false.
{
                neg = true;
                a*= -1;                        //If a is negative, it must be changed to positive.
}

if(a == 0)                                     //The factorial of 0 is one, so if a is 0, afact must be 1.
{
                afact = 1;
}
else                                               //a will only get here if it is a positive integer.
{
                while(a >= 1)                 //In the while loop, afact gets multiplied by (a) and then
     //(a-1), and then (a-2) and so on down to 2 and 1.
                {
                                afact=afact*a;
                                a--;
                }
}
                               
if(neg == true)                               //Finally, if the input number was negative, which was
   //checked for earlier, the output number gets changed to 
   //negative.
{
                afact*= -1;
}
return afact;

}

And that is how you calculate a factorial, at least it’s one way, there are more mathematically precise ways of doing it, but this method works for integers and I’m happy with it. A few weeks after I wrote this method, my teacher taught the whole class a factorial method which was amusing for me as I’d already written this one.

No comments:

Post a Comment