next up previous
Next: Add some new methods Up: Assignment 11: Decks as Previous: Reading

Create a Deck object

This assignment is really a continuation of the last assignment, so you should finish that before you proceed. If you did not finish the last assignment, you should use my solutions to get caught up and proceed from there.

You might want to make a copy of your solution to the last assignment before you start modifying it.

1.
Open the Card project and create a new file called Deck.java. This file will contain the class definition for the Deck class. Roaster should add the new file to your project automatically, but you might have to add it using the Add File command under the project menu.
2.
Write a class definition for Deck. It should have a single instance variable, an array of Cards named cards.

3.
Write two constructors for Deck. One should take no arguments and build a complete 52-card deck (just like newDeck), allocating both the array and the individual cards. The other should take an integer as an argument and allocate an array with the given length, but it should not allocate any cards.

4.
Look at the methods that currently belong to the Card class and consider which of them should really be written as methods of the Deck class. Convert those methods so that instead of being class (static) methods of the Card class, they become object methods of the Deck class.

In this case, it is fairly obvious what class each method belongs to, but there are often cases where it is not so clear. For example, imagine a method that takes a Card and a Deck as arguments, and that returns the location (index) of the cards in the deck. Should this method be an instance method or a class method? Should it belong to the Card class or the Deck class, or both?

5.
For the methods where you changed from a class method to an object method, you will also have to make changes at the places where the method is invoked. Make the appropriate changes and test the program to see that it does the same thing it did before.


next up previous
Next: Add some new methods Up: Assignment 11: Decks as Previous: Reading
Allen B. Downey
4/22/1998