Page 540 - Introduction to Programming with Java: A Problem Solving Approach
P. 540
506
Chapter 12 Aggregation, Composition, and Inheritance
14.
Assume you have written methods for a Card class and a GroupOfCards class. Assume that the addCard method increments currentSize after adding the input card to the end of the currently filled part of the cards array. Assume that the removeCard method retrieves a reference to the card at index in the cards array, decrements the currentSize of the cards array, shifts all array elements above index down by one place, and returns the reference to the card originally at index.
Toshufflethedeck,useaforloopthatstartswithunshuffled = getCurrentSize()
and steps down to one. In each iteration, use Math.random to pick an index in the unshuffled range, remove the card at that index, and then add it to the high end of the array. Include all that functionality in a Deck.shuffle method.
Extra credit:
Write Java code that tests your shuffle method. To do that, you’ll need to implement all of the classes and methods in the above UML class diagram. Your main method should instantiate a deck, display it, shuffle it, and display it again.
[after §12.10] First part of Game class for game of Hearts:
The Game class introduced in the text contained a deck and exactly two players. Improve that Game class by including an array of type Trick[] and a numberOfTricks variable that keeps track of the number of tricks played so far. Include a final instance variable called PLAYERS, that will be initialized in a constructor whose parameter value is the number of players participating in the game. Replace the indi- vidual player1 and player2 instance variables by instances in an array of type Hand[]. Include two boolean instance variables, hearts and queenOfSpades, whose values switch from false to true whenever the first heart or the queen of spades is played.
Write Java code that defines that part of the Game class that includes the class heading, the instance Apago PDF Enhancer
variable declarations, and the one-parameter custom constructor whose parameter is the number of players. This constructor should instantiate a Hand array with a length equal to the number of players. It should instantiate individual Hand objects for each player, using a two-parameter Hand constructor. The first parameter is the player number, starting with 0. The second parameter is the maximum number of cards the player will receive, which depends on the total number of cards in the deck and the number of players. The game constructor should also instantiate a Trick array, but not populate it with any individual tricks.
Review Question Solutions
1. A * on a UML diagram means the multiplicity can be “any number.”
2. In a UML diagram, a solid diamond is placed on an association line next to a containing class in a composition association. It indicates that the containing class exclusively contains the class that’s at the other end of the association line.
3. Putting common code from two classes into a superclass is one example of code reusability. Code reusability can also take place when you want to add a significant chunk of functionality to an existing class, and you implement the solution with a new subclass.
4. Two synonyms for a superclass—parent class, base class.
5. Two synonyms for a subclass—child class, derived class.
6. To tell the compiler that a class is derived from another class, you write extends <other-class> at the end of your new class’s heading line.
7. True. An instance of a subclass includes that class’s instance variables and its ancestors’ instance variables.