Unfortunately, some people have a very rudimentary way of understanding basic things.  I’ve been asked on several occasions to explain simple programming concepts that seem to escape them.

Now, anyone with even a slight grasp of programming knowledge will likely raise one of their eyebrows and put even Mr. Spock to shame, because this is the kind of stuff we learned by ourselves when we were nine years old.  However, I’m not an elitist (mostly) and things like this remind me of how much I felt sorry for the poor fools in high school who couldn’t even spell their own name because they were too busy SMSing each other about their lack of genitalia while bathing in copious amounts of turpentine.

.

First of all, understanding how object-orientation works is of paramount importance.  Visualising code in your head without writing it down and staring at it for hours wondering what it’s supposed to do is a handy skill to have.  This is explicit with Java only (which is a crap language, but is popular because people are obese and lazy), but is easily transferable to other OOP languages.

So, say I wanted to create a simple program that outputted the text “You are fat.” in an OOP-like fashion, it would be something like this:

.

YouAreFat.java

public class YouAreFat {

  String message;

.

This stores a piece of data (’message‘) as a unique part of the class YouAreFat.  (Technically, it’s a reference, but people just say that to try and make you look stupid.)

.

  public Message(String conMessage) {
    this.message = conMessage;
  }

.

This is the actual constructor for the message.  By doing this, the message can be modified to be anything the programmer wants it to be, and have multiple, different instances of it.  Pretty useless, you might think?  Right now, yes.  But this allows high scalability and flexibility because you can add a lot more variables and data if you needed to without giving you a headache later on.

(The reason I prefix ‘con‘ to ‘conMessage‘ is to indicate that it’s a construction variable.  This is just for aesthetics.)

More on this later.

.

  public static void main(String args[]) {
    Message Message01 = new Message("You are fat.");
    System.out.println(Message01.message);
  }
}

.

This is the ‘test’ code; this is what calls the shots, so to speak.  Formally, this part of the code should be separate and in its own file — the reason behind this is to make the code easier to change and look less obfuscated (cluttered to you illiterates).  However, I usually ignore this unless the program is huge, because it’s a stupid rule.  So, why not just write System.out.println(”You are fat.”);?  Because then it would not be very object oriented, and you can’t build on it and manipulate it.  This is how you learn how it’s supposed to work — by starting off with something simple and easy to understand.

Message Message01 = new Message(”You are fat.”); instantiates the constructor.  Basically, this is saying “I want to make a Message object, called Message01, and give it a string of text that reads ‘You are fat.’”.  The line after that, System.out.println(Message01.message);, gets the data of the message and outputs it as text.

Think of it like an MP3 player, or iPod.  Each music track is like a constructor:

.

  String title;

  public Song(String conTitle) {
    this.title = conTitle;
  }

.

Now, you want to put some music on your iPod:

.

  Song Track01 = new Song("Ultra Sheriff - Galactic Fame");
  Song Track02 = new Song("Chopin - Scherzo No. 1 in B minor, Op. 20");
  Song Track03 = new Song("Karnivool - COTE");

.

You now have three songs on it!  Yay!

.

But wait, you want to actually play the music too!

.

  System.out.println(Track01.title);
  System.out.println(Track02.title);
  System.out.println(Track03.title);

.

This is basically the same concept.  This is important.  You must understand this concept, and so many people don’t.

Let’s say you want to expand upon your iPod (or JavaPod, actually).  You want it to distinguish between band and song, maybe so you can order them alphabetically in the future, or see how many songs from each band you have or something.  Easy!

.

String title, band;

.

Instead of just a title, this also defines the band.

.

public Song(String conBand, String conTitle) {
  this.title = conBand;
  this.band  = conTitle;
}

.

This constructs each track entry.

.

public static void main(String args[]) {
  Song Track01 = new Song("Ultra Sheriff", "Galactic Fame");
  Song Track02 = new Song("Chopin", "Scherzo No. 1 in B minor, Op. 20");
  Song Track03 = new Song("Karnivool", "COTE");

  System.out.println("Title: " + Track01.title + "\nBy: " + Track01.band);
  System.out.println("Title: " + Track02.title + "\nBy: " + Track02.band);
  System.out.println("Title: " + Track03.title + "\nBy: " + Track03.band);
}

.

And this creates and displays the track.  Notice there are now two inputs in Song.

However, let’s just say we accidentally named a track wrong, and we want to change it.  All we would have to do is create a method for changing an object’s data, and then run it in the test program (or main).

We add:

.

public void setTitle(String mutTitle) {
  this.title = mutTitle;
}

.

This is a void because it’s not going to return any data, it’s just going to change (mutate) the data of an object.  The reason I prefixed ‘mut‘ to ‘mutTitle‘ is to indicate the input is a mutation; aesthetics again.

We then add this to the main:

.

Track01.setTitle("Destroy All Humans");

.

Now Track01’s title will now display as something different when written to the screen!

Here is a slightly modified version of what the above code should look like at last:

.

public class Song {

  String title, band;

  public Song(String conBand, String conTitle) {
    this.title = conBand;
    this.band  = conTitle;
  }

  public void setTitle(String mutTitle) {
    this.title = mutTitle;
  }

  public static void main(String args[]) {
    Song Track[] = new Song[4];
      Track[1] = new Song("Ultra Sheriff", "Galactic Fame");
      Track[2] = new Song("Chopin", "Scherzo No. 1 in B minor, Op. 20");
      Track[3] = new Song("Karnivool", "COTE");

    Track[1].setTitle("Destroy All Humans");

    for (int idx = 1; idx < 4; idx++) {
      System.out.println("Title: " + Track[idx].title + "\nBy: " + Track[idx].band);
    }
  }

}

.

The for loop and track representation through an array is just a cleaner way of outputting an index of items that would otherwise clutter up the code.  It’s the same as writing System.out.println(”Title: ” + Track01.title + “\nBy: ” + Track01.band); three times, only changing the number in each one.  Don’t worry about understanding this if you failed grade 1 math.

Learning the rest is SO easy once you know this horribly basic and simple part.  Hammer it into your head, preferably with something heavy and made from iron.

2 Responses to “Programming tips you should have learned when you were sticking toy cars in your mouth.”
  1. Anonymous says:

    Did you learn this from The Brillant Paula Bean?

  2. xarcos says:

    My colleagues have the skills of Paula Bean, I’d say.

    Which isn’t so bad — I mean, eating metallic fragments when you were in grade 3 would no doubt give you greater returns in the future.

Leave a Reply