Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package lab08;
import java.util.Random;
import lab08.Card.Suit;
/**
* Class representing a standard 52-card deck of playing
* cards from which cards can be selected at random.
*/
public class Deck
{
/**
* The cards comprising this deck.
*/
private Card[] cards;
/**
* The random number generator to use for selecting cards.
*/
private Random rand;
/**
* Constructs a new deck with a default random number generator.
*/
public Deck()
{
rand = new Random();
init();
}
/**
* Constructs a new deck with the given random number generator.
*/
public Deck(Random givenGenerator)
{
rand = givenGenerator;
init();
}
/**
* Returns a new array containing k elements selected
* at random from this deck.
*/
public Card[] select(int k)
{
// TODO
return null;
}
/**
* Initializes a new deck of 52 cards.
*/
private void init()
{
cards = new Card[52];
int index = 0;
for (int rank = 1; rank <= 13; ++rank)
{
cards[index] = new Card(rank, Suit.CLUBS);
index += 1;
cards[index] = new Card(rank, Suit.DIAMONDS);
index += 1;
cards[index] = new Card(rank, Suit.HEARTS);
index += 1;
cards[index] = new Card(rank, Suit.SPADES);
index += 1;
}
}
}