Create a Deck class to represent a standard deck of 52 playing cards (with no jo
ID: 3640587 • Letter: C
Question
Create a Deck class to represent a standard deck of 52 playing cards (with nojokers).
(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.
(c) A Deck should also contain a draw() method, which returns the next card in the
deck. For simplicity, you may want to simply remove the first Card from the
ArrayList, and return it as the drawn card. This means that the size of the
ArrayList will shrink as you draw more cards, and you will need to recreate the
deck before you use it again. Alternately, you may simply maintain a variable that
stores the index of the next Card to be "drawn" from the ArrayList.
(d) Your Deck class should also include a shuffle() method. This method should
change the order of the Cards in your ArrayList. The simplest way to do this
is to remove randomly-selected cards from the deck, one at a time, and replace
them at the end of the deck. For example, you might remove the fourth card in
the deck and place it in the last (52nd) position. By repeating this a number of
times (say, 20-30), you can be assured that the deck will be somewhat
randomized. To generate random integers, use the Random class in the
java.util package; see the slides for examples of how to use Random.
(e) Your Deck class should also include a toString() method that returns a
String containing the current contents of the deck, in order.
Explanation / Answer
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();
}
} public class Card {
private int value;
private String suit;
Card (int value,String suit) {
this.suit = suit;
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String toString() {
String output = null;
if(this.value > 10) {
if (this.value==11) output="J";
if (this.value==12) output="Q";
if (this.value==13) output="K";
if (this.value==14) output="A";
} else {
output = ""+this.value;
}
if(this.suit!=null) {
char[] id = suit.toCharArray();
output = ""+output+ id[0];
}
return output;
}
}
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();
}
}