Think about a deck of cards. How would you describe a deck of cards? When you play card games, what kind of operations should the deck be able to do?
Think about a deck of cards. How would you describe a deck of cards? When you play card games, what kind of operations should the deck be able to do?
Make a class in your package called Deck and another called DeckTester. Copy/paste the code below into it. Take a moment to familiarize yourself with the Deck code. Especially note its constructor's parameters and its private instance variables.
ALGORITHM 1: since the cards are in an ArrayList, you can call the method that removes and returns an object at a specified index. The choice does not have to be random, since we will be shuffling the cards. Removing the last card is more efficient than removing the first card (because, behind the scenes, every card has to be shifted up a space). You should also create a separate list where you keep track of all the discarded cards.
ALGORITHM 2: It's more efficient to leave all the cards in the original list. Instead of removing the card, just return the card at 'size' and then decrease 'size' by 1. This is perhaps less like true life, but will work better.
2. In DeckTester, make three Deck objects and test out each method.
Questions Answer these in your google doc!
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 *** */
}
/**
* 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 *** */
}
/**
* 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 *** */
}
/**
* 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 *** */
}
/**
* Generates and returns a string representation of this deck.
* @return a string representation of this deck.
*/
@Override
public String toString() {
String rtn = "size = " + size + "\nUndealt cards: \n";
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 + "\n";
}
}
rtn = rtn + "\nDealt cards: \n";
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 + "\n";
}
}
rtn = rtn + "\n";
return rtn;
}
}
/**
* 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) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
}
}