Homework: Arrays

In chapter 7, the Deitel & Deitel text gives example code for classes Card and DeckOfCards. Put these classes in a project, but do not use final for the instance vars in Card. Add to these classes all the methods that we know a real class should have. The example code in 7.11 gives one nice way to approach the formatting for the toString in DeckOfCards, but remember that toStrings return, they don't print.

Create a new class HandOfCards which has an array of Cards, and a firstEmpty. Give it a method addCard which adds a card to the hand if there is still room. You should have at least one parameterized constructor that lets you set up the max size of the hand. Add methods to this class:

You can add other methods to these three classes that you find useful to do the following main, but remember that you shouldn't give direct access to the arrays from outside the class.

In a main class, ask the user how many players, create an array of that many HandofCards, and use a shuffled DeckofCards to deal them one card at a time using addCard, but stop and announce a winner for the first hand to get a pair, or stop and announce a draw if the deck runs out with no winner. (By stop, I mean do not keep dealing, even if it is the middle of a round).

Later in the same main, repeatedly re-shuffle and re-deal 5-card hands to an array of 5 players and count how many tries it takes until a flush comes up [EC2] also see how many tries before a full house comes up.

[EC1]Later in the same main, get a new number of players from the user and deal them two cards each from a shuffled DeckofCards. Show the 0th hand to the user, and ask if they want to stand or hit. As long as they choose hit and the sum of their cards is < 21, deal everyone another card. If the user goes over 21, tell them they lost. If they stand, compute the sum of each of the hands and show the winner, which is the hand with the highest sum without going over. If this is the user's hand, congratulate them.


hint: the main program is dealing randomly, but you can manually create cards and add them to a hand in a test harness to test that your methods work.