Introduction: You implemented a Deck class in Activity 2. This class should be c
ID: 3800959 • Letter: I
Question
Introduction:
You implemented a Deck class in Activity 2. This class should be complete except for the shuffle
method. You also implemented a DeckTester class that you used to test your incomplete Deck
class.
In Activity 3, you implemented methods in the Shuffler class, which shuffled integers.
Now you will use what you learned about shuffling in Activity 3 to implement the Deck shuffle
method.
Exercises:
1. The file Deck.java, found in the Activity4 Starter Code folder, is a correct solution from
Activity 2. Complete the Deck class by implementing the shuffle method. Use the efficient
selection shuffle algorithm from Activity 3.
Note that the Deck constructor creates the deck and then calls the shuffle method. The
shuffle method 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 in the Activity4 Starter Code folder, provides a basic set
of Deck tests. It is similar to the DeckTester class you might have written in Activity 2.
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.
---------------------------------------------
public class Shuffler {
private static final int SHUFFLE_COUNT = 4;
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++) {
values1 = 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++) {
values2 = 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();
}
public static int[] perfectShuffle(int[] values) {
int[] tVal = new int[values.length];
int c = 0;
for (int i = 0; i < (values.length + 1) / 2; i++) {
tVal[c] = values[i];
c += 2;
}
c = 1;
for (int i = (values.length + 1) / 2; i < values.length; i++) {
tVal[c] = values[i];
c += 2;
}
return tVal;
}
public static int[] selectionShuffle(int[] values) {
for (int i = values.length - 1; i > 0; i--) {
int t = (int) Math.round(Math.random() * i);
int tmp = values[i];
values[i] = values[t];
values[t] = tmp;
}
return values;
}
}
------------------------------------------
public class DeckTester {
public static void main(String[] args) {
String[] ranks = { "jack", "queen", "king" };
String[] suits = { "blue", "red", "green"};
int[] pointValues = { 11, 12, 13 };
Deck d = new Deck(ranks, suits, pointValues);
System.out.println(d);
d.deal();
System.out.println(d);
System.out.println("Is the deck empty? " + d.isEmpty());
}
}
-------------------------------------------------
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<Card> 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 <code>Deck</code> instance.<BR>
*
* 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) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
cards = new ArrayList<Card>();
for (int i = 0; i < suits.length; i++)
{
for (int j = 0; j < ranks.length; j++)
{
cards.add(new Card(ranks[j], suits[i], values[j]));
}
}
size = cards.size();
}
/**
*
* Determines if this deck is empty (no undealt cards).
*
* @return true if this deck is empty, false otherwise.
*
*/
public boolean isEmpty() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
return cards.isEmpty();
}
/**
*
* Accesses the number of undealt cards in this deck.
*
* @return the number of undealt cards in this deck.
*
*/
public int size() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
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() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if (isEmpty())
return null;
else
{
size--;
return cards.get(cards.size() - 1);
}
}
/**
*
* 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;
}
}
Explanation / Answer
PROGRAM CODE:
Deck.java
package cards;
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<Card> 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 <code>Deck</code> instance.<BR>
*
* 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) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
cards = new ArrayList<Card>();
for (int i = 0; i < suits.length; i++)
{
for (int j = 0; j < ranks.length; j++)
{
cards.add(new Card(ranks[j], suits[i], 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() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if(size>0)
return false;
else return true;
}
/**
*
* Accesses the number of undealt cards in this deck.
*
* @return the number of undealt cards in this deck.
*
*/
public int size() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
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 *** */
for(int i=cards.size()-1; i>0; i--)
{
int t = (int) Math.round(Math.random() * i);
Card temp = cards.get(i);
cards.set(i, cards.get(t));
cards.set(t, temp);
}
size = cards.size();
}
/**
*
* Deals a card from this deck.
*
* @return the card just dealt, or null if all the cards have been
*
* previously dealt.
*
*/
public Card deal() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if (isEmpty())
return null;
else
{
size--;
return cards.get(size);
}
}
/**
*
* 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;
}
}
DeckTester.java
package cards;
/**
* 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();
String[] fullranks = {"ace","king","queen", "jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
String[] fullSuits = {"spade", "clover", "diamond", "heart"};
int[] fullValues = {11, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2};
d = new Deck(fullranks, fullSuits, fullValues);
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();
/* *** TO BE COMPLETED IN ACTIVITY 4 *** */
}
}
OUTPUT:
**** Original Deck Methods ****
toString:
size = 6
Undealt cards:
Rank: jack Suit: red Value: 11, Rank: jack Suit: blue Value: 11,
Rank: king Suit: red Value: 13, Rank: king Suit: blue Value: 13,
Rank: queen Suit: blue Value: 12, Rank: queen Suit: red Value: 12
Dealt cards:
isEmpty: false
size: 6
**** Deal a Card ****
deal: Rank: jack Suit: red Value: 11
**** Deck Methods After 1 Card Dealt ****
toString:
size = 5
Undealt cards:
Rank: jack Suit: blue Value: 11, Rank: king Suit: red Value: 13,
Rank: king Suit: blue Value: 13, Rank: queen Suit: blue Value: 12,
Rank: queen Suit: red Value: 12
Dealt cards:
Rank: jack Suit: red Value: 11
isEmpty: false
size: 5
**** Deal Remaining 5 Cards ****
deal: Rank: jack Suit: blue Value: 11
deal: Rank: king Suit: red Value: 13
deal: Rank: king Suit: blue Value: 13
deal: Rank: queen Suit: blue Value: 12
deal: Rank: queen Suit: red Value: 12
**** Deck Methods After All Cards Dealt ****
toString:
size = 0
Undealt cards:
Dealt cards:
Rank: jack Suit: red Value: 11, Rank: jack Suit: blue Value: 11,
Rank: king Suit: red Value: 13, Rank: king Suit: blue Value: 13,
Rank: queen Suit: blue Value: 12, Rank: queen Suit: red Value: 12
isEmpty: true
size: 0
**** Deal a Card From Empty Deck ****
deal: null
**** Original Deck Methods ****
toString:
size = 52
Undealt cards:
Rank: 10 Suit: diamond Value: 10, Rank: king Suit: clover Value: 10,
Rank: 3 Suit: heart Value: 3, Rank: 6 Suit: clover Value: 6,
Rank: 6 Suit: diamond Value: 6, Rank: 7 Suit: diamond Value: 7,
Rank: 9 Suit: diamond Value: 9, Rank: 2 Suit: diamond Value: 2,
Rank: 9 Suit: spade Value: 9, Rank: queen Suit: heart Value: 10,
Rank: 7 Suit: heart Value: 7, Rank: 10 Suit: clover Value: 10,
Rank: 10 Suit: heart Value: 10, Rank: 3 Suit: diamond Value: 3,
Rank: 8 Suit: spade Value: 8, Rank: 2 Suit: heart Value: 2,
Rank: 5 Suit: diamond Value: 5, Rank: 7 Suit: clover Value: 7,
Rank: 4 Suit: heart Value: 4, Rank: ace Suit: heart Value: 11,
Rank: 4 Suit: diamond Value: 4, Rank: 3 Suit: spade Value: 3,
Rank: queen Suit: spade Value: 10, Rank: 8 Suit: heart Value: 8,
Rank: 2 Suit: clover Value: 2, Rank: 6 Suit: heart Value: 6,
Rank: jack Suit: diamond Value: 10, Rank: 4 Suit: spade Value: 4,
Rank: king Suit: diamond Value: 10, Rank: king Suit: spade Value: 10,
Rank: queen Suit: diamond Value: 10, Rank: 3 Suit: clover Value: 3,
Rank: 9 Suit: clover Value: 9, Rank: 8 Suit: clover Value: 8,
Rank: ace Suit: diamond Value: 11, Rank: king Suit: heart Value: 10,
Rank: 2 Suit: spade Value: 2, Rank: 5 Suit: spade Value: 5,
Rank: ace Suit: spade Value: 11, Rank: 4 Suit: clover Value: 4,
Rank: 10 Suit: spade Value: 10, Rank: ace Suit: clover Value: 11,
Rank: queen Suit: clover Value: 10, Rank: 9 Suit: heart Value: 9,
Rank: 6 Suit: spade Value: 6, Rank: 7 Suit: spade Value: 7,
Rank: jack Suit: clover Value: 10, Rank: 5 Suit: clover Value: 5,
Rank: jack Suit: heart Value: 10, Rank: 5 Suit: heart Value: 5,
Rank: jack Suit: spade Value: 10, Rank: 8 Suit: diamond Value: 8
Dealt cards:
isEmpty: false
size: 52
**** Deal a Card ****
deal: Rank: 10 Suit: diamond Value: 10
**** Deck Methods After 1 Card Dealt ****
toString:
size = 51
Undealt cards:
Rank: king Suit: clover Value: 10, Rank: 3 Suit: heart Value: 3,
Rank: 6 Suit: clover Value: 6, Rank: 6 Suit: diamond Value: 6,
Rank: 7 Suit: diamond Value: 7, Rank: 9 Suit: diamond Value: 9,
Rank: 2 Suit: diamond Value: 2, Rank: 9 Suit: spade Value: 9,
Rank: queen Suit: heart Value: 10, Rank: 7 Suit: heart Value: 7,
Rank: 10 Suit: clover Value: 10, Rank: 10 Suit: heart Value: 10,
Rank: 3 Suit: diamond Value: 3, Rank: 8 Suit: spade Value: 8,
Rank: 2 Suit: heart Value: 2, Rank: 5 Suit: diamond Value: 5,
Rank: 7 Suit: clover Value: 7, Rank: 4 Suit: heart Value: 4,
Rank: ace Suit: heart Value: 11, Rank: 4 Suit: diamond Value: 4,
Rank: 3 Suit: spade Value: 3, Rank: queen Suit: spade Value: 10,
Rank: 8 Suit: heart Value: 8, Rank: 2 Suit: clover Value: 2,
Rank: 6 Suit: heart Value: 6, Rank: jack Suit: diamond Value: 10,
Rank: 4 Suit: spade Value: 4, Rank: king Suit: diamond Value: 10,
Rank: king Suit: spade Value: 10, Rank: queen Suit: diamond Value: 10,
Rank: 3 Suit: clover Value: 3, Rank: 9 Suit: clover Value: 9,
Rank: 8 Suit: clover Value: 8, Rank: ace Suit: diamond Value: 11,
Rank: king Suit: heart Value: 10, Rank: 2 Suit: spade Value: 2,
Rank: 5 Suit: spade Value: 5, Rank: ace Suit: spade Value: 11,
Rank: 4 Suit: clover Value: 4, Rank: 10 Suit: spade Value: 10,
Rank: ace Suit: clover Value: 11, Rank: queen Suit: clover Value: 10,
Rank: 9 Suit: heart Value: 9, Rank: 6 Suit: spade Value: 6,
Rank: 7 Suit: spade Value: 7, Rank: jack Suit: clover Value: 10,
Rank: 5 Suit: clover Value: 5, Rank: jack Suit: heart Value: 10,
Rank: 5 Suit: heart Value: 5, Rank: jack Suit: spade Value: 10,
Rank: 8 Suit: diamond Value: 8
Dealt cards:
Rank: 10 Suit: diamond Value: 10
isEmpty: false
size: 51
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.