APCS Deck of Cards Programming Assignment
APCS Deck of Cards Programming Assignment
represented card
Ace of Spades
Ten of Diamonds
Four of Clubs
Two of Hearts
Queen of Clubs
The second instance variable of the card class is an integer named cardValue that gives a
numeric value to each card for comparison sake. In the given Card class Aces have a numeric
value of 14, Kings 13, Queens 12 etc.
You will write the DeckOfCards class and a driver program named CardTest.java.
Your DeckOfCards class will have at least two instance variables (you can add more if you need
to). The first will be an array of Card objects (not an ArrayList), 52 in length representing the
deck of cards. The second, an integer, will represent the current position in the deck of cards. The
use of this instance variable will become apparent shortly.
DeckOfCard class should have at least the following methods, you will probably need more, but
you will need at least the following.
A constructor your constructor will have to build the deck. This is a bit more than our
constructors have done in the past. You can build the deck by "brute force" or with loops,
the choice is yours, but this method will take some work. Remember the deck is an array of
52 Card objects.
toString this method will be useful in debugging. It should print out an entire deck of
cards, 4 cards to a line (see the sample output).
a shuffle method this method should shuffle the deck.
Here is a pseudo-code for a shuffle routine:
initialize N to 51
while N >=0
pick a random number, K, between 0 and 51 inclusive
swap the Kth card and the Nth card in the deck
N-this routine will thoroughly shuffle the deck.
a deal1 method this method should simulated dealing ONE card off the top of the deck.
This will be the only deal method we have, if someone wants to deal more than one card,
they will call this method more than once. This method will modify the current position
instance variable and should return a Card object.
You will also have to write a driver program to test your DeckOfCards class called
cardTest.java. I have given you the output to my cardTest.java driver program, but
only the output. You will have to write the entire program yourself. You should make your driver
program produce identical output as to what's given.
Here's what cardTest.java should do:
1. Declare a DeckOfCards object called myDeck.
2. Print out the unshuffled myDeck
3. Shuffle myDeck
4. Print out the shuffled myDeck.
5. Declare an array of Card called hand1 that is of length 26.
6. Deal 26 cards from myDeck into the array hand1.
7. Print out the contents of hand1.
8. Sort hand1 by card value from highest to lowest using selection sort.
9. Print out the sorted contents of hand1.
10. Declare an ArrayList of cards named hand2.
11. Deal 26 cards from myDeck into hand2.
12. Print out the contents of hand2.
I have given you a little less information for this assignment. You are designing more and more of
each class. If you need any help don't hesitate to ask and I will give you a hand (no pun intended).
Here is a sample run from my driver program.
14;
13;
12;
11;
10;
arithmetic to set value
(int)c - '0';
}
public String toString()
{
return cardDisplay;
}
// compareTo
// as required by required by comparable interface
// you must complete this method
}