11. The “Deck” class

Jumping back into Eclipse now, it’s time to add a new class. We’re going to take the same steps we did back here to make a brand new class in our BlackJack project called “Deck“.

Now that Eclipse has built a folder structure for our source files you can simply right-click on the “source” folder in your hierarchy to create your new class…this way you won’t have to worry about ensuring your package reference is correct, Eclipse will do it for you! Here’s what your settings should look like:

And now inside our new “Deck” class, we need to set up our skeleton to match our UML class diagram. Replace the code that’s in there with the following:

package source;

public class Deck {

   // MEMBER VARIABLES
   // ================
   private final Card cards[];
   private int topCard;
   private final int numCards;

   // CONSTRUCTOR
   // ===========
   public Deck(int deckSize) {
      // code to come...
   }

   // METHODS
   // =======
   public void shuffle() {
      // code to come...
   }

   public Card deal() {
      // code to come...
   }

   private int randomCard() {
      // code to come...
   }
}

So, a few things to note about our skeleton before we move on:

  1. We have an array called “cards” where each element is an instance of “Card
  2. Our constructor requires a parameter to tell us how big the deck will be
  3. Our “deal” method is going to return an object of type “Card

Before we plough on with the code required for our “Deck” class, it’s probably time to discuss some of the reserved words I’ve been using like “protected“, “private“, “final” etc…

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>