|
1 | | -import java.util.ArrayList; |
2 | | -import java.util.Random; |
3 | | - |
4 | | -/** |
5 | | - * A collection of playing cards. |
6 | | - */ |
7 | | -public class CardCollection { |
8 | | - |
9 | | - private String label; |
10 | | - private ArrayList<Card> cards; |
11 | | - |
12 | | - /** |
13 | | - * Constructs an empty collection. |
14 | | - */ |
15 | | - public CardCollection(String label) { |
16 | | - this.label = label; |
17 | | - this.cards = new ArrayList<Card>(); |
18 | | - } |
19 | | - |
20 | | - /** |
21 | | - * Returns the label of the card collection. |
22 | | - */ |
23 | | - public String getLabel() { |
24 | | - return label; |
25 | | - } |
26 | | - |
27 | | - /** |
28 | | - * Adds the given card to the collection. |
29 | | - */ |
30 | | - public void addCard(Card card) { |
31 | | - cards.add(card); |
32 | | - } |
33 | | - |
34 | | - /** |
35 | | - * Removes and returns the card with the given index. |
36 | | - */ |
37 | | - public Card popCard(int i) { |
38 | | - return cards.remove(i); |
39 | | - } |
40 | | - |
41 | | - /** |
42 | | - * Removes and returns the last card. |
43 | | - */ |
44 | | - public Card popCard() { |
45 | | - int i = size() - 1; |
46 | | - return popCard(i); |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Returns the number of cards. |
51 | | - */ |
52 | | - public int size() { |
53 | | - return cards.size(); |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * True if the collection is empty, false otherwise. |
58 | | - */ |
59 | | - public boolean empty() { |
60 | | - return cards.size() == 0; |
61 | | - } |
62 | | - |
63 | | - /** |
64 | | - * Moves n cards from this collection to the given collection. |
65 | | - */ |
66 | | - public void deal(CardCollection that, int n) { |
67 | | - for (int i = 0; i < n; i++) { |
68 | | - Card card = popCard(); |
69 | | - that.addCard(card); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * Moves all remaining cards to the given collection. |
75 | | - */ |
76 | | - public void dealAll(CardCollection that) { |
77 | | - int n = size(); |
78 | | - deal(that, n); |
79 | | - } |
80 | | - |
81 | | - /** |
82 | | - * Returns the card with the given index. |
83 | | - */ |
84 | | - public Card getCard(int i) { |
85 | | - return cards.get(i); |
86 | | - } |
87 | | - |
88 | | - /** |
89 | | - * Returns the last card. |
90 | | - */ |
91 | | - public Card last() { |
92 | | - int i = size() - 1; |
93 | | - return cards.get(i); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Swaps the cards at indexes i and j. |
98 | | - */ |
99 | | - public void swapCards(int i, int j) { |
100 | | - Card temp = cards.get(i); |
101 | | - cards.set(i, cards.get(j)); |
102 | | - cards.set(j, temp); |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Randomly permute the cards. |
107 | | - */ |
108 | | - public void shuffle() { |
109 | | - Random random = new Random(); |
110 | | - for (int i = size() - 1; i > 0; i--) { |
111 | | - int j = random.nextInt(i); |
112 | | - swapCards(i, j); |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * Returns a string representation of the card collection. |
118 | | - */ |
119 | | - public String toString() { |
120 | | - return label + ": " + cards.toString(); |
121 | | - } |
122 | | - |
123 | | -} |
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Random; |
| 3 | + |
| 4 | +/** |
| 5 | + * A collection of playing cards. |
| 6 | + */ |
| 7 | +public class CardCollection { |
| 8 | + |
| 9 | + private String label; |
| 10 | + private ArrayList<Card> cards; |
| 11 | + |
| 12 | + /** |
| 13 | + * Constructs an empty collection. |
| 14 | + */ |
| 15 | + public CardCollection(String label) { |
| 16 | + this.label = label; |
| 17 | + this.cards = new ArrayList<Card>(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Returns the label of the card collection. |
| 22 | + */ |
| 23 | + public String getLabel() { |
| 24 | + return label; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Adds the given card to the collection. |
| 29 | + */ |
| 30 | + public void addCard(Card card) { |
| 31 | + cards.add(card); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Removes and returns the card with the given index. |
| 36 | + */ |
| 37 | + public Card popCard(int i) { |
| 38 | + return cards.remove(i); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Removes and returns the last card. |
| 43 | + */ |
| 44 | + public Card popCard() { |
| 45 | + int i = size() - 1; |
| 46 | + return popCard(i); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the number of cards. |
| 51 | + */ |
| 52 | + public int size() { |
| 53 | + return cards.size(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * True if the collection is empty, false otherwise. |
| 58 | + */ |
| 59 | + public boolean empty() { |
| 60 | + return cards.size() == 0; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Moves n cards from this collection to the given collection. |
| 65 | + */ |
| 66 | + public void deal(CardCollection that, int n) { |
| 67 | + for (int i = 0; i < n; i++) { |
| 68 | + Card card = popCard(); |
| 69 | + that.addCard(card); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Moves all remaining cards to the given collection. |
| 75 | + */ |
| 76 | + public void dealAll(CardCollection that) { |
| 77 | + int n = size(); |
| 78 | + deal(that, n); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the card with the given index. |
| 83 | + */ |
| 84 | + public Card getCard(int i) { |
| 85 | + return cards.get(i); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the last card. |
| 90 | + */ |
| 91 | + public Card last() { |
| 92 | + int i = size() - 1; |
| 93 | + return cards.get(i); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Swaps the cards at indexes i and j. |
| 98 | + */ |
| 99 | + public void swapCards(int i, int j) { |
| 100 | + Card temp = cards.get(i); |
| 101 | + cards.set(i, cards.get(j)); |
| 102 | + cards.set(j, temp); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Randomly permute the cards. |
| 107 | + */ |
| 108 | + public void shuffle() { |
| 109 | + Random random = new Random(); |
| 110 | + for (int i = size() - 1; i > 0; i--) { |
| 111 | + int j = random.nextInt(i); |
| 112 | + swapCards(i, j); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns a string representation of the card collection. |
| 118 | + */ |
| 119 | + public String toString() { |
| 120 | + return label + ": " + cards.toString(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments