DeckOfCards Constructor
The class’s constructor instantiates the deck
array (line 20) with NUMBER_OF_CARDS
(52) elements. The elements of deck
are null
by default, so the constructor uses a for
statement (lines 24–26) to fill the deck
with Card
s. The loop initializes control variable count
to 0
and loops while count
is less than deck.length
, causing count
to take on each integer value from 0 to 51 (the indices of the deck
array). Each Card
is instantiated and initialized with two String
s—one from the faces
array (which contains the String
s "Ace"
through "King"
) and one from the suits
array (which contains the String
s "Hearts"
, "Diamonds"
, "Clubs"
and "Spades"
). The calculation count % 13
always results in a value from 0 to 12 (the 13 indices ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.