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

Complete the two methods in the cardshoe class: *dealCard() should remove the fi

ID: 3592813 • Letter: C

Question

Complete the two methods in the cardshoe class:

*dealCard() should remove the first card (e.g., card at index 0) from cards. The method should return the card that was removed from cards.

*cutDeck() does the last step of shuffling -- cutting the deck. This should move the first cutLocation number of cards ("first" meaning at the lowest index) from cards and add them to the back of the List. Make certain that you add cards in the same (relative) order that they were originally stored.

The code is provided below:

package 110

The supporting code for the class is provided here:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
* Class that can be used to get some type of list, but not a predictable type of list. This forces
* students to only use the methods defined by all Lists.
*/
public class ListGenerator {

/**
* Generates an instance of list, but which type of list is unpredictable.
* @return A new List instance.
*/
public List<PlayingCard> createNewList() {
Random rnd = new Random();
if (rnd.nextBoolean()) {
return new ArrayList<PlayingCard>();
} else {
return new LinkedList<PlayingCard>();
}
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class PlayingCard {
public enum Suits {
CLUBS, DIAMONDS, HEARTS, SPADES;

@Override
public String toString() {
return name().substring(0, 1);
}
}

public enum Ranks {
TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), TEN("T"), JACK("J"),
QUEEN("Q"), KING("K"), ACE("A");
private String display;

private Ranks(String s) {
display = s;
}

@Override
public String toString() {
return display;
}
}

private Suits suit;
private Ranks value;

public PlayingCard(Suits s, Ranks r) {
suit = s;
value = r;
}

public Suits getSuit() {
return suit;
}

public Ranks getValue() {
return value;
}

@Override
public String toString() {
return value.toString() + suit.toString();
}

public static PlayingCard[] generate(int numDecks) {
int length = numDecks * Suits.values().length * Ranks.values().length;
PlayingCard[] retVal = new PlayingCard[length];
int idx = 0;
for (int i = 0; i < numDecks; i++) {
for (Suits s : Suits.values()) {
for (Ranks r : Ranks.values()) {
PlayingCard playa = new PlayingCard(s, r);
retVal[idx] = playa;
idx += 1;
}
}
}
return retVal;
}
}

Please make sure that the code pass these junit tests:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

public class CardShoeTest {
// NOTE: Make students write these tests, also.
private Field cards;
private CardShoe testee;
private List<PlayingCard> testeeDecks;
private CardShoe testeeToo;
private List<PlayingCard> testeeTooDecks;
private ListGenerator listGen;

@Before
public final void checkFieldsUnchanged() {
Class<?> shoe = CardShoe.class;
Field[] fields = shoe.getDeclaredFields();
assertEquals("You should not add any fields to the CardShoe class. This class's field count:", 1, fields.length);
try {
cards = shoe.getDeclaredField("cards");
assertEquals("The cards field should be of type List", List.class, cards.getType());
cards.setAccessible(true);
} catch (Exception e) {
fail("Your CardShoe class should still define a field named "cards"");
}
listGen = new ListGenerator();
testeeDecks = listGen.createNewList();
PlayingCard[] decks = PlayingCard.generate(1);
for (PlayingCard p : decks) {
testeeDecks.add(p);
}
testee = new CardShoe(testeeDecks);
testeeTooDecks = listGen.createNewList();
PlayingCard[] decksToo = PlayingCard.generate(4);
for (PlayingCard p : decksToo) {
testeeTooDecks.add(p);
}
testeeToo = new CardShoe(testeeTooDecks);
}

@Test
public final void testOneDeckCutAtTwelve() throws Exception {
String[] "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H",
"4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S",
"7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C",
"TC", "JC", "QC", "KC" };
testee.cutDeck(12);
Object newCards = cards.get(testee);
assertSame("cutDeck(12) should not allocate a new List for the cards field.", testeeDecks, newCards);
assertEquals("cutDeck(12) should not change the number of elements in cards when called with a complete deck of 52 cards",
52, testeeDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(12) did not correctly cut a complete deck of 52 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeDecks.get(i).toString(), oneCut[i],
testeeDecks.get(i).toString());
}
}

@Test
public final void testOneDeckCutAtFourty() throws Exception {
String[] "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C",
"6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D",
"9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH",
"QH", "KH", "AH", "2S" };
testee.cutDeck(40);
Object newCards = cards.get(testee);
assertSame("cutDeck(40) should not allocate a new List for the cards field.", testeeDecks, newCards);
assertEquals("cutDeck(40) should not change the number of elements in cards when called with a complete deck of 52 cards",
52, testeeDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(40) did not correctly cut a complete deck of 52 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeDecks.get(i).toString(), oneCut[i], testeeDecks.get(i).toString());
}
}

@Test
public final void testMultiDeckCutAtOneThirtyThree() throws Exception {
String[] "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S",
"8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC",
"JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD",
"AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S",
"4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C",
"7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D",
"TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH",
"KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C",
"3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D",
"6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
"9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS",
"QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H",
"4H" };
testeeToo.cutDeck(133);
Object newCards = cards.get(testee);
assertSame("cutDeck(133) should not allocate a new List for the cards field when cards had 208 elements.", testeeDecks, newCards);
assertEquals("cutDeck(133) should not change the number of elements in cards when called with a 4 complete decks (so the size of cards was 208)",
208, testeeTooDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(133) did not correctly cut when cards had 208 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeTooDecks.get(i).toString(), oneCut[i], testeeTooDecks.get(i).toString());
}
}

@Test
public final void testDealCard() throws Exception {
PlayingCard answer = testeeDecks.get(0);
PlayingCard[] original = new PlayingCard[51];
for (int i = 0; i < original.length; i++ ) {
original[i] = testeeDecks.get(i + 1);
}
PlayingCard p = testee.dealCard();
Object newCards = cards.get(testee);
assertSame("deal() should not allocate a new List for the cards field.", testeeDecks, newCards);
assertEquals("deal() should remove one card from the List. Expected size of 51, but cards size was actually " +
testeeDecks.size(), 51, testeeDecks.size());
assertSame("deal() should return the actual PlayingCard instance that had been at the start of the List. Expected: " +
answer + " but was " + p, answer, p);
for (int i = 0; i < original.length; i++ ) {
assertSame("deal() rearranged or replaced cards after the card that was dealt. List element at index " + i +
" was expecting " + original[i] + "but was " + testeeDecks.get(i), original[i],
testeeDecks.get(i));
}
}
}

Explanation / Answer

The supporting code for the class is provided here:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
* Class that can be used to get some type of list, but not a predictable type of list. This forces
* students to only use the methods defined by all Lists.
*/
public class ListGenerator {

/**
* Generates an instance of list, but which type of list is unpredictable.
* @return A new List instance.
*/
public List<PlayingCard> createNewList() {
Random rnd = new Random();
if (rnd.nextBoolean()) {
return new ArrayList<PlayingCard>();
} else {
return new LinkedList<PlayingCard>();
}
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public class PlayingCard {
public enum Suits {
CLUBS, DIAMONDS, HEARTS, SPADES;

@Override
public String toString() {
return name().substring(0, 1);
}
}

public enum Ranks {
TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), TEN("T"), JACK("J"),
QUEEN("Q"), KING("K"), ACE("A");
private String display;

private Ranks(String s) {
display = s;
}

@Override
public String toString() {
return display;
}
}

private Suits suit;
private Ranks value;

public PlayingCard(Suits s, Ranks r) {
suit = s;
value = r;
}

public Suits getSuit() {
return suit;
}

public Ranks getValue() {
return value;
}

@Override
public String toString() {
return value.toString() + suit.toString();
}

public static PlayingCard[] generate(int numDecks) {
int length = numDecks * Suits.values().length * Ranks.values().length;
PlayingCard[] retVal = new PlayingCard[length];
int idx = 0;
for (int i = 0; i < numDecks; i++) {
for (Suits s : Suits.values()) {
for (Ranks r : Ranks.values()) {
PlayingCard playa = new PlayingCard(s, r);
retVal[idx] = playa;
idx += 1;
}
}
}
return retVal;
}
}

Please make sure that the code pass these junit tests:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

public class CardShoeTest {
// NOTE: Make students write these tests, also.
private Field cards;
private CardShoe testee;
private List<PlayingCard> testeeDecks;
private CardShoe testeeToo;
private List<PlayingCard> testeeTooDecks;
private ListGenerator listGen;

@Before
public final void checkFieldsUnchanged() {
Class<?> shoe = CardShoe.class;
Field[] fields = shoe.getDeclaredFields();
assertEquals("You should not add any fields to the CardShoe class. This class's field count:", 1, fields.length);
try {
cards = shoe.getDeclaredField("cards");
assertEquals("The cards field should be of type List", List.class, cards.getType());
cards.setAccessible(true);
} catch (Exception e) {
fail("Your CardShoe class should still define a field named "cards"");
}
listGen = new ListGenerator();
testeeDecks = listGen.createNewList();
PlayingCard[] decks = PlayingCard.generate(1);
for (PlayingCard p : decks) {
testeeDecks.add(p);
}
testee = new CardShoe(testeeDecks);
testeeTooDecks = listGen.createNewList();
PlayingCard[] decksToo = PlayingCard.generate(4);
for (PlayingCard p : decksToo) {
testeeTooDecks.add(p);
}
testeeToo = new CardShoe(testeeTooDecks);
}

@Test
public final void testOneDeckCutAtTwelve() throws Exception {
String[] "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H",
"4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S",
"7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C",
"TC", "JC", "QC", "KC" };
testee.cutDeck(12);
Object newCards = cards.get(testee);
assertSame("cutDeck(12) should not allocate a new List for the cards field.", testeeDecks, newCards);
assertEquals("cutDeck(12) should not change the number of elements in cards when called with a complete deck of 52 cards",
52, testeeDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(12) did not correctly cut a complete deck of 52 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeDecks.get(i).toString(), oneCut[i],
testeeDecks.get(i).toString());
}
}

@Test
public final void testOneDeckCutAtFourty() throws Exception {
String[] "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C",
"6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D",
"9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH",
"QH", "KH", "AH", "2S" };
testee.cutDeck(40);
Object newCards = cards.get(testee);
assertSame("cutDeck(40) should not allocate a new List for the cards field.", testeeDecks, newCards);
assertEquals("cutDeck(40) should not change the number of elements in cards when called with a complete deck of 52 cards",
52, testeeDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(40) did not correctly cut a complete deck of 52 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeDecks.get(i).toString(), oneCut[i], testeeDecks.get(i).toString());
}
}

@Test
public final void testMultiDeckCutAtOneThirtyThree() throws Exception {
String[] "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S",
"8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC",
"JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD",
"AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S",
"4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C",
"7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D",
"TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH",
"KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2C",
"3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D",
"6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
"9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS",
"QS", "KS", "AS", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC",
"2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H",
"4H" };
testeeToo.cutDeck(133);
Object newCards = cards.get(testee);
assertSame("cutDeck(133) should not allocate a new List for the cards field when cards had 208 elements.", testeeDecks, newCards);
assertEquals("cutDeck(133) should not change the number of elements in cards when called with a 4 complete decks (so the size of cards was 208)",
208, testeeTooDecks.size());
for (int i = 0; i < oneCut.length; i++ ) {
assertEquals("cutDeck(133) did not correctly cut when cards had 208 cards. At index " + i + " expected " +
oneCut[i] + " but was " + testeeTooDecks.get(i).toString(), oneCut[i], testeeTooDecks.get(i).toString());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote