Shuffling and Dealing Cards

Figure E.10 demonstrates class DeckOfCards (Fig. E.9). Line 9 creates a DeckOfCards object named myDeckOfCards. The DeckOfCards constructor creates the deck with the 52 Card objects in order by suit and face. Line 10 invokes myDeckOfCards’s shuffle method to rearrange the Card objects. Lines 13–20 deal all 52 Cards and print them in four columns of 13 Cards each. Line 16 deals one Card object by invoking myDeckOfCards’s dealCard method, then displays the Card left justified in a field of 19 characters. When a Card is output as a String, the Card’s toString method (lines 17–20 of Fig. E.8) is implicitly invoked. Lines 18–19 (Fig. E.10) start a new line after every four Cards.

 1   // Fig. ...

Get Android™ How to Program, Second 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.