Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a Driver class for running the Deck Class. The output of the Driver and C

ID: 3834486 • Letter: I

Question

I need a Driver class for running the Deck Class. The output of the Driver and Codes for the Deck Class are given.

Deck: A class that represents the source of the cards for dealing and, as the game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck" or when future hands are to be dealt from the same deck). Recall this picture, which relates the Deck to the various Hands that it creates through the process called "dealing":

Let's deconstruct the meaning of this important class.

y the operating system, is a resource we do not abuse).

Example Test Run of Card Class

//Deck Class:

// class Deck ----------------------------------------------------------------

// represents the source of the cards for dealing

class Deck {

       private static final int MAX_PACKS = 6;

       private static final int NUM_CARDS_PER_PACK = 52;

       private static final int MAX_CARDS_PER_DECK = MAX_PACKS * NUM_CARDS_PER_PACK;

       private static Card[] masterPack;

       private Card[] cards;

       private int topCard;

       private int numPacks;

       // constructor

       public Deck(int numPacks) {

             this.numPacks = numPacks;

             masterPack = new Card[NUM_CARDS_PER_PACK];

             cards = new Card[(NUM_CARDS_PER_PACK * numPacks)];

             topCard = 0;

             allocateMasterPack();

             init(numPacks);

       }

       public boolean init(int numPacks) {

             topCard = 0;

             if (numPacks > this.numPacks) {

                    return false;

             } else {

                    for (int i = 0; i < (NUM_CARDS_PER_PACK * numPacks); i++) {

                          int present = i % NUM_CARDS_PER_PACK;

                          cards[i] = masterPack[present];

                    }

             }

             return true;

       }

       // mixes up the cards with the help of the standard random number generator

       public void shuffle() {

             for (int i = 0; i < 500; i++) {

                    int rand1 = (int) (Math.random() * (NUM_CARDS_PER_PACK * numPacks));

                    int rand2 = (int) (Math.random() * (NUM_CARDS_PER_PACK * numPacks));

                    Card temp = new Card(cards[rand1]);

                    cards[rand1] = cards[rand2];

                    cards[rand2] = temp;

             }

       }

       // returns and removes the card in the top occupied position of cards[]

       public Card dealCard() {

             Card top = new Card(cards[topCard]);

             topCard++;

             return top;

       }

       // An accessor for the int

       public int topCard() {

             return topCard;

       }

       // Accessor for an individual card

       public Card inspectCard(int k) {

             if (k >= topCard && k < (NUM_CARDS_PER_PACK * numPacks)) {

                    Card kcard = new Card(cards[k]);

                    return kcard;

             } else {

                    // error flag = true

                    return new Card('E', Card.Suit.spades); // error card, in rare

                    // cases;

             }

       }

       // this is a method that will be called by the constructor

