-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
66 lines (58 loc) · 1.56 KB
/
Copy pathCard.java
File metadata and controls
66 lines (58 loc) · 1.56 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
/**
* Think Java v.6 - Exercise 12.1
*
* Encapsulate the deck-building code from Section 12.6 in a method called
* makeDeck that takes no parameters and returns a fully-populated array of
* Cards.
*
* @author Unai de la O
*/
public class Card {
public static final String[] RANKS = {
null, "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
public static final String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"};
private final int rank;
private final int suit;
/**
* Constructs a card of the given rank and suit.
*/
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
/**
* Returns a string representation of the card.
*/
public String toString() {
return RANKS[this.rank] + " of " + SUITS[this.suit];
}
/**
* Make an array of 52 cards.
*/
public static Card[] makeDeck() {
Card[] cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card(rank, suit);
index++;
}
}
return cards;
}
/**
* Displays the given deck of cards.
*/
public static void printDeck(Card[] cards) {
for (int i = 0; i < cards.length; i++) {
System.out.println(cards[i]);
}
}
// MAIN
public static void main(String[] args) {
Card[] baraja = makeDeck();
printDeck(baraja);
}
}