-
Notifications
You must be signed in to change notification settings - Fork 0
/
Card.java
70 lines (65 loc) · 1.9 KB
/
Card.java
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 exam01;
public class Card {
//----------------------------------------------------------------------------------------
//the following two arrays and three helper methods are provided, do NOT change them!
private final String[] faces = {
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"
};
private final String[] suits = {
"Clubs",
"Diamonds",
"Hearts",
"Spades"
};
private char suit;
private int face;
public Card(int f, char s) {
face = f;
suit = s;
}
public char getSuit() {
return suit;
}
public int getFace() {
return face;
}
//grabs the face associated with the number passed in by the user.
//for instance, 1 -> "Ace" and 13 -> "King", 2 -> "Two".
public String faceString() {
return faces[face - 1];
}
//grabs the suit associated with the char passed in by the user. for instance,
//'c' -> "Clubs", 'd' -> "Diamonds", 'h' -> "Hearts", 'd' -> "Spades"
public String suitString() {
String retVal = "";
for (String str: suits) {
if (str.toLowerCase().charAt(0) == suit || str.charAt(0) == suit) retVal = str;
}
return retVal;
}
//shifts face value to the right num times.
//for instance, if num = 2 and the currentFace is an "Ten" (ie 10) then we
//shift to the right by 2 and the shiftedFace is "Queen" (ie 12)
public int shiftedFace(int num) {
int shiftedFace = face + num;
while (shiftedFace > 13) shiftedFace -= 13;
while (shiftedFace <= 0) shiftedFace += 13;
return shiftedFace;
}
public String toString() {
return faceString() + " of " + suitString();
}
}
//----------------------------------------------------------------------------------------