Okay, we have two methods left. You may have wondered about the constructor though. Well, in this example, our constructor is unnecessary. The only field inside the “Hand” class is our dynamic array and when we create a hand it doesn’t need anything in it, so for this class our constructor can remain blank.
busted()
I should really let you have a go at making this method yourself, it’s pretty simply really, all we need to do is return true if the value of our hand is greater than 21 and false if it isn’t. Just remember that to get the value of our hand we can simply use the “handValue” method we just created, if you really get stuck post a question below.
showHand()
So our final method for the Hand class is a bit of a tricky one. Not so much programatically but it involves thinking like an Object-Oriented programmer. We need to consider how you would show a player’s hand as well as the dealer’s hand. Remember that the dealer’s hand only shows one card at the start of the game. So with this in mind, let’s use the following code:
String theHand = " [ ...hidden... ]\n"; if (cardOne == true) { theHand = " [ " + cardsInHand.get(0).printCardVal() + " ]\n"; } for (int i = 1; i < cardsInHand.size(); ++i) { theHand=theHand+" [ "+cardsInHand.get(i).printCardVal()+" ]\n"; } return theHand;
*Apologies for the compaction of the line inside the for loop.
So this is going to print the hand out in a manner that I found aesthetically pleasing, if you decide you want to print it differently then feel free to alter “theHand” appropriately.
And that’s it! Feel free to critique the coding used and the incompleteness if you want but remember the important thing in this tutorial is to understand Object-Oriented programming!