Answer to Question #290981 in Java | JSP | JSF for Jin

Question #290981

Implement an application (a system) of any abstract data type (stack, tree, graph, hashing) using Java as programming language. Some of the examples are the following:





• Dictionary implementation using AVL Tree





• Log-book for Task Monitoring





1
Expert's answer
2022-01-26T20:17:39-0500
import java.util.Random;
import java.util.Stack;

public class Main {
    private static char[] suits = {'\u2663', '\u2660', '\u2665', '\u2666'};
    private static String[] values = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    private static String[] masterDeck;
    private static Stack<String> deck;
    private static String[][] players;

    public static void createMasterDeck() {
        masterDeck = new String[52];
        int k = 0;
        for (int i = 0; i < suits.length; i++) {
            for (int j = 0; j < values.length; j++) {
                masterDeck[k++] = values[j] + suits[i];
            }
        }
    }

    public static void shuffleCards() {
        boolean[] used = new boolean[masterDeck.length];
        int cards = 0;
        Random random = new Random();
        deck = new Stack<>();
        while (cards < masterDeck.length) {
            int index = random.nextInt(masterDeck.length);
            if (!used[index]) {
                deck.push(masterDeck[index]);
                used[index] = true;
                cards++;
            }
        }
    }

    public static void dealCards() {
        players = new String[2][6];
        for (int i = 0; i < players.length; i++) {
            for (int j = 0; j < players[i].length; j++) {
                players[i][j] = deck.pop();
            }
        }
    }

    public static void showCards() {
        for (int i = 0; i < players[0].length; i++) {
            System.out.println((i < players[0].length - 1 ? "Player " + (i + 1) : "Dealer") +
                    ": Cards [" + players[0][i] + ", " + (i < players[0].length - 1 ? players[1][i] : "?") + "]");
        }
    }

    public static void main(String[] args) {
      createMasterDeck();
      shuffleCards();
      dealCards();
      showCards();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS