08. The Constructor

A Constructor is an important Object-Oriented programming concept because it defines the rules on how an Object is created. Thinking back to the physical representation of a BlackJack game, how do we create the different playing cards that make up a deck (oooooooooh, did I just allude to another Class we’re going to require???) The Constructor is how each of our different playing cards get created.

As I mentioned before, I’m not going to stress over the programming involved in making BlackJack, you’ve hopefully done my Programming tutorial or at least made some programs before and so “source code” is not going to be new to you. Let’s start with the creation of a Card’s suit. Copy the following code into our empty constructor:

suitRank = (cardNum / 13) + 1;
switch (suitRank) {
case 1:
  suitName = "Spade";
  suitColour = "Black";
case 2:
  suitName = "Club";
  suitColour = "Black";
case 3:
  suitName = "Diamond";
  suitColour = "Red";
  suitName = "Heart";
  suitColour = "Red";

You can see here we’ve created a value for “suitRank” by forcing it to be a number between 1 and 4 (provided cardNum is less than 52). Do we need a suit rank for BlackJack? No. But why build a class that can only be used for BlackJack? This type of broad thinking should always be in your mind when creating an OO program. After the rank has been determined we then use a switch statement to give this particular card a suit name and suit colour.

You might have noticed I’ve used the Constructor’s parameter “cardNum” in this code. I will show you how this works a bit later so don’t worry if it doesn’t make sense just yet.

Now underneath our “construction” of the suit (see why it’s called a Constructor?) let’s “construct” the card’s value:

cardRank = (cardNum % 13) + 2;
if (cardRank < 10) {
  cardName = Integer.toString(cardRank);
} else if (cardRank == 10) {
  cardName = "Ten";
} else if (cardRank == 11) {
  cardName = "Jack";
} else if (cardRank == 12) {
  cardName = "Queen";
} else if (cardRank == 13) {
  cardName = "King";
} else {
  cardName = "Ace";

I’ve used Java’s modulus function (“%”) to help ensure “cardRank” is between 2 and 14. You might have noticed that I haven’t told the face cards to be worth 10. That rule may be true of BlackJack but does it have anything to do with a playing card? Most of the time a playing card would have a ranking system that goes from the “2″ through to the “Ace”, so we’ll mimic that and worry about BlackJack rules later.

Once again you can see we’re thinking like an Object-Oriented programmer now. This Card class can stand alone, completely separate from the game of BlackJack and that’s a vital Object-Oriented programming consideration.

Okay, so if we’re trying to mimic a real playing card, what about the Jokers? Or what about the numbers 12 to 14 which are used in six-player 500? Well, you can go ahead and alter this class to account for every possibility if you like but for now I’ll just include code for the use of Jokers.

Jokers are of course quite different to regular cards so the constructor will require some alteration. Change it so it’s more like the following:

public Card(int cardNum) {
  if (cardNum < 52) {
    // ... this is where all the code we already had is to go ...
  } else if (cardNum < 54) {
    if (cardNum == 52) {
      suitColour = "Colour";
      cardRank = 16;
    } else {
      suitColour = "Black and White";
      cardRank = 15;
    suitName = "NIL";
    suitRank = 0;
    cardName = "Joker";
  } else {
    suitColour = "NIL";
    suitName = "NIL";
    suitRank = 0;
    cardName = "NULL";
    cardRank = 0;

Here you can hopefully see we’ve accounted for the possibility of one or two jokers as well as NULL for any card beyond the 54th in a deck.

My implementation here is not even close to perfect but just remember, this tutorial isn’t about perfect programming it’s about understanding Object-Oriented programming!

One thought on “08. The Constructor

  1. You might have noticed I’ve used the Constructor’s parameter “cardNum” in this code. I will show you how this works a bit later so don’t worry if it doesn’t make sense just yet.

    do u mind giving explaining what cardNum does?

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>