You are to write a program name Deck.java that creates a deck of cards. A deck o
ID: 3661802 • Letter: Y
Question
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 OWN RANDOM SHUFFLE ROUTINE TO
SHUFFLE THE DECK OF CARDS.
Explanation / Answer
Step1:
Create the Card class to create each card.
Code:
package Cryptography;
public class Cards
{
public final static int Spade = 0;
public final static int Heart = 1;
public final static int Diamond = 2;
public final static int Club = 3;
public final static int Ace = 1;
public final static int Jack = 11;
public final static int Queen = 12;
public final static int King = 13;
private final int suit;
private final int val;
public Cards(int val, int suit)
{
if (suit != Spade && suit != Heart && suit != Diamond && suit != Club)
{
throw new IllegalArgumentException("Please define appropriate suit");
}
if (val< 1 || val > 13)
{
throw new IllegalArgumentException("Enter correct card value");
}
this.val = val;
this.suit = suit;
}
public int getSuit()
{
return suit;
}
public int getValue()
{
return val;
}
public String getSuitS()
{
switch ( suit )
{
case Spade: return "Spades";
case Heart: return "Hearts";
case Diamond: return "Diamonds";
default: return "Clubs";
}
}
public String getValueS()
{
switch ( val)
{
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
default: return "King";
}
}
public String toString()
{
return getValueS() + " of " + getSuitS();
}
}
Step2:
Place the cards in a vector and sort the Vector.
Code:
package Cryptography;
import java.util.*;
public class Deck
{
public static void main(String[] args)
{
Vector deck_of_cards = new Vector();
for(int i=1;i<=13;i++)
{
for(int j=0;j<=3;j++)
{
deck_of_cards.addElement(new Cards(i,j));
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.