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

Previous Code: public class Cards { /** Array of suit names, in order. */ public

ID: 3840329 • Letter: P

Question

Previous Code:

public class Cards
{
    /** Array of suit names, in order. */
    public static final String[] SUITS =
        {"Clubs", "Diamonds", "Hearts", "Spades"};
    /** Array of rank names, in order. */
    public static final String[] RANKS =
        {"Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King"};

    // Symbolic constants.
  
    public static final int NCARDS = SUITS.length * RANKS.length;
    public static final int CARDS_PER_HAND = 5;

    /**
     * Main method.
     *
     * @param args Name of player.
     */
    public static void main(String[] args)
    {
        checkUsage(args);
        String name = args[0];
        System.out.printf("Hello, %s%n", name);

        int[] deck = initializeDeck();
        display(deck);

        shuffle(deck);
        display(deck);

        int[] hand = dealHand(deck);
        display(hand);
        displayHand(hand);
    }

    public static void checkUsage(String[] args)
    {
        if (args.length < 1) {
            System.out.println("usage: java Cards <yourName>");
            System.exit(-1);
        }
    }

    public static int[] initializeDeck()
    {
        int[] deck = new int[NCARDS];
        for (int i = 0; i < deck.length; ++i) {
            deck[i] = i;
        }
        return deck;
    }

    public static void display(int[] array)
    {
        System.out.println(Arrays.toString(array));
    }

    public static void shuffle(int[] deck)
    {
        Random rand = new Random();

        for (int i = 0; i < NCARDS; ++i) {
            int j = rand.nextInt(NCARDS);
            swap(deck, i, j);
        }
    }

    public static void swap(int[] array, int from, int to)
    {
        int temp = array[from];
        array[from] = array[to];
        array[to] = temp;
    }

    public static int[] dealHand(int[] deck)
    {
        int[] hand = new int[CARDS_PER_HAND];
        for (int i = 0; i < CARDS_PER_HAND; ++i) {
            hand[i] = deck[i];
        }
        return hand;
    }

    public static void displayHand(int[] hand)
    {
        String[] cards = new String[CARDS_PER_HAND];
        for (int i = 0; i < CARDS_PER_HAND; ++i) {
            cards[i] = cardToString(hand[i]);
        }
        System.out.println("Hand: " + Arrays.toString(cards));
    }

    public static String cardToString(int card)
    { return RANKS[rank(card)] + " of " + SUITS[suit(card)]; }
  
    public static int rank(int card)
    { return card % RANKS.length; }
  
    public static int suit(int card)
    { return card / RANKS.length; }
}

Throughout much of the semester we have been working with the cards program and have developed it in multiple forms. We shall continue this development process by refactoring the last instance of the program so as to use multiple classes. You will write classes that model a Card, a Deck of Cards, and a Hand which holds Cards. Finally, you will write a main class, Poker, that will create a Deck of Cards, shuffle the Deck, and then deal out Cards to multiple Hands. For the development process, we shall work from the bottom up. Please read and follow the instructions below to write the specified four classes. Please submit the four Java source code files with the names: Card.java Deck.java Hand.java Poker.java to the Blackboard Assignment, This class represents a single card. It maintain the card's card number. It can provide the card's suit and rank, and a human readable version of the card. Define two symbolic constants as arrays of String elements named SUITS and RANKS. These arrays will be identical to the arrays we used in the previous version of the program. Based on these arrays define additional symbolic constants names NSUITS, NRANKS, and NCARDS (the last being the product of the first two). There will be one instance field named cardNum of type int. Declare this field. Define a single constructor that accepts a card number and assigns it to the instance field. Define two accessor methods similar to the ones previously written in the older version of the program: a. int rank() b. int suit() Define the method String toString() that will return a String of the form: "rank of suit". You may write a local main() method that you can use to unit test the correctness of this class. This class represents a deck of cards (we assume a normal card deck without any jokers). It maintains an array of cards and keeps track of where in the deck we are while cards are being dealt. It provides for constructing a new, ordered, deck of cards, shuffling the deck, and producing a String version of the deck for display purposes (most likely for debugging). Import classes that will be needed by this class: a. java.util.Random b. java.util.Arrays c. java.util.NoSuchElementException Import the static fields from the Card class so that we do not need to prefix their names with the class name, Card (e.g., Card. NCARDS can be referred to by just NCARDS). a. import static exam3.Card.*; Declare 3 instance fields: a. Card[] deck the array to hold the cards in the deck b. int nextCardthe index in the deck which will be the next card to deal out c. Random rand a random number generator used by shuffle (this is an instance field since we only need one generator that may be invoked multiple times if we shuffle the deck multiple times). Define a no-argument constructor. a. Instantiate the deck array with NCARDS elements. b. Set each element of the deck array with the Card element whose cardNum matches its index in the deck. c. Set next Card to 0 to indicate that we will start dealing from the top of the deck. d. Instantiate a new random number generator.

Explanation / Answer

Below is the code, please rate the answer if satisfied, else comment :

Card.java

package cards;

public class Card {
   /** Array of suit names, in order. */
   public static final String[] SUITS = { "Clubs", "Diamonds", "Hearts", "Spades" };
   /** Array of rank names, in order. */
   public static final String[] RANKS = { "Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
           "King" };
   // Symbolic constants.
   public static int NSUITS = SUITS.length;
   public static int NRANKS = SUITS.length;
   public static final int NCARDS = SUITS.length * RANKS.length;

   public static int cardNum;

   public Card(int cardNum) {
       this.cardNum = cardNum;
   }

   private int rank() {
       return NSUITS;
   }

   private int suit() {
       return NRANKS;
   }

   public String toString(int card) {
       return RANKS[rank(card)] + " of " + SUITS[suit(card)];
   }

   public static int rank(int card) {
       return card % RANKS.length;
   }

   public static int suit(int card) {
       return card / RANKS.length;
   }

   public static void main(String[] args) {
       Card card = new Card(0);
       card.rank();
       card.suit();
       System.out.println(card.toString(0));
   }
}

Deck.java

package cards;

import java.util.Random;
import java.util.Arrays;
import java.util.NoSuchElementException;

import static cards.Card.*;

public class Deck {

   private Card[] deck;
   private static int nextCard;
   private Random rand;

   public Deck() {
       deck = new Card[NCARDS];
       for (int i = 0; i < deck.length; i++) {
           if (cardNum == i) {
               deck[i] = new Card(cardNum);
           }
       }
       nextCard = 0;
       rand = new Random();

   }

   public static void shuffle(int[] deck) {
       Random rand = new Random();
       for (int i = 0; i < NCARDS; ++i) {
           int j = rand.nextInt(NCARDS);
           swap(deck, i, j);
       }
   }

   public static void swap(int[] array, int from, int to) {
       int temp = array[from];
       array[from] = array[to];
       array[to] = temp;
   }

   public String toString(int card) {
       StringBuilder builder = new StringBuilder();
       for (int i = 0; i < NRANKS; i++) {
           for (int j = 0; j < NSUITS; j++) {
               builder.append("%-17.0f " + " ");
           }
       }

       return builder.toString();
   }

   public Card dealHand(int[] deck) {
       if (nextCard >= NCARDS) {
           throw new NoSuchElementException(String.valueOf(this.nextCard));
       }
       nextCard = nextCard + 1;
       return new Card(nextCard);
   }

   public static int[] initializeDeck() {
       int[] deck = new int[NCARDS];
       for (int i = 0; i < deck.length; ++i) {
           deck[i] = i;
       }
       return deck;
   }

   public static void main(String[] args) {
       Deck deck = new Deck();

       deck.dealHand(deck.initializeDeck());
       deck.shuffle(deck.initializeDeck());
       System.out.println(deck.toString(2));
   }

}

Hand.java

package cards;

import java.util.Arrays;

public class Hand {

   static final int CARDS_PER_HAND = 5;
   Card[] hand;
   int nCards;

   public Hand() {
       hand = new Card[CARDS_PER_HAND];
       nCards = 0;
   }

   public void addCard(Card card) {
       hand[nCards] = card;
       nCards++;
   }

   public String toString() {
       return Arrays.toString(this.hand);
   }

}

Poker.java

package cards;

/**
* Class represents single card. Maintain's card number.
*
* @author Piduruchetan_Reddy
*
*/
public class Poker {

   private static final int NHANDS = 4;
   private static Hand[] hand = new Hand[NHANDS];

   public static void main(String[] args) {

       Deck deck = new Deck();
       int[] decks = deck.initializeDeck();
       for (int i = 0; i < hand.length; i++) {
           hand[i] = new Hand();
       }

       deck.shuffle(decks);
       deck.toString(NHANDS);

       for (int i = 0; i < Hand.CARDS_PER_HAND; i++) {
           for (int j = 0; j < NHANDS; j++) {
               Hand temp = new Hand();
               temp.addCard(deck.dealHand(decks));
               hand[j] = temp;
               System.out.printf("Hand %d: %s%n", j + 1, hand[j]);
           }
       }

   }
}

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