Question

In: Computer Science

Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application...

Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application will be able to create a deck of cards, shuffle it, sort it and print it.

Use the following class diagrams to code your application.

Use the following code for the PlayingCards class

public class PlayingCards{

public static void main(String[] args){

                System.out.println("Playings Cards");

                Deck deck = new Deck();

                deck.create();

                System.out.println("Sorted cards");

                deck.sort();

                deck.showCards();

                System.out.println("Shuffled cards");

                deck.shuffle();

                deck.showCards();

}

}

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


public class Card {

    private String suit;
    private String face;

    public Card(String suit, String face) {
        this.suit = suit;
        this.face = face;
    }

    public String getSuit() {
        return suit;
    }

    public String getFace() {
        return face;
    }


    @Override
    public String toString() {
        return getFace() + " of " + getSuit();
    }

}

================================================================

public abstract class Cards {

    public abstract Card[] getCards();
}

================================================================

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class Deck extends Cards {

    private Card[] cardDeck;

    public Deck() {
        cardDeck = new Card[52];
    }

    public void create() {

        String suits[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
        String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        int index = 0;
        for (String suit : suits) {
            for (String value : values) {
                cardDeck[index++] = new Card(suit, value);
            }
        }
    }

    @Override
    public Card[] getCards() {

        return cardDeck;

    }

    public void sort() {

        Arrays.sort(cardDeck, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if(o1.getSuit().compareTo(o2.getSuit())!=0){
                    return o1.getSuit().compareTo(o2.getSuit());
                }else{
                    return o1.getFace().compareTo(o2.getFace());
                }
            }
        });

    }

    public void showCards() {
        for (Card card : cardDeck) {
            System.out.println(card);
        }
    }

    public void shuffle() {
        // shuffle the deck 1000 times

        Random random = new Random();
        for (int count = 1; count <= 1000; count++) {
            int firstCardIndex = random.nextInt(cardDeck.length);
            int secondCardIndex = random.nextInt(cardDeck.length);
            // swap the cards at these two indices
            Card tempCard = cardDeck[firstCardIndex];
            cardDeck[firstCardIndex] = cardDeck[secondCardIndex];
            cardDeck[secondCardIndex] = tempCard;
        }
    }
}

================================================================

class  PlayingCards{

    public static void main(String[] args) {

        System.out.println("Playing Cards");
        Deck deck = new Deck();
        deck.create();

        System.out.println("Sorted Cards:");
        deck.sort();
        deck.showCards();

        System.out.println("Shuffled Cards:");
        deck.shuffle();
        deck.showCards();

    }
}

================================================================


Related Solutions

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.
4. The experiment is to draw a card from a standard deck of cards. Consider the...
4. The experiment is to draw a card from a standard deck of cards. Consider the following events: Q = drawing a Queen R = drawing a red card a. Are Q and R mutually exclusive? Clearly explain. b. Find P(Q) and P(Q|R). c. Are the events Q and R independent? Clearly explain.
In poker, there is a 52 card deck with 4 cards each of each of 13...
In poker, there is a 52 card deck with 4 cards each of each of 13 face values. A full house is a hand of 5 cards with 3 of one face value, and 2 of another. What is the probability that a random poker hand is a full house? You can leave your answer in terms of bionomial co-efficients and similar factors, but please explain each term.
. If 2 cards are selected from a standard deck of cards. The first card is...
. If 2 cards are selected from a standard deck of cards. The first card is not replaced in the deck before the second card is drawn. Find the following probabilities: a) P(2 Aces) b) P(Queen of hearts and a King) c) P(Q of Hearts and Q of hearts )
2.Deck of cards(52 total cards; 13 denominations in each of 4 suits). Select a single card...
2.Deck of cards(52 total cards; 13 denominations in each of 4 suits). Select a single card at random from a deck of cards: a.Whatis the probability of selecting the king of hearts? b.What is the probability of selecting a king? c.What is the probability of selecting a heart? d.What is the probability of selecting a king or a heart?
A special deck of cards has 5 red cards, and 4 purple cards. The red cards...
A special deck of cards has 5 red cards, and 4 purple cards. The red cards are numbered 1, 2, 3, 4, and 5. The purple cards are numbered 1, 2, 3, and 4. The cards are well shuffled and you randomly draw one card. R = card drawn is red E = card drawn is even-numbered a. How many elements are there in the sample space? b. P(E) = Round to 4 decimal places.
) The popular card game UNO has a deck of 108 cards. Four are ‘Wild’, and...
) The popular card game UNO has a deck of 108 cards. Four are ‘Wild’, and four are ‘Wild Draw Four’. The remaining cards are grouped into four suits — Blue, Green, Red, and Yellow — with 25 cards per suit. The cards within each suit are a single zero card, two each of the cards one through nine, two ‘Draw Two’ cards, two ‘Skip’ cards, and two ‘Reverse’ cards. (a) If you draw one card at random from a...
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...
Python Programming Write a simple implementation of a card deck to deal cards out randomly. Your...
Python Programming Write a simple implementation of a card deck to deal cards out randomly. Your Deck class will contain a list of card objects. Initially, the deck will contain one instance of each of the 52 possible cards. Your deck should implement a deal() method that chooses a random location from the list and "pops" that card. You should also implement a cardsLeft method that tells how many cards are left in the deck. Please type code and include...
How many different 4 card hands can be dealt from a deck of 52 cards? The...
How many different 4 card hands can be dealt from a deck of 52 cards? The order of the cards does not matter in this case.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT