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 do NOT 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

If doing [EC], also see how many tries before a full house comes up.

At the end of a program, re-shuffle and then print the whole deck.


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.