Menu

[Solved]Write Java Code Simulates Start Card Game Ll Need 5 Classes Card Provided Carddealer Attri Q37262101

Write a java code that simulates the start of a card game.You’ll need 5 classes:

Card: Provided

CardDealer: no attributes, main() creates a new CardPlayer() andDeck(), then calls a .shuffle() method located in Deck, next deals5 cards to the player (call .deal() located in the Deck and passit’s result to .getCard() located in CardPlayer, finally call.showCards() located in CardPlayer.

CardPlayer: 1 attribute: private TreeSet hand; 1 command in theconstructor: hand=new TreeSet(new CardComparator()); and 2 othermethods: getCard(Card c) and showCards() which displays all 5 cardsin ascending order.

Deck: 1 attibute: an ArrayList named cards to hold the deck ofall 52 cards, a constructor that creates the deck, then loads all52 cards into it like this:

for (int suit = Card.CLUBS; suit <= Card.SPADES;suit++)     {

         for (int face =Card.ACE; face <= Card.KING; face++)

             cards.add(new Card(face, suit));

      }

finally the constructor calls shuffle(); to shuffle the deck;the shuffle() method, which just calls Collections.shuffle() andpasses in the deck; the deal() method, which if the deck’s notempty returns cards.remove(0); otherwise return null;

CardComparator: this class needs to handle cases where 2 cardsare “tied” based on faceValue by comparing suits, e.g., Six ofClubs < Six of Spades (see the Cards class to understandwhy).

public class Card {

// Constants for suits
public final static int CLUBS = 0;
public final static int DIAMONDS = 1;
public final static int HEARTS = 2;
public final static int SPADES = 3;

// Constants for face values
public final static int JOKER = 0;
public final static int ACE = 1;
public final static int TWO = 2;
public final static int THREE = 3;
public final static int FOUR = 4;
public final static int FIVE = 5;
public final static int SIX = 6;
public final static int SEVEN = 7;
public final static int EIGHT = 8;
public final static int NINE = 9;
public final static int TEN = 10;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;

// Array of printable face value names
private static String[] valueNames
= {“Joker”, “Ace”, “Two”, “Three”, “Four”,
“Five”, “Six”, “Seven”, “Eight”, “Nine”,
“Ten”, “Jack”, “Queen”, “King”};

// Array of printable suit names
private static String[] suitNames
// = {“Hearts”, “Diamonds”, “Spades”, “Clubs”};
= { “Clubs”, “Diamonds”,”Hearts”, “Spades”,};
  
// Instance fields for face value and suit
private int value;
private int suit;

/**
* Constructor
*
* @param faceValue An integer representing the card’s facevalue.
* @param cardSuit An integer representing the card’s suit.
* @throws IllegalArgumentException when faceValue or cardSuit areset to
* illegal values.
*/
public Card(int faceValue, int cardSuit) {
// Validate the face value.
if (faceValue < ACE || faceValue > KING) {
throw new IllegalArgumentException();
}

// Validate the suit.
if (cardSuit < CLUBS || cardSuit > SPADES) {
throw new IllegalArgumentException();
}

suit = cardSuit;
value = faceValue;
}

/**
* getFaceValue method
*
* @return An integer representing the card’s face value.
*/
public int getFaceValue() {
return value;
}

/**
* getSuit method
*
* @return An integer representing the card’s suit.
*/
public int getSuit() {
return suit;
}

/**
* toString method
*
* @return A String representing the suit and face value of thecard.
*/
public String toString() {
return valueNames[value] + ” of “
+ suitNames[suit];
}
}

Expert Answer


Answer to Write a java code that simulates the start of a card game. You’ll need 5 classes: Card: Provided CardDealer: no attrib… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *