// Banner: Card class for representing individual cards in a deck. import java.util.*; public class Card implements Comparable<Card> { private int suit; // Encodes suit (1-4: Clubs, Diamonds, Spades, Hearts) private int rank; // Encodes rank (1-13: Ace through King) public Card(int s, int r) { this.suit = s; this.rank = r; } public int getSuit() { return suit; } public int getRank() { return rank; } @Override public int compareTo(Card c) { if (this.rank == c.rank) { return this.suit - c.suit; } return this.rank - c.rank; } @Override public String toString() { String[] suits = { "Clubs", "Diamonds", "Spades", "Hearts" }; String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; return ranks[this.rank - 1] + " of " + suits[this.suit - 1]; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter