Skip to main content
Engineering LibreTexts

5.4: Dealing Cards

  • Page ID
    15301
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    At this point we can create a Deck and start dealing cards. Here’s a simple example that deals five cards to a hand, and deals the rest into a draw pile:

    Deck deck = new Deck("Deck");
    deck.shuffle();
    
    Hand hand = new Hand("Hand");
    deck.deal(hand, 5);
    hand.display();
    
    Hand drawPile = new Hand("Draw Pile");
    deck.dealAll(drawPile);
    System.out.printf("Draw Pile has %d cards.\n",
                      drawPile.size());
    

    CardCollection provides dealAll, which deals all of the remaining cards. Here’s the output of the previous example:

    Hand:
    5 of Diamonds
    Ace of Hearts
    6 of Clubs
    6 of Diamonds
    2 of Clubs
    
    Draw Pile has 47 cards. 

    Of course, if you run this example you will probably get a different hand, because the deck is shuffled randomly.

    If you are a careful reader, you might notice something strange about this example. Take another look at the definition of deal:

    public void deal(CardCollection that, int n) {
        for (int i = 0; i < n; i++) {
            Card card = popCard();
            that.addCard(card);
        }
    }
    

    Notice that the first parameter is supposed to be a CardCollection. But we invoked it like this:

    Hand hand = new Hand("Hand");
    deck.deal(hand, 5); 

    The argument is a Hand, not a CardCollection. So why is this example legal? It’s because Hand is a subclass of CardCollection, so a Hand object is also considered to be a CardCollection object. If a method expects a CardCollection, you can give it a Hand, a Deck, or a CardCollection.

    But it doesn’t work the other way around: not every CardCollection is a Hand, so if a method expects a Hand, you have to give it a Hand, not a CardCollection.

    If it seems strange that an object can belong to more than one type, remember that this happens in real life, too. Every cat is also a mammal, and every mammal is also an animal. But not every animal is a mammal, and not every mammal is a cat.


    This page titled 5.4: Dealing Cards is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?