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

Create a Deck class to represent a standard deck of 52 playing cards (with no jo

ID: 3640545 • Letter: C

Question

Create a Deck class to represent a standard deck of 52 playing cards (with no
jokers).
(a) This class should contain an ArrayList (whose capacity can be initialized to
52).
(b) The constructor for this class should fill the ArrayList with 52 Card objects
(the deck may start with the cards in any order; use nested loops to simplify and
“automate” the Card-creation process).
HINT: you will need to repeat this ArrayList-filling process later for your
shuffle() method. Consider creating a private helper method to fill the
ArrayList, and calling it from within your constructor.

Explanation / Answer

DONT FORGET TO RATE LIFESAVER!!


import java.util.ArrayList;

public class Deck {

    private ArrayList capacity = new ArrayList(52);

    Deck() {
        this.capacity = generateDeck();
    }

    private ArrayList generateDeck() {
        ArrayList deckCards = new ArrayList(52);
        Card card = null;

        for (int i = 2; i < 15; i++) {
            card = new Card(i, "Hearts");
            deckCards.add(card);
            System.out.print(card.toString() + ", ");
        }
        System.out.println();
        for (int i = 2; i < 15; i++) {
            card = new Card(i, "Clubs");
            deckCards.add(card);
            System.out.print(card.toString() + ", ");
        }
        System.out.println();
        for (int i = 2; i < 15; i++) {
            card = new Card(i, "Diamonds");
            deckCards.add(card);
            System.out.print(card.toString() + ", ");
        }
        System.out.println();
        for (int i = 2; i < 15; i++) {
            card = new Card(i, "Spade");
            deckCards.add(card);
            System.out.print(card.toString() + ", ");
        }
        System.out.println();

        return deckCards;
    }

    public static void main(String[] args) {
        Deck deck = new Deck();
    }
}