- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCard.java
More file actions
Latest commit
99 lines (89 loc) · 1.95 KB
/
Copy pathCard.java
File metadata and controls
99 lines (89 loc) · 1.95 KB
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
importjava.util.*;
/**
* A card from a card deck with a nbr, a suit and a text description.
*/
classCard {
protectedintnbr;
protectedintsuit;
/**
* Constructor
*
* @param nbr the number of the card, must be in range 1-13
* @param suit the suit of the card, must be in the range 1-4 where
* hearts = 1, spades = 2, diamonds = 3, clubs = 4
*/
publicCard (intnbr, intsuit) {
if (nbr >= 1 && nbr <= 13) {
this.nbr = nbr;
} else {
thrownewIllegalArgumentException("Nbr must be between 1 and 13.");
}
if (suit >= 1 && suit <= 4) {
this.suit = suit;
} else {
thrownewIllegalArgumentException("Suit must be between 1 and 4.");
}
}
/**
* Get the number of the card.
*
* @return an int representing the number of the card
*/
publicintgetNbr() {
returnnbr;
}
/**
* Get the suit of the card:
* hearts = 1, spades = 2, diamonds = 3, clubs = 4.
*
* @return an int representing the suit of the card
*/
publicintgetSuit() {
returnsuit;
}
/**
* Create a String description of the card on the following form:
* "6 of clubs" and "Jack of hearts".
*
* @return a String describing the card
*/
publicStringtoString() {
StringBuildercard = newStringBuilder();
switch (nbr) {
case1:
card.append("Ace");
break;
case11:
card.append("Jack");
break;
case12:
card.append("Queen");
break;
case13:
card.append("King");
break;
default:
card.append(nbr);
break;
}
card.append(" of ");
switch (suit) {
case1:
card.append("hearts");
break;
case2:
card.append("spades");
break;
case3:
card.append("diamonds");
break;
case4:
card.append("clubs");
break;
default:
/* Impossible case */
break;
}
returncard.toString();
}
}