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

Using Java, define a class for representing playing cards in a standard 52-card

ID: 652310 • Letter: U

Question

Using Java, define a class for representing playing cards in a standard 52-card deck. It should contain information about the card's suit and its rank. It must include the following methods:

One or more constructors, which allow you to build a particular card given a particular suit and rank.

.toString() which represents the card as a string. It must contain information about the rank and suit.

.equals() which determines if two cards are equal by checking to make sure that they have the same rank and suit.

Explanation / Answer

A Card may be a not a Deck of Cards. A Card has attributes (i.e. member variables) like suit and rank (although for scrutiny it's sometimes helpful to use integers - or enums - for the rank and so amendment however they're displayed).

A Deck of Cards is several cards; sometimes with restrictions established once the deck is made. during this case the suits[] and ranks[] will be accustomed build the various Cards within the deck (giving every Card a specific suit and rank once it's created). the simplest thanks to try this in Java is to use nested loops - once for every dimension.

The "color" of every card springs from the suit and doesn't ought to be hold on because it will be determined given a suit. If enums square measure used this may be simply allotted as associate degree attribute.

Going by the on top of logic, here may be a sample set of classes:

class Card {

   final public String suit;

   final public String rank;

   public Card(String suit, String rank) {

      // Assign suit/rank from arguments

   }

   String getColor() {

      // Return the color based on suit (effectively "for free" if

      // using an enumeration)

   }

}

class DeckOfCards {

    String[] suits = {..};

    String[] ranks = {..};

   // Arrays are icky to deal with; favor Lists/Collections.

    List<Card> cards = new ArrayList<Card>();

    public DeckOfCards () {

       // For each suit/rank pair, create a Card and add it

       // to the cards collection.

       // You'll want nested loops, as shown by another answer.

    }

    public List<Card> getCards () {

        // Return cards, perhaps

    }

    // Other methods relating to the Deck of Cards

    public void shuffle () {

    }

While i might suggest wanting into enums, the on top of ought to indicate the distinction between the 2 distinct "things" - Cards and a Deck (or Collection) of Cards. the opposite answer contains code showing the way to generate cards from the vector product of the 2 input arrays.

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