-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
97 lines (84 loc) · 1.99 KB
/
Copy pathCard.java
File metadata and controls
97 lines (84 loc) · 1.99 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
/**
* Card.java
* class which models a card,
* to be used with PokerTest.java
*
* COMS W1004
* @author Trevor Rukwava (ttr2107)
*
* Friday, November 3, 2017
*/
public class Card implements Comparable<Card>{
private int suit; // uses integers 1-4 to encode the suit
private int rank; // uses integers 1-13 to encode the rank
public Card(int s, int r){
//constructor makes a card with suit s and value v
suit = s;
rank = r;
}
public int compareTo(Card c){
//this method to compares cards so they
// may be easily sorted
if (c.rank < rank){
return 1;
}
if (c.rank > rank){
return -1;
}
if (c.rank == rank && c.suit < suit){
return 1;
}
if (c.rank == rank && c.suit > suit){
return -1;
}
if (c.rank == rank && c.suit == suit){
return -1;
}
else {
return 0;
}
}
public String toString(){
//this method easily prints a Card object
String cardName = "";
String cardSuit = "";
String cardRank = "";
if (suit==1){
cardSuit= "Clubs";
}
if (suit==2){
cardSuit= "Diamonds";
}
if (suit==3){
cardSuit= "Hearts";
}
if (suit==4){
cardSuit= "Spades";
}
if (rank==1){
cardRank= "Ace";
}
else if (rank==11){
cardRank= "Jack";
}
else if (rank==12){
cardRank= "Queen";
}
else if (rank==13){
cardRank= "King";
}
else {
cardRank = ""+rank;
}
cardName = cardRank+ " of "+ cardSuit;
return cardName;
}
public int getRank(){
//returns the card's rank
return rank;
}
public int getSuit(){
//returns the card's suit
return suit;
}
}