Thursday 17 October 2013

Object-Oriented Programming and Recursion

Object oriented programming is a concept which I have been using for a few years now ever since I first started learning Java in Grade 10 Computer Science class.
I perceive object oriented programming to be a model used for writing programs. The goal of object oriented programming is to have a network of different objects which interact with each other. Each object is represented by a class which is basically the blueprints for that object.
I find object oriented programming to be useful because it provides a great framework for programs and it is very easy to edit code that uses objects.

Recursion, unlike object oriented programming is a very hard concept to grasp at first. Recursion is an approach to solving problems which can be very complex but can also be broken into multiple smaller and easier problems to solve. In order to solve a problem recursively, you must first think of the simplest base case(s). You then have to think of a way to break the bigger problem down into smaller problems which can be solved individually and then put together to solve the bigger problem. This is done by recursively calling the function until the base case is met.
One of the simplest cases of recursion would be the Factorial:
All of us know that the factorial, f(n), where n is an integer is defined as:
if n is 0 then n! is 1
otherwise, n! is n * (n-1)!
In order to solve this problem for a case when n is greater than 0 using programming, you must recursively call the function until the base case is met. So for this example if n is 1, then your function will return n * (n-1)! which would be 1 * (0)! and we know that 0! is 1 so we get 1 * 1 as our answer.

I feel that recursion is extremely useful for computer scientists because it can be used to solve problems that are impossible to solve using loops such as finding the maximum depth of a binary tree or solving the Tower of Hanoi problem.

-Andrew