-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.java
69 lines (59 loc) · 1.6 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
public class Card implements Comparable<Card> {
protected int suit;
protected int rank;
private static final String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
private static final String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
/**
* No-argument constructor.
*/
public Card() {
this(0, 0);
}
/**
* Constructor with arguments.
*/
public Card(int suit, int rank) {
this.suit = suit;
this.rank = rank;
}
/**
* Prints a card in human-readable form.
*/
@Override
public String toString() {
return ranks[rank] + " of " + suits[suit];
}
/**
* Return true if the cards are equivalent.
*/
@Override
public boolean equals(Object obj) {
boolean b = false;
if (obj instanceof Card) {
Card that = (Card) obj;
b = this.compareTo(that) == 0;
}
return b;
}
public static void printCard(Card card) {
System.out.println(card);
}
/**
* Compares two cards: returns 1 if the first card is greater
* -1 if the seconds card is greater, and 0 if they are the equivalent.
*/
public int compareTo(Card that) {
// first compare the suits
if (this.suit > that.suit)
return 1;
if (this.suit < that.suit)
return -1;
// compare the ranks
if (this.rank > that.rank)
return 1;
if (this.rank < that.rank)
return -1;
return 0;
}
}