OOPs, there goes another Java program!

It’s been a while since I published a proper tutorial, so I thought I’d share a program that shows off some of the features of Object Oriented Programming in Java. If you want to, you can get the code here.

Object Oriented Programming (or OOP) is a very important part of modern computer programming. For a while, C was the most popular language (here’s a .pdf of Kernighan and Ritchie’s classic book: The C Programming Language). However, as programs got more complex, it became near-impossible to write them in C.

Simula-67 was the first language that used OOP. It allowed you to create “classes” – these are basically blueprints for creating software objects. You’ll see what I mean by this later in the tutorial. Smalltalk was probably the most successful early Object Oriented Programming language (and it’s still alive today in, for instance, Squeak and Pharo). These languages influenced the creators of C++ (aka C with classes) and Java.

Okay, that’s enough history. My program is called: “RomeoAndJuliet.java”. It’s a very simple, console based program. It starts like this:

The code starts with a comment and then I am straight into declaring my first class. The program lets me create “Tragic_Lover”s. All instances of this class will have some attributes: a name (which will be stored in a string), an age (stored as an integer), a gender (I’ve used “char” because this will just be one character), a string telling us who they are in love with and another which stores the way they will choose to kill themselves.

Here’s the next section:

As you can see, this part is a constructor. This happens automatically when a new “Tragic_Lover” is created. In brackets after the class name, we see references for all the attributes we’ve set up. The program tells us that a new Tragic_Lover has been created. It puts the data into the object’s variables and then there are some lines which print out the lover’s name, age and gender.

The next bit is very similar because it’s an “overloaded constructor”.

If we want to create a Tragic_Lover who is not in love with anyone, we can now. This constructor doesn’t take a value for “in_love_with”. Instead, it puts the string, “nobody” in that variable. So if we try to make an instance of this class without that fifth parameter, Java will automatically use this constructor instead. I realise that this is a rather contrived example, but it does demonstrate this feature of the language.

The program continues …

Now we are defining some methods. These are things that “Tragic_Lover”s can do. The first one just tells us who this instance of the class is in love with. The next one, “seen_by”, is more interesting. When one of our lovers is seen by another, it calls the love_at_first_sight method belonging to that instance (here referred to with the variable “hot_stuff”. So, when Juliet is seen by Romeo, she send a message to him to fall in love with her (and vice versa). The fact that software objects can communicate with each other is one of the things that makes OOP so powerful.

Something similar happens  in the next method. When fate keeps the lovers apart, one lover’s “star_crossed” method sends a message to the other lover, leading them to commit suicide. Next we have the methods I have been talking about – one that sets a Tragic_Lover’s in_love_with variable to the name of the lover who called the method, the other which tells us that they have committed suicide.

Now we get to the main part of the program:

First we create two “Tragic_Lover”s. We don’t supply a value saying who Juliet is in love with, but the overloaded constructor will have no problem with that. Next we call the “loves” method, to find out who they are in love with. After they meet at the party, Juliet is seen by Romeo, which causes him to fall in love with her (and vice versa). We call the “loves” method to check this. Sadly, fate keeps them apart and so Romeo calls Juliet’s star_crossed method which will result in her suicide (and the same happens in reverse to him).

Here’s the output of this sad story:

Okay, that all for now!

(By the way, Shadow, if you read this, let me know if I have made any rookie errors!)



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: