You are to write a program name Deckjava that creates a deck

You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of
52 cards. Each card is made up of a suit and a value (or name). There are, therefore, 4 suits (clubs, heart,
spades and diamonds) with each suit made up of 13 cards (2 through 10 and Jack, Queen, King, and Ace note).
NOTE: 1 of Clubs will actually be Ace of Clubs, and so on. You would, therefore, need to write a class
name card.java which will let you create each card.
Place the deck of cards in a vector in sorted order with all 13 hearts first, followed by the 13 clubs, then
the 13 spades and finally the 13 diamonds.
Display the contents of the vector on the screen example:
Ace of Hearts
King of Hearts
Queen of Hearts
Jack of Hearts
10 of Hearts
9 of Hearts
8 of Hearts
7 of Hearts
6 of Hearts
5 of Hearts
4 of Hearts
3 of Hearts
2 of Hearts
Now take this deck of cards and randomly shuffle it (for at least 20 times) and display the randomly
shuffled deck of cards, i.e. you must write your random shuffle routine to shuffle the deck of cards

Solution

//Card.java
public class Card
{
   private final String face;
   private final String suit;

   // Constructor that takes face and suit string
   public Card(String face, String suit)
   {
      this.face = face;
      this.suit = suit;
   }

   // Returns String representation of Card object
   public String toString()
   {
      return face + \" of \" + suit;
   }
} // end class Card


----------------------------------------------------------------------------------------------------------------------------------------------------------------

///DeckOfCards.java
import java.util.Random;
public class Deck
{
   //Card array
   private Card[] deck;
   // number of cards in deck
   private static final int SIZE_OF_DECK = 52;
   // random number generator
   private Random rand = new Random();

   // constructor initializes the deck array of type Card
   public Deck()
   {
       String[] faces = {\"Ace\", \"King\",\"Queen\", \"Jack\",
               \"10\",\"9\",\"8\",\"7\",\"6\",\"5\",\"4\",\"3\",\"2\"};
       String[] suits = {\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"};

         // create array of Card

       deck = new Card[SIZE_OF_DECK];
       //fill the deck of cards
       for (int count = 0; count < deck.length; count++)
           deck[count] =
           new Card(faces[count % 13], suits[count / 13]);
   }

   // shuffle the deck cards
   public void shuffle()
   {
       // replace card with random index
       for (int index = 0; index < deck.length; index++)
       {
           // select a random number between 0 and 51
           int randIndex = rand.nextInt(SIZE_OF_DECK);
           // swap temp with rand index
           Card temp = deck[index];      
           deck[index] = deck[randIndex];
           deck[randIndex] = temp;          
       }
   }

   /*Override the toString method that returns
   52 deck of cards */
   @Override
   public String toString() {
       String str=\"\";
       for (Card card : deck)
       {
           str+=card+\"\ \";
       }
       return str;
   }
} // end class DeckOfCards

----------------------------------------------------------------------------------------------------------------------------------------------------------------


//CardsDemo.java
public class CardsDemo {
   public static void main(String[] args) {

       //set constant number of shuffles
       final int NUMBER_OF_SHUFFLES=20;
       //Create an instance of Deck class
       Deck deck=new Deck();
       System.out.println(\"--------------------\");
       System.out.println(\"***Deck of Cards***\");
       System.out.println(\"--------------------\");
       //print the deck of cards
       System.out.println(deck);
      
       //call shuffle on deck for 20 times      
       for (int i = 0; i < NUMBER_OF_SHUFFLES; i++)       
               deck.shuffle();
      
      
       System.out.println(\"--------------------\");
       System.out.println(\"***After shuffle***\");
       System.out.println(\"--------------------\");
       //print shuffled cards
           System.out.println(deck);      
   }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

--------------------
***Deck of Cards***
--------------------
Ace of Hearts
King of Hearts
Queen of Hearts
Jack of Hearts
10 of Hearts
9 of Hearts
8 of Hearts
7 of Hearts
6 of Hearts
5 of Hearts
4 of Hearts
3 of Hearts
2 of Hearts
Ace of Diamonds
King of Diamonds
Queen of Diamonds
Jack of Diamonds
10 of Diamonds
9 of Diamonds
8 of Diamonds
7 of Diamonds
6 of Diamonds
5 of Diamonds
4 of Diamonds
3 of Diamonds
2 of Diamonds
Ace of Clubs
King of Clubs
Queen of Clubs
Jack of Clubs
10 of Clubs
9 of Clubs
8 of Clubs
7 of Clubs
6 of Clubs
5 of Clubs
4 of Clubs
3 of Clubs
2 of Clubs
Ace of Spades
King of Spades
Queen of Spades
Jack of Spades
10 of Spades
9 of Spades
8 of Spades
7 of Spades
6 of Spades
5 of Spades
4 of Spades
3 of Spades
2 of Spades

--------------------
***After shuffle***
--------------------
4 of Spades
8 of Hearts
King of Hearts
7 of Diamonds
7 of Clubs
9 of Hearts
Jack of Spades
Queen of Hearts
3 of Hearts
2 of Spades
8 of Diamonds
8 of Clubs
3 of Diamonds
6 of Diamonds
Ace of Spades
Ace of Hearts
Queen of Diamonds
Ace of Clubs
Queen of Clubs
Jack of Hearts
5 of Hearts
4 of Diamonds
6 of Clubs
9 of Spades
6 of Hearts
5 of Diamonds
King of Spades
2 of Hearts
10 of Clubs
7 of Spades
King of Clubs
King of Diamonds
5 of Clubs
4 of Hearts
Jack of Diamonds
3 of Clubs
5 of Spades
7 of Hearts
2 of Clubs
3 of Spades
Ace of Diamonds
6 of Spades
9 of Diamonds
2 of Diamonds
4 of Clubs
8 of Spades
9 of Clubs
Queen of Spades
10 of Spades
Jack of Clubs
10 of Hearts
10 of Diamonds

You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of 52 cards. Each card is made up of a suit and a value (or n
You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of 52 cards. Each card is made up of a suit and a value (or n
You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of 52 cards. Each card is made up of a suit and a value (or n
You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of 52 cards. Each card is made up of a suit and a value (or n
You are to write a program name Deck.java that creates a deck of cards. A deck of cards is made up of 52 cards. Each card is made up of a suit and a value (or n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site