Question: Create a DeckOfCards class Create a CardDealer class with a main method. DeckOfCards Specifications The DeckOfCards class that will contain and manage an array containing

Create a DeckOfCards class

Create a CardDealer class with a main method.

DeckOfCards Specifications

The DeckOfCards class that will contain and manage an array containing the 52 unique Card objects. You will keep track of the size of the deck with a constant integer named DECKSIZE. You will also keep track of the next card to deal using an instance variable named nextCardIndex.

Your DeckOfCards class must implement the DeckOfCardsInterface. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

public class DeckOfCards implements DeckOfCardsInterface { //These are the only instance variables you will need. private final int DECKSIZE = 52; private Card[] cards; private int nextCardIndex; ... } 

To implement the interface, you will need to use the implements keyword in your DeckOfCards class declaration as follows.

Your DeckOfCards class should contain at least the following methods. We recommend that you implement the methods in the order they are listed below.

Pro tip: Test your methods in your CardDealer main method as you write them to make sure they are working.

public DeckOfCards() - The constructor. Creates a new deck of cards. The constructor will need a nested loop to go over all combinations of Suit values and FaceValue values and create a card for each of the 52 possibilities. The following nested for-each loop will make sure that you are creating a card with each suit and face value, but you will also need a variable to keep track of where you are inserting your next card in the cards array e.g. cards[i] = new Card(s, v);

for(Suit s : Suit.values()) { for(FaceValue v : FaceValue.values()) { //Create new card and add it to your deck. } } 

void shuffle() - You can use the shuffle method given below or you could try to write your own custom shuffle algorithm.

public void shuffle() { Random generator = new Random(); //Attempt to swap each card with a random card in the deck. //This isn't a perfect random shuffle but it is a simple one. //A better shuffle requires a more complex algorithm. for (int i=0; i< cards.length; i++) { int j = generator.nextInt(cards.length); Card temp = cards[i]; cards[i] = cards[j]; cards[j] = temp; } //Reset instance variable that keeps track of dealt and remaining cards. nextCardIndex = 0; 

}

String toString() - return a String that represents the state of the deck as shown in the output below.

Card draw() - draw one Card from the deck. Obviously, once a card has been drawn, it is no longer in the deck and can't be drawn again. After 52 calls to draw() the deck will be empty and the method should return null. To make this work, DeckOfCards will need to keep track of the cards that have been dealt and the cards that have not been dealt. The easiest way to accomplish this is to keep track of the index in the card array that separates the dealt cards from the undealt cards.Keep track of this using the instance variable nextCardIndex.

int numCardsRemaining() - return the number of cards that have not been dealt.

int numCardsDealt() - return the number of cards that have been dealt.

Card[] dealtCards() - return the collection of all cards that have been dealt. Make sure you do not violate encapsulation in this method (the array you return should not be one your deck is using). The variable nextCardIndex points to the index of the next card to be dealt, which implies that cards from index 0 to nextCardIndex - 1 have already been dealt. The following method creates a temporary array to copy references to the dealt cards and returns that to support encapsulation.

Card[] remainingCards() - return the collection of all cards that have not been dealt. Make sure you do not violate encapsulation in this method (the array you return should not be one your deck is using).

CardDealer

Write a driver class called CardDealer to test DeckOfCards. This class will have the main method that will have the code for the following.

First, create a new DeckOfCards and print the deck (using the toString() method) to verify that it contains the correct cards.

Shuffle your deck and print it to verify that it is shuffled.

Write a card game where the computer plays against itself (no Scanner needed!).

For 5 rounds, draw 1 card for computer player 1, and draw 1 card for computer player 2.

Use the card.compareTo method to determine which card is higher and print the winner. For example, to compare two Card objects player1 and player2:

if (player1.compareTo(player2) > 0) { //player 1's card is bigger so they win } 

Print the dealt cards.

Print the remaining cards in the deck.

BONUS: Make it more awesome by reading the number of rounds to play from the command-line. The usage for your program should be:

Usage: CardDealer 

Card.java file

/** * Represents a card from a deck of standard playing cards * * @author Instructors */ public class Card implements Comparable { private Suit suit; private FaceValue face;

/** * Creates a new Card with the give Suit and FaceValue. * * @param s Card's suit. * @param v Card's face value. */ public Card(Suit s, FaceValue v) { suit = s; face = v; }

/** * Returns this Card's suit. * * @return card suit */ public Suit getSuit() { return suit; }

/** * Returns this Card's face value. * * @return face value */ public FaceValue getFaceValue() { return face; }

/** * Checks if the given card is equal to this card (same * suit and face value). * * @param other * Card to compare * @return true if Cards are equivalent, else false */ public boolean equals(Card other) { return (this.suit == other.suit && this.face == other.face); }

/* * (non-Javadoc) * * @see java.lang.Object#toString() */ public String toString() { return face + " of " + suit; }

/** * Return relative rank of this Card as compared to other Card. * * @param other * Card to compare to this Card for relative rank * @return -1 if this Card is less than other Card, +1 if this Card is * greater than other Card, 0 if Card values are equal */ @Override public int compareTo(Card other) { if (this.face.getRank() < other.face.getRank()) { return -1; } else if (this.face.getRank() > other.face.getRank()) { return 1; } return 0; } }

My DeckOfCards.java file (Is incomplete). Needs methods for dealtCards and remainingCards as dictated in specifications but I'm struggling to know how to complete that.

import java.util.Random;

public class DeckOfCards implements DeckOfCardsInterface { private final int DECKSIZE= 52; private Card[] cards; private int nextCardIndex;

public DeckOfCards(){ Card[] cards= new Card[DECKSIZE]; int i=0; for(Suit s: Suit.values()){ for(FaceValue v: FaceValue.values()){ //Create a new card and add it to your deck cards[i]=new Card(s,v); i++; //System.out.print(); } } }

public void shuffle() { Random generator= new Random();

for(int i=0; i int j= generator.nextInt(cards.length); Card temp=cards[i]; cards[i]=cards[j]; cards[j]=temp;

nextCardIndex=0; }

} public String toString() { String str=""; for(int i=nextCardIndex; i str=" "+cards[i].getFaceValue() + "of"+ cards[i].getSuit(); } return str; } public Card draw() {

if(nextCardIndex nextCardIndex++; return cards[nextCardIndex-1]; //nextCardIndex++; } else{ return null; } }

public int numCardsRemaining() { return DECKSIZE-nextCardIndex; }

public int numCardsDealt() { return nextCardIndex; }

public Card[] dealtCards() { //for(int i=0; i return null; //}

}

public Card[] remainingCards() { return null; } }

CardDealer.java not yet created. Needs a main method.

You may also need the following enums and interface (although it is implemented already in DeckOfCards):

Suit.java enum:

/** * Represents the suits of standard playing cards * * @author Instructors */ public enum Suit { Clubs("\u2663"), Diamonds("\u2666"), Hearts("\u2665"), Spades("\u2660");

private String symbol;

Suit(String symbol) { this.symbol = symbol; }

/** * Returns the unicode symbol for this Suit. * @return String the unicode value. */ public String getSymbol() { return this.symbol; }

@Override public String toString() { return this.name() + " (" + symbol + ")"; } }

FaceValue.java enum:

/** * Represents the face value of standard playing cards. * * @author Instructors */ public enum FaceValue { Two(2), Three(3), Four(4), Five(5), Six(6), Seven(7), Eight(8), Nine(9), Ten(10), Jack(11), Queen(12), King(13), Ace(14);

private int rank;

FaceValue(int rank) { this.rank = rank; }

public int getRank() { return rank; } }

DeckOfCardsInterface.java

/** * An interface for a deck of cards. * @author Instructors */ public interface DeckOfCardsInterface { /** * Shuffles the deck. After this method is called, each call to the * draw() method will return a random card. */ public void shuffle();

/** * Draws and returns the next undealt Card in the deck. * * Once a card has been drawn, it is no longer in the deck and can't be * drawn again. After 52 calls to draw() the deck will be empty and the * method will return null. * * @return The next Card from the deck, or null if the deck is empty. */ public Card draw();

/** * Returns the number of cards remaining in the deck. * @return the number of cards. */ public int numCardsRemaining();

/** * Returns the number of cards dealt. * @return the number of cards. */ public int numCardsDealt();

/** * Returns the collection of all cards that have been dealt so far. * * @return an array containing the dealt cards. The array will be empty * if no cards have been dealt. */ public Card[] dealtCards();

/** * Returns the collection of all cards still in the deck. * * @return an array containing the cards. The array will be empty if * no cards are left in the deck. */ public Card[] remainingCards();

@Override public String toString(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!