       private static void allocateMasterPack() {

             for (int i = 0; i < NUM_CARDS_PER_PACK; i++) {

                    char val = 'A';

                    if (i % 13 == 0) {

                          val = 'A';

                    } else if ((i % 13) >= 1 && (i % 13) <= 8) {

                          String temp = ((i % 13) + 1) + "";

                          val = temp.charAt(0);

                    } else if ((i % 13) == 9) {

                          val = 'T';

                    } else if ((i % 13) == 10) {

                          val = 'J';

                    } else if ((i % 13) == 11) {

                          val = 'Q';

                    } else if ((i % 13) == 12) {

                          val = 'K';

                    }

                    if (i < 13) {

                          masterPack[i] = new Card(val, Card.Suit.spades);

                    } else if (i >= 13 && i < 26) {

                          masterPack[i] = new Card(val, Card.Suit.hearts);

                    } else if (i >= 26 && i < 39) {

                          masterPack[i] = new Card(val, Card.Suit.diamonds);

                    } else if (i >= 39 && i < 52) {

                          masterPack[i] = new Card(val, Card.Suit.clubs);

                    }

             }

       }

}/* ---------------------------------------------------------

A of spades / K of spades / Q of spades / J of spades / T of spades / 9 of
spades / 8 of spades / 7 of spades / 6 of spades / 5 of spades / 4 of spade
s / 3 of spades / 2 of spades / A of hearts / K of hearts / Q of hearts /
J of hearts / T 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 / A of diamo
nds / K of diamonds / Q of diamonds / J of diamonds / T 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 / A of clubs / K of clubs / Q
of clubs / J of clubs / T 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 /

K of spades / 9 of clubs / Q of hearts / T of diamonds / 9 of spades / 5 of hear
ts / 2 of clubs / A of hearts / J of hearts / K of clubs / T of hearts / 4 of sp
ades / 7 of hearts / 3 of spades / Q of spades / A of clubs / 8 of spades / 5 of
clubs / T of clubs / J of spades / 2 of spades / 6 of diamonds / 2 of diamonds
/ 8 of diamonds / 4 of hearts / 2 of hearts / 6 of hearts / 5 of spades / 8 of h
earts / 7 of diamonds / 7 of spades / Q of diamonds / K of hearts / J of diamond
s / T of spades / 8 of clubs / 3 of diamonds / J of clubs / 6 of spades / 4 of c
lubs / K of diamonds / 4 of diamonds / 3 of clubs / A of spades / 9 of hearts /
Q of clubs / A of diamonds / 6 of clubs / 5 of diamonds / 7 of clubs / 3 of hear
ts / 9 of diamonds /

A of spades / K of spades / Q of spades / J of spades / T of spades / 9 of
spades / 8 of spades / 7 of spades / 6 of spades / 5 of spades / 4 of spade
s / 3 of spades / 2 of spades / A of hearts / K of hearts / Q of hearts /
J of hearts / T 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 / A of diamo
nds / K of diamonds / Q of diamonds / J of diamonds / T 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 / A of clubs / K of clubs / Q
of clubs / J of clubs / T 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 / A of sp
ades / K of spades / Q of spades / J of spades / T 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 / A of hearts / K of hearts / Q of hearts / J of he
arts / T 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 / A of diamonds /
K of diamonds / Q of diamonds / J of diamonds / T of diamonds / 9 of diamond
s / 8 of diamonds / 7 of diamonds / 6 of diamonds / 5 of diamonds / 4 of di
amonds / 3 of diamonds / 2 of diamonds / A of clubs / K of clubs / Q of clu
bs / J of clubs / T 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 /

6 of diamonds / 7 of clubs / 9 of hearts / 4 of diamonds / 9 of diamonds /
J of hearts / 3 of spades / 2 of clubs / J of clubs / A of hearts / J of cl
ubs / Q of hearts / Q of diamonds / 7 of clubs / 2 of diamonds / 5 of clubs
/ K of spades / 5 of clubs / 5 of diamonds / 8 of hearts / 3 of clubs / 6
of hearts / A of spades / A of hearts / 4 of hearts / J of diamonds / A of
clubs / 3 of diamonds / A of diamonds / K of diamonds / 6 of clubs / 4 of
spades / 6 of hearts / 7 of spades / K of clubs / 3 of hearts / 3 of hearts
/ K of spades / Q of spades / J of spades / 7 of hearts / J of hearts / K
of hearts / T of spades / K of clubs / 8 of hearts / 2 of spades / A of di
amonds / 4 of diamonds / Q of clubs / T of diamonds / 7 of hearts / 9 of cl
ubs / T of clubs / 7 of spades / 6 of spades / 6 of spades / K of diamonds
/ Q of spades / 7 of diamonds / Q of hearts / J of spades / 3 of clubs / 5
of hearts / K of hearts / 9 of spades / 9 of clubs / 2 of clubs / 7 of dia
monds / 8 of diamonds / T of spades / 8 of clubs / 8 of diamonds / 8 of clu
bs / T of diamonds / 4 of spades / 9 of hearts / A of spades / 8 of spades
/ 4 of hearts / T of hearts / 5 of diamonds / T of hearts / 5 of spades /
9 of diamonds / 5 of hearts / T of clubs / Q of diamonds / 6 of diamonds /
3 of spades / J of diamonds / 8 of spades / Q of clubs / 2 of diamonds / 2
of hearts / 6 of clubs / 2 of hearts / 9 of spades / 4 of clubs / 2 of spad
es / 3 of diamonds / 5 of spades / 4 of clubs / A of clubs /
--------------------------------------------------------- */

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

hand

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Card {

enum Suit {
spades, hearts, diamonds, clubs
}
private char val;
private Suit suit;

public Card(Card card) {
this.val = card.val;
this.suit = card.suit;
}

public int getVal() {
return val;
}

public void setVal(char val) {
this.val = val;
}

public Suit getSuit() {
return suit;
}

public void setSuit(Suit suit) {
this.suit = suit;
}

public Card(char val, Suit suit) {
this.val = val;
this.suit = suit;
}
}

