Monday, June 8, 2009

Object-Oriented Programming with Greenfoot

We've heard a lot about programming. For those of us that have tried to study it, we've heard the phrase "object-oriented" even more. So what exactly is it?

"Object-oriented" basically means the same thing as it sounds. Its a programming language based on "objects", which can be referenced in the code multiple times. Let us take this sample scenario. You have a world, with cells in it, and you want to stick Rocks into it. So in the code, you have to make everything that defines a Rock as many times as you need it, which would take a long time. However, in object-oriented programming, you can have a Rock class. In an object-oriented language, you can reuse the Rock class, by writing it once, and making it over and over again, just by initializing it, like this:

public class Rock() {
//there's currently nothing in this

Rock myRock = new Rock();

Now for those technologically challenged people who have no idea what I just wrote, here's an easier approach to this problem. Greenfoot, an open source Java (programming language) ide (interactive development environment), makes learning programming much easier. Download and install the program onto your computer (you'll need a recent Java JDK), to try things yourself, or watch along with this tutorial.

Upon opening Greenfoot for the first time, select the "wombats" scenario. Then, something similiar to the below will open. The square grid in the middle is the "world", or place where all the objects live in. To the right are the "classes", the types of objects that have been premade for you.

To begin experimenting, right click on a "Wombat" in the classes. You will be given the option of "new Wombat()", which initiates a new wombat. Put that Wombat into your world. Right click the Rock class, and click "new Rock()", and put the rock into the world. You have just created instances of classes, your very own objects. Continue placing rocks and leaves in the world (make sure not to overcrowd the world with rocks).

Now, you may be thinking, "this is a very uninteresting diagram of a wombat and rocks and leaves." Well the thing you don't know is that the world is currently paused. Click on the "Run" button on the bottom, and watch the wombat move. The Wombat is precoded to not run into rocks, and eats leaves. Click pause, and right click the wombat in the world. You given a list of "methods", basically functions that the wombat can carry out. Below is a screenshot of the scenario in action.

You may be wondering about what makes this program tick, and where all of the coding is. Right click the Wombat class on the right, and click "Open Editor". You will see something similar to the below screenshot:

You can play around with the code, and try to add your own, or maybe wait for the next post! Happy Programming!

That's all folks for today! Please comment on tech as we need comments to improve on the tech guide. Don't hesitate to ask any questions. Thanks!
By - WinTechpedia™ 2009 Contributors