you created a Card class that represents a standard playing card. Use this to design and implement a class called DeckOfCards that stores 52 objects of the Card class using an array. Include methods to shuffle the deck, deal a card, return the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck.Create a separate driver class that first outputs the populated deck to prove it is complete, shuffles the deck, and then deals each card from a shuffled deck, displaying each card as it is dealt along with the number of cards left in the deck.

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 2GZ
icon
Related questions
Question

you created a Card class that represents a standard playing card. Use this to design and implement a class called DeckOfCards that stores 52 objects of the Card class using an array. Include methods to shuffle the deck, deal a card, return the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck.Create a separate driver class that first outputs the populated deck to prove it is complete, shuffles the deck, and then deals each card from a shuffled deck, displaying each card as it is dealt along with the number of cards left in the deck.

This is my Card class:

mport java.util.Random;

/*Card.java              Richard Mino
 * This is a program demonstrating the use of random and predetermined objects and values in the form
 * of a playing card using getter/setter methods and two constructors and printing them out as a string
 * using the toString method */
public class Card {
    //Set up variables for card rank and suit
    private int ranks, suit;

    //Create random objects for card rank and suit
    //Set up initial card constructor for randomizer that takes no parameters
    public Card() {
        Random randRank = new Random();
        int randomRank = randRank.nextInt(13) + 1;
        Random randSuit = new Random();
        int randomSuit = randSuit.nextInt(4) + 1;
        ranks = randomRank;
        suit = randomSuit;
    }
    //Sets up card constructor for hard coded (predetermined) cards, takes in rank and suit parameters
    public Card(int cR, int cS) {
        ranks = cR;
        suit = cS;
    }
    //The following code returns a value that is set to a predetermined rank
    //and returns it as a string, and returns invalid if outside the parameter
    public static String setRank(int ranks) {
        if (ranks == 1) {
            return "Ace";
        }else if (ranks == 2) {
            return "Two";
        }else if (ranks == 3) {
            return ("Three");
        }else if (ranks == 4) {
            return "Four";
        }else if (ranks == 5) {
            return "Five";
        }else if (ranks == 6) {
            return "Six";
        }else if (ranks == 7) {
            return "Seven";
        }else if (ranks == 8) {
            return "Eight";
        }else if (ranks == 9) {
            return "Nine";
        }else if (ranks == 10) {
            return "Ten";
        }else if (ranks == 11) {
            return "Jack";
        }else if (ranks == 12) {
            return "Queen";
        }else if (ranks == 13) {
            return "King";
        }else {
            return "Invalid rank";
        }
    }
    //The following code gets and returns the set rank that gets invoked from the toString method
    public String getRank() {
        return setRank(ranks);
    }
    //The following code sets a value to a predetermined card suit
    //and returns it as a string, and returns invalid if outside the parameter 
    public static String setSuit(int suits) {
        if (suits == 1) {
            return "Spades";
        }else if (suits == 2) {
            return "Hearts";
        }else if (suits == 3) {
            return "Clubs";
        }else if (suits == 4) {
            return "Diamonds";
        }else {
            return "an Invalid suit";
        }
    }
    //The following code gets and returns the set suit that gets invoked from the toString method
    public String getSuit() {
        return setSuit(suit);
    }
    //The following code returns and prints out the entire values compiled as a readable string output
    public String toString() {
        return getRank() + " of " + getSuit();
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage