Vous êtes sur la page 1sur 5

APCS

DeckofCards Class Programming Assignment


(Lesson 6 includes Sorting, Arrays and ArrayLists)

For this assignment you are to write a class to represent a deck


of cards. Your class should be named DeckOfCards.
Cards in the DeckOfCards class will be Card objects that come from the Card class which is
given to you, but is incomplete. The Card class implements the comparable interface . You will
have to write the compareTo method for the Card class which will complete the Card class.
Cards are compared by their card value (see below).
The Card class is VERY well documented. Please read through the code and documentation before
proceeding with this assignment.
A Card object has two instance variables. The first instance variable cardDisplay is a twocharacter string that will represent how the card is displayed. The first character is the character
value of the card, the second character is the suit of the card.
Card character values range from 2 (lowest) to Ace (highest). The character values 2-9 can be
represented by the respective digit, the ten will be represented by a 'T', the Jack by a 'J', the queen
by a 'Q', the king by a 'K' and the Ace by an 'A'. Here are some example cardDisplay values
and their representations:
cardDisplay
AS
TD
4C
2H
QC

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.

Sample Run of CardTest.java


The unshuffled deck:
2S 3S 4S 5S
6S 7S 8S 9S
TS JS QS KS
AS 2H 3H 4H
5H 6H 7H 8H
9H TH JH QH
KH AH 2D 3D
4D 5D 6D 7D
8D 9D TD JD
QD KD AD 2C
3C 4C 5C 6C
7C 8C 9C TC
JC QC KC AC
The shuffled deck:
7H KC 5C 2H
AH 6C 8S 4H
JH KH JD 3D
5S AD JC TC
TH TD 3S 8H
7C QD AS KS
4S 5H AC 4D
9D 6D 7D 9C
3C QS 9H 5D
TS 2S 8D QH
QC 6H 7S 8C
2D 2C KD JS
6S 3H 9S 4C
Hand 1 - array of cards - unsorted:
7H KC 5C 2H AH 6C 8S 4H JH KH JD 3D 5S AD JC TC TH TD 3S 8H 7C QD AS KS 4S 5H

Hand 1 - array of cards sorted using Selection Sort:


AH AD AS KH KC KS QD JH JD JC TC TH TD 8H 8S 7H 7C 6C 5C 5S 5H 4H 4S 3S 3D 2H
Hand 2 - ArrayList of cards unsorted:
AC 4D 9D 6D 7D 9C 3C QS 9H 5D TS 2S 8D QH QC 6H 7S 8C 2D 2C KD JS 6S 3H 9S 4C

// Card.java - Card class - card object


// This file is in the class account
// ***************************************************************
public class Card implements Comparable
{
// display string for a card
// all cards are represented by 2 character strings.
// Strings are all caps, 2nd letter is the suit, first is the face val
// numeric values use numbers, ten thru ace use T, J, Q, K, and A
// respectively
// Some examples:
AS = ace of spades
//
TD = ten of diamonds
//
5H = five of hearts
//
2C = two of clubs
private String cardDisplay;
private int cardValue;
// The value of the card, ace higher than king
// king higher than queen,
// queen higher than jack etc.
// precondition: 2 character string representing a valid card
// postcondition: assigns string parameter to cardDisplay instance var
//
sets the cardvalue appropriately.
public Card(String card)
{
cardDisplay = card;
setCardValue();
}
// setCardValue()
// precondition: none
// postcondition: uses cardDisplay string to set card value:
public void setCardValue()
{
char c = cardDisplay.charAt(0);
if (c == 'A')
cardValue =
else if (c == 'K')
cardValue =
else if (c == 'Q')
cardValue =
else if (c == 'J')
cardValue =
else if (c == 'T')
cardValue =
else
// use char
cardValue =

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
}

Vous aimerez peut-être aussi