Java Reference
In-Depth Information
27
return face;
28
}
29
30
// return suit of Card
31
public Suit getSuit()
32
{
33
return suit;
34
}
35
36
// return String representation of Card
37
public String toString()
38
{
39
return String.format( "%s of %s" , face, suit);
40
}
41
} // end class Card
42
43
// class DeckOfCards declaration
44
public class DeckOfCards
45
{
46
private List<Card> list; // declare List that will store Cards
47
48
// set up deck of Cards and shuffle
49
public DeckOfCards()
50
{
51
Card[] deck = new Card[ 52 ];
52
int count = 0 ; // number of cards
53
54
// populate deck with Card objects
55
for (
Card.Suit suit
: Card.Suit.values())
56
{
57
for (
Card.Face face
: Card.Face.values())
58
{
59
deck[count] = new Card(face, suit);
60
++count;
61
}
62
}
63
64
list = Arrays.asList(deck); // get List
Collections.shuffle(list); // shuffle deck
65
66
} // end DeckOfCards constructor
67
68
// output deck
69
public void printCards()
70
{
71
// display 52 cards in two columns
72
for ( int i = 0 ; i < list.size(); i++)
73
System.out.printf( "%-19s%s" , list.get(i),
74
((i + 1 ) % 4 == 0 ) ? "%n" : "" );
75
}
76
77
public static void main(String[] args)
78
{
Fig. 16.10 | Card shuffling and dealing with Collections method shuffle . (Part 2 of 3.)
Search WWH ::




Custom Search