next up previous
Next: About this document ... Up: Assignment 11: Decks as Previous: Create a Deck object

Add some new methods

1.
Write a method called subDeck that is an object method of the Deck class. It should take two integers, low and high, and return a new Deck that contains all the cards in this from low to high (including both ends). Note that the new deck will probably not contain 52 cards. That's ok. This method should use the constructor of the Deck class to create the array of cards, but it should not allocate any card objects. Rather, the new deck will contain references to the same cards that are in the old deck. It is ok to do that because we are not going to change the values of any of the cards.

2.
Write a static (or class) method called merge that takes two decks as arguments and that creates and returns a new deck that contains all the cards from the two decks, in descending order.

You should assume that the two decks you are given are already sorted in desending order. What this means is that you can assemble the merged deck by repeatedly choosing the larger of the two cards on top of the decks. I will give a demonstration in class that will, hopefully, make this algorithm clear.

3.
In main, test your methods by creating a new Deck, shuffling it, creating two subdecks of it (not the same size), sorting the two subdecks using the sort method from last time, and then merging the two subdecks into a full, sorted deck.


next up previous
Next: About this document ... Up: Assignment 11: Decks as Previous: Create a Deck object
Allen B. Downey
4/22/1998