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

1. Write a Deck class that generates all 52 playing cards and stores it. 2. Writ

ID: 3750575 • Letter: 1

Question

1. Write a Deck class that generates all 52 playing cards and stores it.
2. Write a Hand class that will store 5 cards
3. Write a player class that stores-player_name, accumulated_points,...and other possible
4. Write a dealer class that uses Deck class and gives n randomfor n Player(s).The deck has to be shuffled each time before giving a hand to players

Use any Data structure you like to store the cards but each card has to be unique like:
a. C1,C2....C13(clubs)
b. D1,D2....D13(Diaminds)
c. H1,H2....H13(Hearts)
d. S1,S2....S13(Spades)

Explanation / Answer


public class Deck {
public static void main(String[] args) {
String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};

String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};

// initialize deck
int n = SUITS.length * RANKS.length;
String[] deck = new String[n];
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}

// shuffle
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}

// print shuffled deck
for (int i = 0; i < n; i++) {
System.out.println(deck[i]);
}
}

}
import java.util.Vector;

public class Hand {

private Vector hand;

public Hand() {
  
hand = new Vector();
}

public void clear() {
  
hand.removeAllElements();
}

public void addCard(Card c) {
  
if (c != null)
hand.addElement(c);
}

public void removeCard(Card c) {
  
hand.removeElement(c);
}

public void removeCard(int position) {

if (position >= 0 && position < hand.size())
hand.removeElementAt(position);
}

public int getCardCount() {
  
return hand.size();
}

public Card getCard(int position) {
  
if (position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}

public void sortBySuit() {
  
Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = (Card)hand.elementAt(0); // Minumal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getSuit() < c.getSuit() ||
(c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}

public void sortByValue() {

Vector newHand = new Vector();
while (hand.size() > 0) {
int pos = 0; // Position of minimal card.
Card c = (Card)hand.elementAt(0); // Minumal card.
for (int i = 1; i < hand.size(); i++) {
Card c1 = (Card)hand.elementAt(i);
if ( c1.getValue() < c.getValue() ||
(c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
pos = i;
c = c1;
}
}
hand.removeElementAt(pos);
newHand.addElement(c);
}
hand = newHand;
}

}
public class Player {

private String PlayerName;
private int accumulatedPoints;
  
public Player(String playerName) {
PlayerName = playerName;
}
  
public Player(String playerName, int accumulatedPoints) {
PlayerName = playerName;
this.accumulatedPoints = accumulatedPoints;
}

public int getAccumulatedPoints() {
return accumulatedPoints;
}

public void setAccumulatedPoints(int accumulatedPoints) {
this.accumulatedPoints = accumulatedPoints;
}

public void setName(String name) {
this.PlayerName = name;
}

public String getName() {
return PlayerName;
}

}

import java.util.*;

public class Dealer implements Person{
private Player player; //The player the dealer is playing against
private final Hand hand; //The dealers hand
private Deck deck; //The deck the dealer deals from
  
/**
* Private constructor to be used in static factory method
* @param Hand, Deck
*/
private Dealer(Hand hand, Deck deck){
this.hand = hand;
this.deck = deck;
}
  
//Static factory method
public static Dealer startGame(Hand hand, Deck deck){
Dealer d = new Dealer(hand, deck);
return d;
}
  
//adds a card to the dealers hand
public Card hit(){
System.out.println("Dealer has hit");
Card c = dealNext();
hand.addCard(c);
return c;
}
  
//Deals the next card from the deck
public Card dealNext(){
Card c = deck.removeNext();
return c;
}
  
//Creates a new deck if the deck is empty
public synchronized void newDeck(){
if(deck.empty())
deck = Deck.createDeck();
}
  
//sets up the player for the dealer to play against
public synchronized void setPlayer(Person player){
this.player = player;
}
  
//deals card to the player and the dealer
public void deal(){
for(int i = 0; i < 2; ++i){
hit();
player.hit();
}
}
  
//displays the dealer hand as viewable by the player
public Card displayPlayerView(){
return hand.viewFirst();
}
  
public Hand viewHand(){
return hand;
}
  
@Override public String toString(){
return hand.toString() + " Hand Value: " + hand.getHandValue() + " ";
}
  
public static void main(String[] args){
Deck d = Deck.createDeck();
  
Hand h = new Hand(new ArrayList<Card>());
Hand h1 = new Hand(new ArrayList<Card>());
  
Dealer dealer = Dealer.startGame(h, d);
Player player = Player.joinGame(h1, 500);
  
dealer.setPlayer(player);
player.setDealer(dealer);
dealer.deal();
  
System.out.println(dealer);
System.out.println(player);
  
player.hit();
player.hit();
  
System.out.println(dealer);
System.out.println(player);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote