Page 531 - Introduction to Programming with Java: A Problem Solving Approach
P. 531

                12.10 Design Practice with Card Game Example 497
call to addCard with the groupOfCards calling object. Isn’t the inheritance code nicer where you don’t have to worry about all that? In particular, you can call addCard directly (no groupOfCards calling object required), and that results in more readable code. By the way, the addCard method is defined in the GroupOfCards class. With inheritance, the fact that it’s in a separate class from Deck is transparent. In other words, you call it from the Deck constructor the same way that you would call any other Deck method—without a calling object.
For some class pairs (like Deck and GroupOfCards), it’s legal to use either an inheritance or a composition relationship. But it’s never OK to use both inheritance and composition for the same feature. What would happen if Deck declared a GroupOfCards local variable and Deck also inherited from a GroupOfCards class? Deck objects would then contain two separate groups of cards and that’s wrong!
At this point, you might want to go back to Figure 12.17’s preliminary UML class diagram and add some more detail. We didn’t bother with constants or constructors in Figure 12.17’s class diagram. In working with the Deck class skeleton (see Figure 12.18), it’s now clear that there’s a need to (1) add a TOTAL_CARDS constant to the Deck class, (2) add a constructor to the Deck class, and (3) add a construc- tor to the Card class. For practice we encourage you to update Figure 12.17’s class diagram with these changes in mind. If you don’t feel like it, that’s OK; our main point here is to make you aw
nature of the program design process. Try to organize your thoughts as clearly as
possible up front, but be prepared to adjust those thoughts later on.
Code to Get You Started
Once you’ve finished with the card game’s class diagram, normally the next step would be to implement the
     are of the iterative
Designing is a gradual process.
   classes with Java code. We won’t bother to show class implementation details, but we would like to show
Apago PDF Enhancer
you how the suggested classes might be driven by a main method. Having followed proper OOP design guidelines, it’s easy to produce an elegant main method—see Figure 12.20. Note how short and under- standable the main method is. Yeah!
Another example will further illustrate how the finished classes might be used by the rest of the pro- gram. See main’s call to playAGame in Figure 12.20. Figure 12.21 shows a partial implementation for the
 public static void main(String[] args)
{
}
// end main
Scanner stdIn = new Scanner(System.in);
String again;
Game game;
do
{
}
game = new Game();
game.playAGame();
System.out.print("Play another game (y/n)?: ");
again = stdIn.nextLine();
while (again.equals("y"));
Figure 12.20 Card Game program’s main method





































































   529   530   531   532   533