How do I start creating a program to shuffle and deal a deck of cards? The program should consist of class Card.The implementation code for the following class is: class DeckOfCards{public:   DeckOfCards(); // constructor initializes deck   void shuffle(); // shuffles cards in deck   Card dealCard(); // deals cards in deck   bool moreCards() const; // are there any more cards leftprivate:   std::vector< Card > deck; // represents deck of cards   unsigned currentCard; // index of next card to be dealt}; // end class DeckOfCardsClass Card should provide:a) Data members face and suit -- use enumerations to represent the faces and suits.b) A constructor that receives two enumeration constants representating the face and suit and uses them to initialize the data members.c) Two static arrays of strings representing the faces and suits.d) A toString function that returns the Card as a string in the form "face of suit." You can use the + operator to concatenate strings.

Question
Asked Oct 30, 2019
22 views

How do I start creating a program to shuffle and deal a deck of cards? 

The program should consist of class Card.

The implementation code for the following class is: 

class DeckOfCards
{
public:
   DeckOfCards(); // constructor initializes deck
   void shuffle(); // shuffles cards in deck
   Card dealCard(); // deals cards in deck
   bool moreCards() const; // are there any more cards left

private:
   std::vector< Card > deck; // represents deck of cards
   unsigned currentCard; // index of next card to be dealt
}; // end class DeckOfCards

Class Card should provide:

a) Data members face and suit -- use enumerations to represent the faces and suits.

b) A constructor that receives two enumeration constants representating the face and suit and uses them to initialize the data members.

c) Two static arrays of strings representing the faces and suits.

d) A toString function that returns the Card as a string in the form "face of suit." You can use the + operator to concatenate strings.

 

check_circle

Expert Answer

Step 1

Program:

Filename: Game.java

 

package deckOfCards;
import java.util.Scanner;

public class Game
{
public static void main(String[] ags)
{
DeckOfCards deck=new DeckOfCards();
Scanner sc=new Scanner(System.in);
int cardNum=0;
deck.shuffle();

System.out.println("Enter how many cards the user want to play?");
cardNum=sc.nextInt();

int count=1;

while(count<=cardNum && deck.moreCards())
{
Card c=deck.dealCard();
if(c!=null)
{
System.out.println(count+". "+c.toString());
count++;
}
else
{
break;
}
}
}
}

Step 2

Filename: DeckOfCards.java

 

package deckOfCards;
import java.util.Random;

public class DeckOfCards
{
private Card[] deck;
private int currentCard;
public DeckOfCards()
{
deck=new Card[52];
this.currentCard = 0;
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 4; j++)
{
this.deck[this.currentCard++] = new Card(i + 1, j + 1);
}
}
}
public void shuffle()
{
Card temp;
int a,b;
Random rand = new Random();

for(int i=1;i<10000;i++)
{
a = rand.nextInt(51);
b = rand.nextInt(51);
temp=this.deck[a];
this.deck[a]=this.deck[b];
this.deck[b]=temp;
}
}
public Card dealCard()
{
if(this.getNumOfCards()>0)
{
this.currentCard=this.getNumOfCards()-1;
return deck[this.currentCard];
}
else
return null;
}
public boolean moreCards()
{
if(this.getNumOfCards()>0)
return true;
else
return false;
}
public Card[] getCards()
{
return deck;
}
public void setCards(Card[] deck)
{
this.deck = deck;
}
public int getNumOfCards()
{
return currentCard;
}
public void setNumOfCards(int currentCard)
{
this.currentCard = currentCard;
}
}

Step 3

Filename: Card.java

 

package deckOfCards;

public class Card
{
private int face;
private String faceStr;
private int suit;
private String suitStr;
public Card()
{
this.face = 0;
this.suit = 0;
this.faceStr = "";
this.suitStr = "";
}
public Card(int face, int suit)
{
this.face = face;
this.suit = suit;
this.assignFace();
this.assignSuit();
}
private void assignFace()
{
switch (this.face)
{
case 1:
{
this.setFaceStr("ACE");
break;
}
case 2:
{
this.setFaceStr("TWO");
break;
}
case 3:
{
this.setFaceStr("THREE");
break;
}
case 4:
{
this.setFaceStr("FOUR");
break;
}
case 5:
{
this.setFaceStr("FIVE");
break;
}
case 6:
{
this.setFaceStr("SIX");
break;
}
case 7:
{
this.setFaceStr("SEVEN");
break;
}
case 8:
{
this.setFaceStr("EIGHT");
break;
}
case 9:
{
this.setFaceStr("NINE");
break;
}
case 10:
{
this.setFaceStr("TEN");
break;
}
case 11:
{
this.setFaceStr("JACK");
break;
}
case 12:
{
this.setFaceStr("QUEEN");
break;
}
case 13:
{
this.setFaceStr("KING");
break;
}
}
}
private void assignSuit()
{
switch (this.suit)
{
case 1:
{
this.setSuitStr("CLUBS");
break;
}
case 2:
{
this.setSuitStr("SPADES");
break;
}
case 3:
{
this.setSuitStr("HEARTS");
break;
}
case 4:
{
this.setSuitStr("DIAMONDS");
break;
}
}
}
public int getFace()
{
ret...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: What is single loss expectancy? What is annualized loss expectancy? What is wrong with these risk me...

A: Single loss expectancy is related to risk management and is considered as the value related to money...

question_answer

Q: Write a loop that reads positive integers from standard input and that terminates when it reads an i...

A: Following is the loop which prints the sum of all even and odd integers entered by the user and will...

question_answer

Q: This program is written in pseudocode."Design a program that asks the user to enter a store's sales ...

A: Program code #1:

question_answer

Q: Euler's method is a numerical method for generating a table of values (xi, yi) that approximatethe s...

A: Here we use following Variables, Functions, Data types and Header files to estimate the value of y w...

question_answer

Q: Siobhan’s laptop has 8GB of RAM. Her operating system takes up 2GB, and her application programs use...

A: Random Access Memory (RAM):RAM is a memory device used for storage purposes.The data stored in the m...

question_answer

Q: Discuss key areas of concern for risk management. How is risk management important in the developmen...

A: Risk management-Risk management is an identification, evaluation and prioritization of risks in orde...

question_answer

Q: Python 3.7.4 Write a statement that associates s with the empty set.

A: Set:A set is a list of unranked data items, in python data items can be added or removed in a set.Em...

question_answer

Q: 2. (5.12 in 8e) answer each of the followig question. a) what does it mean to choose numbers "at ran...

A:  a) Choosing numbers at random means that any number can be selected and every number has an equal c...

question_answer

Q: Write down two approaches to reduce re-work cost. Inyour opinion how incremental delivery supports b...

A: The two approaches that are used to reduce re-work cost are as follows:Change toleranceChange avoida...