< // Card class represents a playing card.
public class Card
{
private String face; // face of card ("Ace", "Deuce", ...)
private String suit; // suit of card ("Hearts", "Diamonds", ...)
// two-argument constructor initializes card's face and suit
public Card( String cardFace, String cardSuit )
{
face = cardFace; // initialize face of card
suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor
// return String representation of Card
public String toString()
{
return face + " of " + suit;
} // end method toString
} // end class Card
/***********************************************************************************************************************/
// Fig. 7.10: DeckOfCards.java
// DeckOfCards class represents a deck of playing cards.
import java.util.Random;
public class DeckOfCards
{
private Card[] deck; // array of Card objects
private int currentCard; // index of next Card to be dealt
private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
// random number generator
private static final Random randomNumbers = new Random();
// constructor fills deck of Cards
public DeckOfCards()
{
String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };
deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] =
new Card( faces[ count % 13 ], suits[ count / 13 ] );
} // end DeckOfCards constructor
// shuffle deck of Cards with one-pass algorithm
public void shuffle()
{
// after shuffling, dealing should start at deck[ 0 ] again
currentCard = 0; // reinitialize currentCard
// for each Card, pick another random Card and swap them
for ( int first = 0; first < deck.length; first++ )
{
// select a random number between 0 and 51
int second = randomNumbers.nextInt( NUMBER_OF_CARDS );
// swap current Card with randomly selected Card
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
} // end for
} // end method shuffle
// deal one Card
public Card dealCard()
{
// determine whether Cards remain to be dealt
if ( currentCard < deck.length )
return deck[ currentCard++ ]; // return current Card in array
else
return null; // return null to indicate that all Cards were dealt
} // end method dealCard
} // end class DeckOfCards
/********************************************************************************************************/
// Fig. 7.11: DeckOfCardsTest.java
// Card shuffling and dealing.
public class DeckOfCardsTest
{
// execute application
public static void main( String[] args )
{
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order
// print all 52 Cards in the order in which they are dealt
for ( int i = 1; i <= 5; i++ )
{
// deal and display a Card
System.out.printf( "%-19s", myDeckOfCards.dealCard() );
if ( i % 1 == 0 ) // output newline every 4 cards
System.out.println();
} // end for
} // end main
} // end class DeckOfCardsTest
/**************************************************************************
/***********the output are as follows****/
so if one can be able to show me the method to find only question a,then i can at least try to find the rest
Ace of Spades
Five of Hearts
Six of Diamonds
Nine of Hearts
Three of Diamonds
Process completed.>