Write a program to store a deck of 52 cards in a linked list in random sequence using a Random class object. You can represent a card as a two-character string "1C" for the ace of clubs, "JD" for the jack of diamonds, and so on. Output the cards from the linked list as four hands of 13 cards.
import java.util.Vector; import java.util.LinkedList; import java.util.Random; import java.util.ListIterator; public class DealCards { public static void main(String[] args) { String[] suits = {"C", "D", "H", "S"}; String[] cardValues = { "1","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; int cardsInDeck = 52; Vector<String> deck = new Vector<String>(cardsInDeck); LinkedList<String> shuffledDeck = new LinkedList<String>(); Random chooser = new Random(); // Card chooser // Load the deck for(String suit : suits) { for(String cardValue : cardValues) { deck.add(cardValue + suit); } } // Select cards at random from the deck to transfer to shuffled deck int selection = 0; // Selected card index for(int i = 0 ; i < cardsInDeck ; ++i) { selection = chooser.nextInt(deck.size()); shuffledDeck.add(deck.remove(selection)); } // Deal the cards from the shuffled deck into four hands StringBuffer[] hands = { new StringBuffer("Hand 1:"), new StringBuffer("Hand 2:"), new StringBuffer("Hand 3:"), new StringBuffer("Hand 4:")}; ListIterator<String> cards = shuffledDeck.listIterator(); while(cards.hasNext()) { for(StringBuffer hand : hands) { hand.append(' ').append(cards.next()); } } // Display the hands for(StringBuffer hand : hands) { System.out.println(hand); } } }Output of Above Java Program
Hand 1: 3D KS 8C 9S 10D 4D 5C JC 8D 1C 5S 10C 7C Hand 2: 5D 1H 6H 7H 5H 3H KH 6D 10S QC 9D 7S 3C Hand 3: 1D 9C QH 2S QD 2C 6C JH 8H KD JD 4H 7D Hand 4: 9H 1S 4C 3S 10H KC 2H 4S QS 8S 2D 6S JS