Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Spring 2015
Thursday Lab #9
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by the start of class on Friday, 04/17/2015

In this assignment, you will gain some practice with class definitions. You will begin with an example program that includes a convention for representing playing cards and several methods that operate on this representation. Your job is to transform this into a version that uses objects from a Card class, which you will complete from a skeleton distribution. For the most part, that involves moving method definitions around (there is very little in the way of hard algorithmic problem solving).

# Supporting Code

The printDeck source code is a complete program that reads input data representing the number of playing cards in a set, followed by a series of numbers between 1 and 52, representing those cards. The number convention for the cards is to group all cards of a suit together, then order by poker rank: 1 through 13 represent the Clubs suit (from 2 through 9, then jack, queen, king, and ace), 14 - 26 represent the Diamonds suit (same ranking within), 27 - 39 represent the Hearts, and 40 - 52 the Spades.

The program first prints out the cards (with rank and suit names) according to the order they were given, then sorts then (in poker ordering), and prints them again, in sorted order. There is no prompt for input, so only the input need be given.

For example, the input

7 3 13 27 52 50 39 40

represents seven cards, which will result in the output

7 cards.
4 of Clubs
Ace of Clubs
2 of Hearts
Ace of Hearts

In poker order:
2 of Hearts
4 of Clubs
Ace of Clubs
Ace of Hearts

Your job is not to write this program, as it already exists (see "Supporting Code", above). Rather, you are to refactor this code in order to complete the definition of a Card class, and modify the printDeck source code to use this class appropriately. Here's how:

• The Card class already has a field (value), representing the card according to the 1-52 scheme detailed above. It also has a constructor, and two static variables, ranks and suits, which store the rank and suit names.

• Move all of the methods in printDeck that are declared public (except for main()) to the Card class, and deleted the static modifier from each one.

• Several of these methods—cardToString(), rankName(), suitName(), rank(), and suit(), and compareTo()— take an int parameter, card. Delete this parameter, and replace references to in the method bodies with the field this.value. (This card value represents the card on which the method is computing a value, and for an instance method, we already have access to that information in the object's fields.)

• In the compareTo() method, there is a second int parameter, c2. Change the type of this parameter to Card, and modify the body of compareTo() to reflect this change.

• The rankName() and suitName() methods declare local variables ranks and suits. These declarations are redundant with the static class variables. Delete them, and replace occurrences of the variables ranks/suits with Card.ranks/Card.suits.

• Rename the cardToString() method to toString() (with exactly those letters, capitalization included)

You now have a Card class that is fully functional and ready for use. Now you'll need to modify printDeck to use it:

• The int array cards, defined in the main method represents the cards in the set. Now that we're representing a playing card with a Card object, modify the declaration of this variable, from int[] to Card[]. You'll also need to modify the code that reads each card value to reflect this change, by using the Card() constructor, instead of storing the raw int value in cards. No other changes to main() are necessary.

• The printCards(), sortPokerOrder(), and swap() all have a parameter, cards of type int[]. Change this type to Card[]. You'll need to modify the bodies of these methods accordingly. In particular, the calls to cardToSring() and compareTo will need to be redone.

You'll know you got everything right if, after making these changes, the program printDeck behaves the same way as the original version.

# Turn In

The source code for your completed source code files, printDeck.java and Card.java, in a subfolder of your turn-in directory called "lab9". Please also provide a paper copy of your work.