Source of DeckOfCards.java


  1: // Fig. 19.12: DeckOfCards.java
  2: // Using algorithm shuffle.
  3: import java.util.List;
  4: import java.util.Arrays;
  5: import java.util.Collections;
  6: 
  7: // class to represent a Card in a deck of cards
  8: class Card 
  9: {    
 10:    public static enum Face { Ace, Deuce, Three, Four, Five, Six,
 11:       Seven, Eight, Nine, Ten, Jack, Queen, King  };
 12:    public static enum Suit { Clubs, Diamonds, Hearts, Spades };
 13: 
 14:    private final Face face; // face of card
 15:    private final Suit suit; // suit of card
 16:    
 17:    // two-argument constructor
 18:    public Card( Face cardFace, Suit cardSuit ) 
 19:    {   
 20:        face = cardFace; // initialize face of card
 21:        suit = cardSuit; // initialize suit of card
 22:    } // end two-argument Card constructor
 23:    
 24:    // return face of the card
 25:    public Face getFace() 
 26:    { 
 27:       return face; 
 28:    } // end method getFace
 29: 
 30:    // return suit of Card
 31:    public Suit getSuit() 
 32:    { 
 33:       return suit; 
 34:    } // end method getSuit
 35: 
 36:    // return String representation of Card
 37:    public String toString()
 38:    {
 39:       return String.format( "%s of %s", face, suit );
 40:    } // end method toString
 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:          } // end for
 62:       } // end for
 63: 
 64:       list = Arrays.asList( deck ); // get List
 65:       Collections.shuffle( list );  // shuffle deck
 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( "%-20s%s", list.get( i ),
 74:             ( ( i + 1 ) % 2 == 0 ) ? "\n" : "" );
 75:    } // end method printCards
 76: 
 77:    public static void main( String args[] )
 78:    {
 79:       DeckOfCards cards = new DeckOfCards();
 80:       cards.printCards();
 81:    } // end main  
 82: } // end class DeckOfCards
 83: 
 84: /**************************************************************************
 85:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 86:  * Pearson Education, Inc. All Rights Reserved.                           *
 87:  *                                                                        *
 88:  * DISCLAIMER: The authors and publisher of this book have used their     *
 89:  * best efforts in preparing the book. These efforts include the          *
 90:  * development, research, and testing of the theories and programs        *
 91:  * to determine their effectiveness. The authors and publisher make       *
 92:  * no warranty of any kind, expressed or implied, with regard to these    *
 93:  * programs or to the documentation contained in these books. The authors *
 94:  * and publisher shall not be liable in any event for incidental or       *
 95:  * consequential damages in connection with, or arising out of, the       *
 96:  * furnishing, performance, or use of these programs.                     *
 97:  *************************************************************************/