Activity 2: Deck Class

Introduction

Think about a deck of cards. How would you describe a deck of cards? When you play card games, what kind of operations should the deck be able to do?


Introduction

Think about a deck of cards. How would you describe a deck of cards? When you play card games, what kind of operations should the deck be able to do?


Exploration

Make a class in your package called Deck and another called DeckTester. Copy/paste the code below into it. Take a moment to familiarize yourself with the Deck code. Especially note its constructor's parameters and its private instance variables.


Exercises

  1. Complete the Deck class by coding each of the following:
    1. CONSTRUCTOR: So far, the constructor receives three arrays as parameters (rank, suit, point value). Your task is to take the information from these arrays and create a card to represent each rank-suit possibility. Please note that the point values are connected to the ranks -- that is, if the ranks are {"Jack", "Queen", "King"} and the point values are {1, 2, 3}, then every Jack will be 1, every Queen is 2, and every King is 3. Each card that is created should be added to the List called cards. The size should be set to the number of cards that were created. The constructor should also shuffle the cards by calling the shuffle method, which we'll implement in Activity 4.
    2. size method - return the number of cards in the deck that have not been dealt
    3. isEmpty method - return true when the size is 0, false otherwise.
    4. deal method - This method deals a card by removing and returning a card from the deck (if there are any cards left to be dealt). It will return null if the deck is empty. Here are a few ways you could do this:
  • ALGORITHM 1: since the cards are in an ArrayList, you can call the method that removes and returns an object at a specified index. The choice does not have to be random, since we will be shuffling the cards. Removing the last card is more efficient than removing the first card (because, behind the scenes, every card has to be shifted up a space). You should also create a separate list where you keep track of all the discarded cards.
  • ALGORITHM 2: It's more efficient to leave all the cards in the original list. Instead of removing the card, just return the card at 'size' and then decrease 'size' by 1. This is perhaps less like true life, but will work better.

2. In DeckTester, make three Deck objects and test out each method.

Questions Answer these in your google doc!

  1. Explain the relationship between a Deck and a Card.
  2. Consider the deck initialized below. How many cards will the deck have?
  3. The game Twentyone is played with a deck of 52 cards. The highest ranking card is ace; the lowest is 2. The suits are spades, hearts, diamonds, and clubs. Face cards are worth 10 points, ace is worth 11, and the point values for 2-10 are 2-10, respectively. Fill in the blanks for this code so that it will properly initialize a deck for this game: Deck d = new Deck(____, ____, ____);
  4. Does the order of the elements of the ranks, suits, and pointValues arrays matter?



Deck.java

import java.util.List;
import java.util.ArrayList;

/**
 * The Deck class represents a shuffled deck of cards.
 * It provides several operations including
 *      initialize, shuffle, deal, and check if empty.
 */
public class Deck {

  /**
   * cards contains all the cards in the deck.
   */
  private List<Card> cards;

  /**
   * size is the number of not-yet-dealt cards.
   * Cards are dealt from the top (highest index) down.
   * The next card to be dealt is at size - 1.
   */
  private int size;


  /**
   * Creates a new <code>Deck</code> instance.<BR>
   * It pairs each element of ranks with each element of suits,
   * and produces one of the corresponding card.
   * @param ranks is an array containing all of the card ranks.
   * @param suits is an array containing all of the card suits.
   * @param values is an array containing all of the card point values.
   */
  public Deck(String[] ranks, String[] suits, int[] values) {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
  }


  /**
   * Determines if this deck is empty (no undealt cards).
   * @return true if this deck is empty, false otherwise.
   */
  public boolean isEmpty() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
  }

  /**
   * Accesses the number of undealt cards in this deck.
   * @return the number of undealt cards in this deck.
   */
  public int size() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
  }

  /**
   * Randomly permute the given collection of cards
   * and reset the size to represent the entire deck.
   */
  public void shuffle() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
  }

  /**
   * Deals a card from this deck.
   * @return the card just dealt, or null if all the cards have been
   *         previously dealt.
   */
  public Card deal() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
  }

  /**
   * Generates and returns a string representation of this deck.
   * @return a string representation of this deck.
   */
  @Override
  public String toString() {
    String rtn = "size = " + size + "\nUndealt cards: \n";

    for (int k = size - 1; k >= 0; k--) {
      rtn = rtn + cards.get(k);
      if (k != 0) {
        rtn = rtn + ", ";
      }
      if ((size - k) % 2 == 0) {
        // Insert carriage returns so entire deck is visible on console.
        rtn = rtn + "\n";
      }
    }

    rtn = rtn + "\nDealt cards: \n";
    for (int k = cards.size() - 1; k >= size; k--) {
      rtn = rtn + cards.get(k);
      if (k != size) {
        rtn = rtn + ", ";
      }
      if ((k - cards.size()) % 2 == 0) {
        // Insert carriage returns so entire deck is visible on console.
        rtn = rtn + "\n";
      }
    }

    rtn = rtn + "\n";
    return rtn;
  }
}

DeckTester.java

/**
 * This is a class that tests the Deck class.
 */
public class DeckTester {

  /**
   * The main method in this class checks the Deck operations for consistency.
   *  @param args is not used.
   */
  public static void main(String[] args) {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
  }
}