Question

In: Computer Science

Write a class whose instances represent a single playing card from a deck of cards. Playing...

Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Use an enum type to represent rank and another enum type to represent suit. Write another class whose instances represent a full deck of cards. Both these classes should have toString methods. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards by calling its toString method.

(You might need to create the following files: Suit.java for enum type, Rank.java for enum type, Card.java, Deck.java and DeckDemo.java)

Solutions

Expert Solution

Code Image:

Sample Output:

Code to Copy:

Suit.java:

//Suit.java

//Declare Suit as enum type

public enum Suit {

       //SPADE,CLUB,DIAMOND,HEART as type of

       //enum variables

    SPADE, CLUB, DIAMOND, HEART

}

Rank.java:

//Rank.java

//Declare Rank as type of enum

public enum Rank {

       //Decalre ONE, TWO, THREE, FOUR, FIVE, SIX,

       //SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE

       //as type of enum variables

    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE

}

Card.java:

//Card.java

//Implementation of Card class

public class Card {

       //Declae suit as type of Suit

    private Suit suit;

    //Declare rank as type of Rank

    private Rank rank;

    //Implementation of parameterized constructor

    public Card(Suit s, Rank r) {

       //assign s to this.suit

        this.suit = s;

        //assign r to this.rank

        this.rank = r;

    }

    //Implementation of getRank function

    public Rank getRank() {

        return rank;

    }

    //Implementation of getSuit function

    public Suit getSuit() {

        return suit;

    }

    //Implementation of toString function

    public String toString()

    {

       //Declare str as type of String

       String str = rank +" of "+suit;

       //return str

        return str ;

    }

}

Deck.java:

//Deck.java

import java.util.ArrayList;

//Implementation of Deck class

public class Deck {

       //Declare listOfCards as type of ArrayList<Card>

    private ArrayList<Card> listOfCards;

    //and assign 0

    private int cardCount = 0;

    //Implementation of Default constructor

    public Deck() {

       listOfCards = new ArrayList<>();

       //Iterate the loop

        for (Suit each : Suit.values())

        {

             //Iterate the loop

            for (Rank eachRank : Rank.values())

            {

            //Create Card class object through new

            Card requiredCard = new Card(each, eachRank);

            //call add function

            listOfCards.add(requiredCard);

            }

        }

        //Initilaize cardCount with 0

        cardCount = 0;

    }

    //Implementation of dealCards function

    public Card dealCards() {

       //increment cardCount by 1

       //call get functon

        return listOfCards.get(cardCount++);

    }

    //Implementation of containCard function

    public boolean containCard() {

       //return the resultant value of cardCount is

        return cardCount < listOfCards.size();

    }

    //Implementation of toString function

    public String toString() {

             return listOfCards.toString();

    }

}

DeckDemo.java:

//DeckDemo.java

//Implementation of DeckDemo class

public class DeckDemo {

       //Implementation of main function

    public static void main(String[] args)

    {

       //Declare an object deckDetails of Deck class

       //and initialize

        Deck deckDetails = new Deck();

        //Iterate the loop

        while (deckDetails.containCard()){

             //Display statement

            System.out.println(deckDetails.dealCards());

        }

    }

}


Related Solutions

(In C++) Write a class that will represent a card in a standard deck of playing...
(In C++) Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Write methods to • Initialize the deck of cards • Perform a perfect shuffle In a perfect shuffle, the deck is broken exactly in half and rearranged so that...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a three-of-a-kind hand if it contains exactly three cards of the same rank (e.g. 3 aces and 2 other cards). How many distinct three-of-a-kind hands can be dealt with? Calculate a numeric answer.
Cards: Suppose you draw one card from a single deck of cards. (a) What is the...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the probability that you draw an queen? Round your answer to 3 significant digits*. (b) What is the probability that you draw a heart? Round your answer to 3 significant digits*. (c) What is the probability that you draw the queen of hearts? Round your answer to 3 significant digits*. ............................................... *Significant Digits: Here are some probabilities expressed to 3 significant digits. You start counting...
USE C++ 1. Write a class that will represent a card in a standard deck of...
USE C++ 1. Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Note: Use enumeration instead of strings For example: Do something like this...        enum suits        {            CLUBS,            DIAMONDS,   ...
Your friend has chosen a card from a standard deck of 52 playing cards and no...
Your friend has chosen a card from a standard deck of 52 playing cards and no one knows the card except himself. Now you have to guess the unknown card. Before guessing the card, you can ask your friend exactly one question, the question must be either Q1, Q2 or Q3 below: Q1. whether the chosen card is an ace (A)? Q2. whether the chosen card is a spade (♠)? Q3. whether the chosen card is the ace of spades...
1. A card is drawn at random from an ordinary deck of 52 playing cards. Describe...
1. A card is drawn at random from an ordinary deck of 52 playing cards. Describe the sample space if consideration of suits (a) is not, (b) is, taken into account. 2. Answer for b: both a king and a club = king of club. 3. A fair die is tossed twice. Find the probability of getting a 4, 5, or 6 on the first toss and a 1, 2, 3, or 4 on the second toss. 4. Find the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the probability of each of the following events: a) The hand has at least one club b) The hand has at least two cards with the same rank c) The hand has exactly one club or exactly one spade d) The hand has at least one club or at least one spade
Your friend has chosen a card from a standard deck of 52 playing cards and no...
Your friend has chosen a card from a standard deck of 52 playing cards and no one knows the card except himself. Now you have to guess the unknown card. Before guessing the card, you can ask your friend exactly one question, the question must be either Q1, Q2 or Q3 below: Q1. whether the chosen card is a king (K)? Q2. whether the chosen card is a spade (♠)? Q3. whether the chosen card is the king of spades...
A single card is chosen at random from a deck of 52 cards, the probability that...
A single card is chosen at random from a deck of 52 cards, the probability that a club card is selected is 1/4. Does this probability mean that, if you choose a card at random 8 times, a club will appear twice? If not, what does it mean? Probability?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT