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).
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 Spades Queen of Spades Ace of Hearts 2 of Spades In poker order: 2 of Hearts 2 of Spades 4 of Clubs Queen of Spades Ace of Clubs Ace of Hearts Ace of Spades
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.
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.
Standards (READ ME)
See the Style Guide, available from the General Notes section of our course web site. All elements of beautiful, clear code style described there will be expected.
Your code must be syntactically and semantically correct, which means that it has to compile successfully. Any program that cannot be successfully compiled due to syntax or semantic errors will receive no credit.
Naturally, your code must also be behaviorally correct, which means that it should print the right output in the right format. However, partial credit will be given for partial solutions. At a minimum, be sure to test on the sample input, above
John H. E. Lasseter