class Deck {

private static final int MAX_PACKS = 6;
private static final int NUM_CARDS_PER_PACK = 52;
private static final int MAX_CARDS_PER_DECK = MAX_PACKS * NUM_CARDS_PER_PACK;
private static Card[] masterPack;
private Card[] cards;
private int topCard;
private int numPacks;

// constructor
public Deck(int numPacks) {
this.numPacks = numPacks;
masterPack = new Card[NUM_CARDS_PER_PACK];
cards = new Card[(NUM_CARDS_PER_PACK * numPacks)];
topCard = 0;
allocateMasterPack();
init(numPacks);

}

public boolean init(int numPacks) {
topCard = 0;
if (numPacks > this.numPacks) {
return false;
} else {
for (int i = 0; i < (NUM_CARDS_PER_PACK * numPacks); i++) {
int present = i % NUM_CARDS_PER_PACK;
cards[i] = masterPack[present];
}
}
return true;
}

// mixes up the cards with the help of the standard random number generator
public void shuffle() {
for (int i = 0; i < 500; i++) {
int rand1 = (int) (Math.random() * (NUM_CARDS_PER_PACK * numPacks));
int rand2 = (int) (Math.random() * (NUM_CARDS_PER_PACK * numPacks));
Card temp = new Card(cards[rand1]);
cards[rand1] = cards[rand2];
cards[rand2] = temp;
}
}

// returns and removes the card in the top occupied position of cards[]
public Card dealCard() {
Card top = new Card(cards[topCard]);
topCard++;
return top;
}

// An accessor for the int
public int topCard() {
return topCard;
}

// Accessor for an individual card
public Card inspectCard(int k) {

if (k >= topCard && k < (NUM_CARDS_PER_PACK * numPacks)) {
Card kcard = new Card(cards[k]);
return kcard;
} else {
// error flag = true
return new Card('E', Card.Suit.spades); // error card, in rare
// cases;
}
}

// this is a method that will be called by the constructor
private static void allocateMasterPack() {
for (int i = 0; i < NUM_CARDS_PER_PACK; i++) {
char val = 'A';
if (i % 13 == 0) {
val = 'A';
} else if ((i % 13) >= 1 && (i % 13) <= 8) {
String temp = ((i % 13) + 1) + "";
val = temp.charAt(0);
} else if ((i % 13) == 9) {
val = 'T';
} else if ((i % 13) == 10) {
val = 'J';
} else if ((i % 13) == 11) {
val = 'Q';
} else if ((i % 13) == 12) {
val = 'K';
}
if (i < 13) {
masterPack[i] = new Card(val, Card.Suit.spades);
} else if (i >= 13 && i < 26) {
masterPack[i] = new Card(val, Card.Suit.hearts);
} else if (i >= 26 && i < 39) {
masterPack[i] = new Card(val, Card.Suit.diamonds);
} else if (i >= 39 && i < 52) {
masterPack[i] = new Card(val, Card.Suit.clubs);
}
}
}

}

public class Driver {

public static void main(String[] args) throws IOException {
int choice = 1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Card p1[], p2[], p3[], p4[];
int numPack;
do {
System.out.println("Enter the number of card pack");
numPack = Integer.parseInt(in.readLine());

Deck deck = new Deck(numPack);//initializing Deck Class

deck.shuffle(); //suffling the card in the deck

p1 = new Card[numPack * 13];
p2 = new Card[numPack * 13];
p3 = new Card[numPack * 13];
p4 = new Card[numPack * 13];

/*
We have multiplied by 13 because lets say if we have 1 deck of card and there are 4 players
then each player will get minimum 13 cards. If there are 2 deck of cards then each player will get 13*2
that is 26 cards.
*/
for (int i = 0; i < numPack * 13; i++) {//dealing cards to players where p1,p2,p3 and p4 are 4 players
p1[i] = new Card(deck.dealCard());
p2[i] = new Card(deck.dealCard());
p3[i] = new Card(deck.dealCard());
p4[i] = new Card(deck.dealCard());
}

//This will show the players card
showPlayersCard(p1);
System.out.println(" ");
showPlayersCard(p2);
System.out.println(" ");
showPlayersCard(p3);
System.out.println(" ");
showPlayersCard(p4);
System.out.println(" ");

System.out.println("Do you want to play again? Enter 1 for YES");
choice = Integer.parseInt(in.readLine());

} while (choice == 1);
}

private static void showPlayersCard(Card card[]) {
for (Card card1 : card) {
System.out.print((char) card1.getVal() + " of " + card1.getSuit() + " \ ");
}
}
}

_________________________________________________________________________________________

I have added two class Card and Driver.

This is the output i have got

Enter the number of card pack
1
4 of spades 3 of hearts J of hearts 5 of hearts 2 of spades 6 of spades A of clubs 2 of diamonds K of clubs Q of diamonds T of hearts 8 of hearts 9 of diamonds

4 of hearts A of hearts 6 of diamonds 3 of diamonds Q of hearts A of spades 7 of hearts K of spades 9 of hearts K of hearts 9 of clubs 5 of diamonds 5 of spades

7 of clubs 7 of diamonds T of spades J of clubs 3 of spades 3 of clubs K of diamonds J of spades T of clubs Q of spades 4 of diamonds 4 of clubs 8 of spades

6 of hearts 2 of clubs A of diamonds 8 of diamonds 9 of spades T of diamonds 5 of clubs 8 of clubs 2 of hearts 7 of spades J of diamonds 6 of clubs Q of clubs

Do you want to play again?
Enter 1 for YES
2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote