Question

The first class "cards" creates cards of different ranks and suits. It also includes a method to compare ranks of 2 cards.

public class card implements Comparable
    {
        private int suit;
    private int rank;

    public card(int card)
    {rank = card % 13;
        suit = card / 13;
    }

    public int getSuit()
    {return suit;}


    public int getRank()
    { return rank;}   


    public int compareTo(Object other)
    { 
        card c = (card) other;
        if (getRank() > c.getRank())
          return 1;
         else if (getRank() <c.getRank())
         return -1;
         return 0;
        }

    public String toString()
    {
        String[] ranks = {"2", "3", "4" , "5", "6", "7" , "8", "9" , "10", "Jack",       "Queen", "King",
            "Ace" }; 
        String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        return ranks[getRank()] + " of " + suits[getSuit()];
    }
}

The second class gives me 10 random cards in the hand1 ArrayList.

import java.util.Collections;
import java.util.ArrayList;
import java.util.Random;
public class Gin
{
    public static void main(String []args)
    {
        ArrayList<card> deck = new ArrayList<card>();

        Random r = new Random();
        for (int i = 0; i <52; i++)
          deck.add (new card(i));

          //deal 10 cards to a hand from the deck 

            ArrayList<card> hand1 = new ArrayList<card>();
            card c;
            for (int i=0; i<10;i++)
            { 
                int pickACard = r.nextInt (deck.size());
                 c = deck.remove(pickACard);
                 hand1.add(c);
                 System.out.println (c);

            }
            System.out.println ("  "); 
}
}

If the hand is not in order I'll use Collections.shuffle(hand1) to bogo sort.But how can I check if element 0 >= element 1 >= element 2>= element 3>= ect... in hand1? I tired writing a smaller portion of the code that will compare element 0 and 1 in hand1 below:

if(hand1.get(0).compareTo(hand1.get(1)== 1))
           System.out.print(hand1.get(0));
           else if (hand1.get(0).compareTo(1)==-1)
           System.out.print (hand1.get(1));
           else
            System.out.print ("Equal");

However I got the error "Incompatible types: card and int. Any help would be appreciated.

No correct solution

OTHER TIPS

if(hand1.get(0).compareTo(hand1.get(1)== 1))

Here you're comparing hand1.get(0), which is the first alement of the list of cards, and is thus a card, with hand1.get(1)== 1. What you want is

if (hand1.get(0).compareTo(hand1.get(1)) > 0)

(because a comparison can return any positive number to mean first > second).

else if (hand1.get(0).compareTo(1)==-1)

And not you're comparing hand1.get(0), which is the first alement of the list of cards, and is thus a card, with1`, which is an int. It doesn't make sense either. You want

else if (hand1.get(0).compareTo(hand1.get(1)) < 0)

Also, note that yoy should use generics for your class. It should be declared as a Comparable<card>, and not as a raw Comparable:

public class card implements Comparable<card>

    public int compareTo(card other) {
        ...
    }
}

And of course, it should also respect the Java naming conventions: classes start with an uppercase letter:

public class Card implements Comparable<Card>

    public int compareTo(Card other) {
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top