02. What is an Object?

The easiest way to understand an Object is to actually encounter one so let’s begin considering the game of BlackJack. Start by thinking about how the game is played. If you were physically sitting at a BlackJack table, what kind of Objects are present (if you’ve never played BlackJack then go and check out this quick tutorial). Probably the most obvious physical object that you deal with would be a playing card. (“deal with“…get it?… never mind…)

A Playing Card
If we were to consider what defines a playing card we are starting to think like an Object-Oriented Programmer. A card is defined by the following pieces of info:

  • The suit of the card
  • The rank of the suit
  • The colour of the suit
  • The value of the card
  • The rank of the card

Some of these properties may not matter in BlackJack but this is the crux of Object-Oriented programming. If we were to make a Card that only works for our BlackJack program then it can’t be reused in other card games. So in this tutorial we will attempt to make a fairly thorough Card class that could be used in other card games. (If you know about OO Programming and are wondering why I haven’t mentioned “Inheritance“…bare with me, it’ll come!)

Now that we’ve defined the variables that make up a playing card, we need to consider what you can do with one. Really a card doesn’t have actions but one that might be useful later is to be able to state all the information about a card succinctly, so an action of the card could be:

  • Print the card value

So now that we’ve defined a card in regard to its values and it’s actions it’s time to clarify that with a diagram. This is where UML and in particular yUML comes in:

The Card object

The Card class

yUML is an excellent tool for drawing classes/objects within a system and defining how they link to each other. In order to create that picture above I’ve simply used this code:

[Card|
 suitColour;suitName;suitRank;cardName;cardRank|
 printCardVal()]

Don’t worry if that code is confusing, I’m going to walk you through the diagrams used in this tutorial. If you’re interested in creating your own UML diagrams then the yUML website has some good samples to use as a guide. Being able to define a picture through code is an ingenius concept and as you’ll see through this tutorial, it makes clarifying the project a whole lot easier.

Okay, you might have noticed me using the word “Class” a couple of times and so I think it’s best for me to clarify Objects and Classes at this point.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>