Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the implementation of the Deck class by coding each of the following: •

ID: 3675160 • Letter: C

Question

Complete the implementation of the Deck class by coding each of the following:

• Deck constructor — This constructor receives three arrays as parameters. The arrays contain the ranks, suits, and point values for each card in the deck. The constructor creates an ArrayList, and then creates the specified cards and adds them to the list. For example, if ranks = {"A", "B", "C"}, suits = {"Giraffes", "Lions"}, and values = {2,1,6}, the constructor would create the following cards: ["A", "Giraffes", 2], ["B", "Giraffes", 1], ["C", "Giraffes", 6], ["A", "Lions", 2], ["B", "Lions", 1], ["C", "Lions", 6] and would add each of them to cards. The parameter size would then be set to the size of cards, which in this example is 6. Finally, the constructor should shuffle the deck by calling the shuffle method. Note that you will not be implementing the shuffle method until Activity 4.

• isEmpty — This method should return true when the size of the deck is 0; false otherwise.

• size — This method returns the number of cards in the deck that are left to be dealt. • deal — This method “deals” a card by removing a card from the deck and returning it, if there are any cards in the deck left to be dealt. It returns null if the deck is empty. There are several ways of accomplishing this task. Here are two possible algorithms:

Algorithm 1: Because the cards are being held in an ArrayList, it would be easy to simply call the List method that removes an object at a specified index, and return that object. Removing the object from the end of the list would be more efficient than removing it from the beginning of the list. Note that the use of this algorithm also requires a separate “discard” list to keep track of the dealt cards. This is necessary so that the dealt cards can be reshuffled and dealt again.

Algorithm 2: It would be more efficient to leave the cards in the list. Instead of removing the card, simply decrement the size instance variable and then return the card at size. In this algorithm, the size instance variable does double duty; it determines which card to “deal” and it also represents how many cards in the deck are left to be dealt. This is the algorithm that you should implement.

2. Once you have completed the Deck class, find DeckTester.java file in the Activity 2 folder. Add code in the main method to create three Deck objects and test each method for each Deck object.

Deck Class:

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 + " Undealt cards: ";

       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 + " ";
           }
       }

       rtn = rtn + " Dealt cards: ";
       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 + " ";
           }
       }

       rtn = rtn + " ";
       return rtn;
   }
}

/**
* 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 *** */
   }
}

Explanation / Answer

/*********************** 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) {
       cards = new ArrayList<Card>();
       for (int j = 0; j < ranks.length; j++) {
           for (String suitString : suits) {
               cards.add(new Card(ranks[j], suitString, values[j]));
           }
       }
       size = cards.size();
       shuffle();
   }


   /**
   * Determines if this deck is empty (no undealt cards).
   * @return true if this deck is empty, false otherwise.
   */
   public boolean isEmpty() {
       return size == 0;
   }

   /**
   * Accesses the number of undealt cards in this deck.
   * @return the number of undealt cards in this deck.
   */
   public int size() {
       return size;
   }

   /**
   * Randomly permute the given collection of cards
   * and reset the size to represent the entire deck.
   */
   public void shuffle() {
       for( int k = size - 1; k >= 0; k-- ) {
            int r = (int)(Math.random() * k);
            Card tmp = cards.get(r);
            cards.set(r, cards.get(k));
            cards.set(k, tmp);
        }
   }

   /**
   * Deals a card from this deck.
   * @return the card just dealt, or null if all the cards have been
   *         previously dealt.
   */
   public Card deal() {
       if (isEmpty()) {
           return null;
       }
       size--;
       Card c = cards.get(size);
       return c;
   }

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

       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 + " ";
           }
       }

       rtn = rtn + " Dealt cards: ";
       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 + " ";
           }
       }

       rtn = rtn + " ";
       return rtn;
   }
}

/******************************* Card.java ****************************/

/**
* Card.java
*
* <code>Card</code> represents a playing card.
*/
public class Card {

   /**
   * String value that holds the suit of the card
   */
   private String suit;

   /**
   * String value that holds the rank of the card
   */
   private String rank;

   /**
   * int value that holds the point value.
   */
   private int pointValue;


   /**
   * Creates a new <code>Card</code> instance.
   *
   * @param cardRank a <code>String</code> value
   *                  containing the rank of the card
   * @param cardSuit a <code>String</code> value
   *                  containing the suit of the card
   * @param cardPointValue an <code>int</code> value
   *                  containing the point value of the card
   */
   public Card(String cardRank, String cardSuit, int cardPointValue) {
       //initializes a new Card with the given rank, suit, and point value
       rank = cardRank;
       suit = cardSuit;
       pointValue = cardPointValue;
   }


   /**
   * Accesses this <code>Card's</code> suit.
   * @return this <code>Card's</code> suit.
   */
   public String suit() {
       return suit;
   }

   /**
   * Accesses this <code>Card's</code> rank.
   * @return this <code>Card's</code> rank.
   */
   public String rank() {
       return rank;
   }

   /**
   * Accesses this <code>Card's</code> point value.
   * @return this <code>Card's</code> point value.
   */
   public int pointValue() {
       return pointValue;
   }

   /** Compare this card with the argument.
   * @param otherCard the other card to compare to this
   * @return true if the rank, suit, and point value of this card
   *              are equal to those of the argument;
   *         false otherwise.
   */
   public boolean matches(Card otherCard) {
       return otherCard.suit().equals(this.suit())
           && otherCard.rank().equals(this.rank())
           && otherCard.pointValue() == this.pointValue();
   }

   /**
   * Converts the rank, suit, and point value into a string in the format
   *     "[Rank] of [Suit] (point value = [PointValue])".
   * This provides a useful way of printing the contents
   * of a <code>Deck</code> in an easily readable format or performing
   * other similar functions.
   *
   * @return a <code>String</code> containing the rank, suit,
   *         and point value of the card.
   */
   @Override
   public String toString() {
       return rank + " of " + suit + " (point value = " + pointValue + ")";
   }
}

/***************************** 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) {
       String[] rank1 = {"Jack", "Queen", "King"};
       String[] suit1 = {"Hearts", "Spades", "Diamonds"};
       int[] value1 = {11, 12, 13};
        Deck deck1 = new Deck(rank1, suit1, value1);
        System.out.println("Deck1 size should be 3: " + deck1.size());
      
        String[] rank2 = {"10", "Queen", "King"};
        String[] suit2 = {"Clubs", "Spades", "Diamonds"};
        int[] value2 = {10, 12, 13};
        Deck deck2 = new Deck(rank2, suit2, value2);
        System.out.println("Deck2 should not be empty: " + !deck2.isEmpty());
      
        String[] rank3 = {"1", "3", "Queen"};
        String[] suit3 = {"Diamonds", "Spades", "Hearts"};
        int[] value3 = {1, 3, 12};
        Deck deck3 = new Deck(rank3, suit3, value3);
        System.out.println("Deck3 dealt card is: " + deck3.deal());
     
   }
}