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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package lab08;
/**
* Class representing a playing card with possible rank
* 1 through 13 and four possible suits.
*/
public class Card
{
/**
* Constants for the four suits.
*/
public enum Suit
{
CLUBS, DIAMONDS, HEARTS, SPADES
};
/**
* Suit for this card.
*/
private final Suit suit;
/**
* Rank for this card.
*/
private final int rank;
/**
* Names used for displaying strings.
*/
private final String[] NAMES =
{
"Dummy", // element 0 not used
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"
};
/**
* Constructs a card with the given rank and suit. Ranks are
* assumed to be in the range 1 through 13.
* @param givenRank
* rank for this card
* @param givenSuit
* suit for this card
*/
public Card(int givenRank, Suit givenSuit)
{
rank = givenRank;
suit = givenSuit;
}
/**
* Returns the rank for this card.
* @return
* rank for this card
*/
public int getRank()
{
return rank;
}
/**
* Returns the suit for this card.
* @return
* suit for this card
*/
public Suit getSuit()
{
return suit;
}
/**
* Returns a String representation of this card.
*/
public String toString()
{
return NAMES[rank] + " of " + suit;
}
/**
* Returns a String representation of a given array of cards.
* @param arr
* array of Card objects
* @return
* a String representation of the given array
*/
public static String toString(Card[] arr)
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; ++i)
{
if (i > 0)
{
// every element after first one has comma before it
sb.append(", ");
}
sb.append(arr[i]);
}
sb.append("]");
return sb.toString();
}
}