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

This code has the start of a class representing a \"card shoe\" -- the actual na

ID: 3801730 • Letter: T

Question

This code has the start of a class representing a "card shoe" -- the actual name of the things that casino dealers use to help shuffle and deal cards.

package edu.sbu.cse114;

import java.util.List;

public class CardShoe {

private List cards;

public CardShoe(List originalList) {

cards = originalList;

}

public void shuffle(ListGenerator gen) {

List firstHalf = gen.createNewList();

List secondHalf = gen.createNewList();

}

}

For this problem, you will need to complete the shuffle()method by implementing the following algorithm:

/*

shuffle():

Move the first half of cards into firstHalf // This may or may not be done in one line of code

Move the remaining elements in cards into secondHalf // This may or may not be done in one line of code

While there are elements in firstHalf

   Remove the element at the start of firstHalf and add it to cards

   Remove the element at the start of secondHalf and add it to cards

EndWhile

If secondHalf has any elements remaining

   Remove the element at the start of secondHalf and add it to cards

EndWhile

*/

The classes required to complete this code are the following:

package edu.sbu.cse114;

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.
* @author Matthew Hertz
*/
public class ListGenerator {

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

package edu.sbu.cse114;

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;

}

}

Explanation / Answer

Since the type of list is unpredictable, we use the methods which are defined for all Lists for traversing, appending and clearing the lists. These methods are get(), add() and clear(). Following is the complete code for shuffle() method :

public void shuffle(ListGenerator gen) {
       List firstHalf = gen.createNewList();
       List secondHalf = gen.createNewList();
      
       // we make use of add to add cards to list
       // we use get to get card at particular index
       // we use clear to empty the card deck
       // these are common for both ArrayList and LinkedList
       int size = cards.size();
       int size1 = size/2;
       int size2 = size-size1;
      
       // distribute cards in first half
       for(int i=0;i<size1;i++)
       {
           firstHalf.add(cards.get(i));
       }
      
       // remaining cards in second half
       for(int i=size1;i<size;i++)
       {
           secondHalf.add(cards.get(i));
       }
       cards.clear(); // remove all cards
      
       // add cards back to pile one by one from both decks while i<size1
       for(int i=0;i<size1;i++)
       {
           cards.add(firstHalf.get(i));
           cards.add(secondHalf.get(i));
       }
      
       // remaining cards from second half while cards remain in secondHalf
       for(int i=size1;i<size2;i++)
       {
           cards.add(secondHalf.get(i));
       }
      
       // now both first and second half are empty
       firstHalf.clear();
       secondHalf.clear();
   }