Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USING JAVA, thank you! 1. Using the file Deck.java, found below, complete the Deck class by implementing the shuffle method (found in shuffler.java). Use the

USING JAVA, thank you!

1. Using the file Deck.java, found below, complete the Deck class by implementing the shuffle method (found in shuffler.java). Use the efficient selection shuffle, which works as follows:

For k = 51 down to 1, Generate a random integer r between 0 and k, inclusive; Exchange cards[k] and cards[r].

Note that the Deck constructor creates the deck and then calls the shuffle method. The shufflemethod also needs to reset the value of size to indicate that all of the cards can be dealt again.

2. The DeckTester.java file, found below, provides a basic set of Decktests.

Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method. You can use the Deck toString method to see the cards after every shuffle. Thanks again!!!

Deck.java

import java.util.List; import java.util.ArrayList;

/** * The Deck class represents a shuffled deck of cards. * It provides several operations including * initialize, shuffle, deal, and check if empty. */ public class Deck {

/** * cards contains all the cards in the deck. */ private List cards;

/** * size is the number of not-yet-dealt cards. * Cards are dealt from the top (highest index) down. * The next card to be dealt is at size - 1. */ private int size;

/** * Creates a new Deck instance.
* It pairs each element of ranks with each element of suits, * and produces one of the corresponding card. * @param ranks is an array containing all of the card ranks. * @param suits is an array containing all of the card suits. * @param values is an array containing all of the card point values. */ public Deck(String[] ranks, String[] suits, int[] values) { cards = new ArrayList(); for (int j = 0; j < ranks.length; j++) { for (String suitString : suits) { cards.add(new Card(ranks[j], suitString, values[j])); } } size = cards.size(); shuffle(); }

/** * Determines if this deck is empty (no undealt cards). * @return true if this deck is empty, false otherwise. */ public boolean isEmpty() { return size == 0; }

/** * Accesses the number of undealt cards in this deck. * @return the number of undealt cards in this deck. */ public int size() { return size; }

/** * Randomly permute the given collection of cards * and reset the size to represent the entire deck. */ public void shuffle() { /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */ }

/** * Deals a card from this deck. * @return the card just dealt, or null if all the cards have been * previously dealt. */ public Card deal() { if (isEmpty()) { return null; } size--; Card c = cards.get(size); return c; }

/** * Generates and returns a string representation of this deck. * @return a string representation of this deck. */ @Override public String toString() { String rtn = "size = " + size + " Undealt cards: ";

for (int k = size - 1; k >= 0; k--) { rtn = rtn + cards.get(k); if (k != 0) { rtn = rtn + ", "; } if ((size - k) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + " "; } }

rtn = rtn + " Dealt cards: "; for (int k = cards.size() - 1; k >= size; k--) { rtn = rtn + cards.get(k); if (k != size) { rtn = rtn + ", "; } if ((k - cards.size()) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + " "; } }

rtn = rtn + " "; return rtn; } }

Card.java

/** * Card.java * * Card represents a playing card. */ public class Card {

/** * String value that holds the suit of the card */ private String suit;

/** * String value that holds the rank of the card */ private String rank;

/** * int value that holds the point value. */ private int pointValue;

/** * Creates a new Card instance. * * @param cardRank a String value * containing the rank of the card * @param cardSuit a String value * containing the suit of the card * @param cardPointValue an int value * containing the point value of the card */ public Card(String cardRank, String cardSuit, int cardPointValue) { //initializes a new Card with the given rank, suit, and point value rank = cardRank; suit = cardSuit; pointValue = cardPointValue; }

/** * Accesses this Card's suit. * @return this Card's suit. */ public String suit() { return suit; }

/** * Accesses this Card's rank. * @return this Card's rank. */ public String rank() { return rank; }

/** * Accesses this Card's point value. * @return this Card's point value. */ public int pointValue() { return pointValue; }

/** Compare this card with the argument. * @param otherCard the other card to compare to this * @return true if the rank, suit, and point value of this card * are equal to those of the argument; * false otherwise. */ public boolean matches(Card otherCard) { return otherCard.suit().equals(this.suit()) && otherCard.rank().equals(this.rank()) && otherCard.pointValue() == this.pointValue(); }

/** * Converts the rank, suit, and point value into a string in the format * "[Rank] of [Suit] (point value = [PointValue])". * This provides a useful way of printing the contents * of a Deck in an easily readable format or performing * other similar functions. * * @return a String containing the rank, suit, * and point value of the card. */ @Override public String toString() { return rank + " of " + suit + " (point value = " + pointValue + ")"; } }

Shuffler.java

/** * This class provides a convenient way to test shuffling methods. */ public class Shuffler {

/** * The number of consecutive shuffle steps to be performed in each call * to each sorting procedure. */ private static final int SHUFFLE_COUNT = 1;

/** * The number of values to shuffle. */ private static final int VALUE_COUNT = 4;

/** * Tests shuffling methods. * @param args is not used. */ public static void main(String[] args) { System.out.println("Results of " + SHUFFLE_COUNT + " consecutive perfect shuffles:"); int[] values1 = new int[VALUE_COUNT]; for (int i = 0; i < values1.length; i++) { values1[i] = i; } for (int j = 1; j <= SHUFFLE_COUNT; j++) { perfectShuffle(values1); System.out.print(" " + j + ":"); for (int k = 0; k < values1.length; k++) { System.out.print(" " + values1[k]); } System.out.println(); } System.out.println();

System.out.println("Results of " + SHUFFLE_COUNT + " consecutive efficient selection shuffles:"); int[] values2 = new int[VALUE_COUNT]; for (int i = 0; i < values2.length; i++) { values2[i] = i; } for (int j = 1; j <= SHUFFLE_COUNT; j++) { selectionShuffle(values2); System.out.print(" " + j + ":"); for (int k = 0; k < values2.length; k++) { System.out.print(" " + values2[k]); } System.out.println(); } System.out.println(); }

/** * Apply a "perfect shuffle" to the argument. * The perfect shuffle algorithm splits the deck in half, then interleaves * the cards in one half with the cards in the other. * @param values is an array of integers simulating cards to be shuffled. */ public static void perfectShuffle(int[] values) { /* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */ }

/** * Apply an "efficient selection shuffle" to the argument. * The selection shuffle algorithm conceptually maintains two sequences * of cards: the selected cards (initially empty) and the not-yet-selected * cards (initially the entire deck). It repeatedly does the following until * all cards have been selected: randomly remove a card from those not yet * selected and add it to the selected cards. * An efficient version of this algorithm makes use of arrays to avoid * searching for an as-yet-unselected card. * @param values is an array of integers simulating cards to be shuffled. */ public static void selectionShuffle(int[] values) { /* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */ } }

DeckTester.java

/** * This is a class that tests the Deck class. */ public class DeckTester {

/** * The main method in this class checks the Deck operations for consistency. * @param args is not used. */ public static void main(String[] args) { String[] ranks = {"jack", "queen", "king"}; String[] suits = {"blue", "red"}; int[] pointValues = {11, 12, 13}; Deck d = new Deck(ranks, suits, pointValues);

System.out.println("**** Original Deck Methods ****"); System.out.println(" toString: " + d.toString()); System.out.println(" isEmpty: " + d.isEmpty()); System.out.println(" size: " + d.size()); System.out.println(); System.out.println();

System.out.println("**** Deal a Card ****"); System.out.println(" deal: " + d.deal()); System.out.println(); System.out.println();

System.out.println("**** Deck Methods After 1 Card Dealt ****"); System.out.println(" toString: " + d.toString()); System.out.println(" isEmpty: " + d.isEmpty()); System.out.println(" size: " + d.size()); System.out.println(); System.out.println();

System.out.println("**** Deal Remaining 5 Cards ****"); for (int i = 0; i < 5; i++) { System.out.println(" deal: " + d.deal()); } System.out.println(); System.out.println();

System.out.println("**** Deck Methods After All Cards Dealt ****"); System.out.println(" toString: " + d.toString()); System.out.println(" isEmpty: " + d.isEmpty()); System.out.println(" size: " + d.size()); System.out.println(); System.out.println();

System.out.println("**** Deal a Card From Empty Deck ****"); System.out.println(" deal: " + d.deal()); System.out.println(); System.out.println();

/* *** TO BE COMPLETED IN ACTIVITY 4 *** */ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions