A Java demo turned in as a final project for the RevUp course of Revature. Cuts and shuffles a virtual deck of cards and displays what order they end up in.
The following is taken from comments in the program code and a description of this program posted on Discord in the RevUp #homework-help group.
Cards are set by (rank, suit) int pairs where rank is between 1 and 13 inclusive (1 is the Ace, 11 is the Jack, 12 is the Queen, 13 is the King, and 2 through 10 are their respective number ranks) and suit is one of 0 for Spades, 1 for Hearts, 2 for Clubs, or 3 for Diamonds. The only exceptions to these rules are the pairs (0, 4) for a blank card, (1, 4) for the Minor Joker, (2, 4) for the Major Joker, and (3, 4) for the Title card. Any other int pairs are invalid, will cause a BadRankException, a BadSuitException, or a BadSuitlessException in the case of a card with suit 4, but rank that is not one of 0, 1, 2, or 3, to be thrown, and if an instance of Card with such an int pair calls its toString() method, it will return "an erroneous card".
There is a class Card that has the rank (number) and suit as int variables. An abstract class Shuffleable is configured as a collection of cards (LinkedList) and has concrete methods for drawing single Card object from the top, bottom, or nth Card from the top, as well as placing a Card on the top of a new pile or the bottom of it. The method deepClone() copies the collection Card by Card from the Shuffleable object fed into the parameter to 'this.' The show() method draws every Card in the Shuffleable object and displays what it is on the screen.
There are abstract methods for making a cut into two Shuffleable objects out of one (one is 'this' and the other is returned) and then 3 methods for uniting two Shuffleable objects (one is 'this' and the other one is fed in as a parameter) into one: uncut(), shuffleUp(), and shuffleDown(). Next, the PileOfCards class extends the Shuffleable class and concretely implements all the abstract methods in Shuffleable by iterating through the single draw and place methods in the Shuffleable class itself. Finally, the DeckofCards class extends the PileOfCards class and has a method to create a fresh deck of cards in order. The main() method is pretty trivial, asking the user how many Jokers they want in the deck, how many times to randomize the cards (0 to 9), and how to randomize them: cutting and uncutting only, cutting and shuffling upward, cutting and shuffling downward, or a combination of all of these techniques (cut, uncut, cut again, shuffle upward, cut one more time, and shuffle downward).
The author noticed that cutting and uncutting (reassembling) the DeckOfCards instance several times more often than not resembled the effect of only cutting and uncutting once. The PileOfCards objects were reviewed step by step and this was fonfirmed. The author mused about perhaps attempting this physically with a real deck of cards. The solution to achieve more randomness from only cutting and uncutting was to cut() the whole deck, then cut() the pile left in 'this,' then uncut() those (all cards that were formerly in the bottom are now on top) and then uncut() the new 'this' with the Shuffleable object that came out of the first cut().
Many throw, try, and catch trios are used to check for every possible Exception the author could think of. The author thought perhaps he was a little over-meticulous about this. However, it helped very much with debugging.
The numbered list was accomplished with formatted printing (System.out.printf()). A format String is placed as the first parameter and then the information to be formatted right after. Like the cards were printed to the screen by using the equivalent of System.out.printf("%2d: %s", i, cardString); where i is the ordinal number the Card is found in the pile (PileOfCards) and cardString is the name of the Card. %2d is format code for a 2 digit decimal number and %s is format code for a String. The format code is a legacy of the original C programming language that Java is based on.