Java Reference
In-Depth Information
79
DeckOfCards cards = new DeckOfCards();
80
cards.printCards();
81
}
82
} // end class DeckOfCards
Deuce of Clubs Six of Spades Nine of Diamonds Ten of Hearts
Three of Diamonds Five of Clubs Deuce of Diamonds Seven of Clubs
Three of Spades Six of Diamonds King of Clubs Jack of Hearts
Ten of Spades King of Diamonds Eight of Spades Six of Hearts
Nine of Clubs Ten of Diamonds Eight of Diamonds Eight of Hearts
Ten of Clubs Five of Hearts Ace of Clubs Deuce of Hearts
Queen of Diamonds Ace of Diamonds Four of Clubs Nine of Hearts
Ace of Spades Deuce of Spades Ace of Hearts Jack of Diamonds
Seven of Diamonds Three of Hearts Four of Spades Four of Diamonds
Seven of Spades King of Hearts Seven of Hearts Five of Diamonds
Eight of Clubs Three of Clubs Queen of Clubs Queen of Spades
Six of Clubs Nine of Spades Four of Hearts Jack of Clubs
Five of Spades King of Spades Jack of Spades Queen of Hearts
Fig. 16.10 | Card shuffling and dealing with Collections method shuffle . (Part 3 of 3.)
Class Card (lines 8-41) represents a card in a deck of cards. Each Card has a face and
a suit. Lines 10-12 declare two enum types— Face and Suit —which represent the face and
the suit of the card, respectively. Method toString (lines 37-40) returns a String con-
taining the face and suit of the Card separated by the string "of" . When an enum con-
stant is converted to a String , the constant's identifier is used as the String
representation. Normally we would use all uppercase letters for enum constants. In this
example, we chose to use capital letters for only the first letter of each enum constant
because we want the card to be displayed with initial capital letters for the face and the suit
(e.g., "Ace of Spades" ).
Lines 55-62 populate the deck array with cards that have unique face and suit com-
binations. Both Face and Suit are public static enum types of class Card . To use these
enum types outside of class Card , you must qualify each enum 's type name with the name
of the class in which it resides (i.e., Card ) and a dot ( . ) separator. Hence, lines 55 and 57
use Card.Suit and Card.Face to declare the control variables of the for statements. Recall
that method values of an enum type returns an array that contains all the constants of the
enum type. Lines 55-62 use enhanced for statements to construct 52 new Card s.
The shuffling occurs in line 65, which calls static method shuffle of class
Collections to shuffle the elements of the array. Method shuffle requires a List argu-
ment, so we must obtain a List view of the array before we can shuffle it. Line 64 invokes
static method asList of class Arrays to get a List view of the deck array.
Method printCards (lines 69-75) displays the deck of cards in four columns. In each
iteration of the loop, lines 73-74 output a card left justified in a 19-character field followed
by either a newline or an empty string based on the number of cards output so far. If the
number of cards is divisible by 4, a newline is output; otherwise, the empty string is output.
16.7.3 Methods reverse , fill , copy , max and min
Class Collections provides methods for reversing , filling and copying List s. Collections
method reverse reverses the order of the elements in a List , and method fill overwrites
 
 
Search WWH ::




Custom Search