I am making a card related application.
Suits are ranked Clubs, Diamonds, Spades, Hearts
I'd like to order them like this:
Within the cards, Ace is the highest, and 2 is the lowest, so the standard Ace King Queen Jack order will be fine.
I'm trying to use the compareTo method.I don't understand it...at all.
I'm a visual learner, so code examples, like a step by step walkthrough of how to use the method would -really- help me out. It doesn't have to be related to my code, anything that I can study and try to implement by learning it visually will help at this point.
Here's my code so far. If someone could show me -where- exactly i should implement this, that would also help.
import java.util.Arrays;
public class PlayingCard implements Comparable {
// Class Constants
public static final int ACE = 1;
public static final int KING = 13;
public static final int QUEEN = 12;
public static final int JACK = 11;
public static final String SPADES = "spades";
public static final String CLUBS = "clubs";
public static final String HEARTS = "hearts";
public static final String DIAMONDS = "diamonds";
// Instance Variables
private int rank;
private String suit;
// Constructor
public PlayingCard () {
this.rank = PlayingCard.QUEEN;
this.suit = PlayingCard.SPADES;
}
public PlayingCard (int rank, String suit) {
this.rank = rank;
this.suit = suit;
Arrays.sort();
}
// Mutators
public void setRank (int rank) {
this.rank = rank;
}
public void setSuit (String suit) {
this.suit = suit;
}
// Accessors
public int getRank () {
return this.rank;
}
public String getSuit () {
return this.suit;
}
public String toString () {
return PlayingCard.rankToString(this.rank) + " of " + this.suit;
}
//Class Method
public static String rankToString(int rank) {
switch (rank) {
case(1): return "Ace";
case(2): return "two";
case(3): return "three";
case(4): return "four";
case(5): return "five";
case(6): return "six";
case(7): return "seven";
case(8): return "eight";
case(9): return "nine";
case(10): return "ten";
case(11): return "Jack";
case(12): return "Queen";
case(13): return "King";
}
return "INVALID";
}
public static void main(String [] args) {
// Generate an array of playing cards
PlayingCard [] deck = new PlayingCard[52];
String [] suits = {PlayingCard.CLUBS, PlayingCard.DIAMONDS, PlayingCard.SPADES, PlayingCard.HEARTS};
for(int i = 0; i < suits.length; i++) { // Run through each suit
for (int j = 0; j < 13; j++) { // Make each card
deck[i*13 + j] = new PlayingCard(j+1, suits[i]);
}
}
// Shuffle cards
for(int i = 0; i < deck.length; i++) {
int newPos = (int)(Math.random()*52);
PlayingCard temp = deck[i];
deck[i] = deck[newPos];
deck[newPos] = temp;
}
// Print out cards
System.out.println("Shuffled Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}
// Sort the deck
Arrays.sort(deck);
// Print out cards
System.out.println("
Sorted Deck");
for(int i = 0; i < deck.length; i++) {
System.out.println(deck[i]);
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…