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

//Activity 5 import cards.*; import hands.*; public class Activity5 { public sta

ID: 3585265 • Letter: #

Question

//Activity 5 import cards.*; import hands.*; public class Activity5 { public static void main(String[] args) { Deck d = new Deck(52); HandOfCards hand = new HandOfTwo(); hand.addCard(d.dealOne()); hand.addCard(d.dealOne()); System.out.println("Hand :"); hand.printHand(); } package hands; import cards.PlayingCard; // HandOfTwo is a class implementing a hand of 2 cards using 2 variables public class HandOfTwo implements HandOfCards { PlayingCard card1; PlayingCard card2; public void HandOf2() { card1 = null; card2 = null; } public void addCard(PlayingCard c) { if (card1 == null) card1 = c; else if (card2 == null) card2 = c; // else hand is full, do nothing } public void printHand() { if (card1 != null) System.out.println("Card 1: " + card1); if (card2 != null) System.out.println("Card 2: " + card2); } } Recall that the source code has two implementations of a HandOfCards. Add a new hand of cards class called LinkedListHand that uses the linked list class we made in class (not the implementation from the book or the Java API) as the underlying data structure. Your new class should be part of the hands package. In your driver, create a new hand of cards using the linked list hand of cards class and display its contents

Explanation / Answer

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: