Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's an example for compareTo for the Integer class:

public int compareTo(Object o) {
    return this.value - ((Integer)o).value;
}

This is not the actual implementation, because if you are dealing with a small negative number and a large positive number, the result could overflow. However, this method is sufficient in many cases, including the OP's.

This subtracts the int value of this object and the int value of the other object. Think about why this works:

  • If the two numbers are equal, the subtraction yields 0, so the numbers are understood to be equal.
  • If this is greater than o, the subtraction yields a positive value, so this is understood to be greater.
  • If this is less than o, the subtraction yields a negative value, so o is understood to be greater.

To help with this numeric method, you should probably give the suits integer values ranked in the appropriate order instead of string values, and define ACE to be 14 instead of 1, because the ace is greater than the king, which is 13. With this, you can write a method that combines two comparing strategies:

public int compareTo(Object o) {
    PlayingCard other = (PlayingCard) o;
    int result = this.suit - other.suit;
    if (result != 0) 
        return result;
    return this.rank - other.rank;
}

This will first compare the suits, and if they are the same, it will compare the ranks.

You must place the compareTo method in the PlayingCard class, or it will not compile if you implement Comparable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...