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.

Saturday 17 August 2013

ALBATROSS!!!

It’s been a long time since I’ve posted here, more than six months in fact. But I’ve been very busy with college work lately so I haven’t had much time to make things. So rest assured I haven’t forgotten about this blog, I’m just concentrating on school at the moment. I’m currently studying maths specialised, physics, advanced electronics and computer science at college. For you American readers out there, that’s a Tasmanian college, it’s not like a university. It’s years 11 and 12, just before university. We’ve just had our mid-year exams and I thought this was a good time to write about my most recent project.

A few weeks ago my friend had a fancy dress birthday party, I went as the albatross seller as played by John Cleese in Monty Python. It’s something I’ve always wanted to do, don’t judge me. The outfit wasn’t difficult to find, I just went to a costume hire shop but the albatross proved to be much harder. Weeks before the event, I scoured the internet looking for stuffed toy albatrosses. Or any sort of albatross at all. I looked through EBay, model shops, toy shops and Monty Python even had their own albatross-in-a-tray plush toy. But there was nothing in stock or in my price range anywhere. Buying albatrosses is harder than you might imagine! So in a moment of desperation, I decided to make the damn thing myself.

John Cleese (left) and Terry Jones (right) Performing live at the Hollywood bowl.
This is pretty much what I looked like on the night. 
 Here is a link to that Performance, and a warning, this does contain coarse language.

The first thing I had to do was decide what materials to make it out of. To start with, all of this was very hard for me; I hadn’t made a model of an animal since grade 2. That was a Platypus and it was made from chicken wire and paper mache. After talking to some of my more artistic friends, who do this sort of thing a lot, chicken wire and paper mache is what I went for. I made a wire frame in the general shape of an albatross, just based on pictures from the internet. It has a wingspan slightly narrower than a doorway; I did this deliberately just to make it practical. 

The wire frame then had to be covered with chicken wire, and for this I used ½ inch hexagonal chicken wire. It wouldn’t be good enough to simply stretch chicken wire over the frame because it would flatten out in the sections between the frame wires. So to give it some “form”, I stuffed the body and part of the wings with tissue paper. That certainly made it more "solid".

I’m not really sure why I did this, it just seemed right at the time. I covered the whole thing with masking tape. I think it was to smooth-out the surface a bit before applying the paper mache. If that is indeed what it was for, it certainly seemed to work.

The tail was very satisfying, it looks really nice but it was so simple to make. All I did was poke wooden skewers into the rear end in a sort-of fan pattern and then put masking tape over it. I think the effect works really well. 

The paper mache was pretty easy. It was suggested to me to use PVA mixed 50/50 with water and really long strips of paper, again by my artistic friends. 

Next was the beak. At last back to familiar territory, because it’s made from balsa wood. Before I even started to make the frame, I cut out two pieces of 12mm balsa easily big enough to make the beak and glued them together side by side. I made two of these in case I messed the first one up. But the carving and sanding all went fine and it looks just like an albatross beak.



The next thing was to paint it. This is where I needed help; I’m not that good when it comes to painting things artistically, which is what was needed here. So I asked my good friend Kat to paint it for me because she’s really good with those sorts of things and I’m glad I did because, as I’m sure you’ll agree, it looks great! 



The day of the party: First class has just finished, I rush to the shops to buy the rest of the things I need to finish the albatross box, and I then go home in my free to work on it. I still had the box and the straps to make so it was a bit of a rush job. The box is made of 3mm corflute and the straps are just one inch wide red ribbon. The box is simply taped together, nothing fancy, and the straps are stapled on. But it all held together and that night, I had a lot of fun making everyone at La-Porchetta have a good laugh.