2

Word of caution, I only have one semester under my belt. Having asked a similar question only to be redirected to here, honestly I could not understand most of what was being said and it was not a giant help.

I made my code a little more full as to what is going on (I consolidated everything into a main method. Which I think should help a little.

Anyway I am getting this error

Exception in thread "main" java.lang.ClassCastException: lab13d.Card cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652)
at java.util.PriorityQueue.siftUp(PriorityQueue.java:647)
at java.util.PriorityQueue.offer(PriorityQueue.java:344)
at java.util.PriorityQueue.add(PriorityQueue.java:321)
at lab13d.Lab13d.main(Lab13d.java:53)
Java Result: 1

And cannot make heads nor tails of it. I have never used queues before so this is really very new to me.

Here is my code:

public static void main(String[] args)
{


    int players = 2;
    Card[] pile;
    int MAX_CARDS = 52;
    int MAX_SUITS = 4;
    int MAX_RANKS = 14;




    pile = new Card[MAX_CARDS];
    int index = 0; 
    for(int suit = 1; suit <= MAX_SUITS; suit++){
        for(int rank = 2; rank <= MAX_RANKS; rank++, index++ ){
            Card c = new Card(suit, rank);
            pile[index] = c;

        }
    }       


    PriorityQueue<Card>[] hand = new PriorityQueue[players + 1];
      for(int i = 0; i < players + 1; i++){
          hand[i] = new PriorityQueue();
      }


    for(int i = 0; i < players; i++){
        for(int j = i; j < pile.length; j+= players){
            hand[i].add(pile[j]);
        }
    }
    int leftOverCards = pile.length - (pile.length % players);
    for(int i = leftOverCards; i < pile.length; i++){
        hand[players].add(pile[i]);
    }

    System.out.printf("%s", hand[0]);

}
Community
  • 1
  • 1
mhm.sherpa
  • 89
  • 1
  • 9
  • 1
    the answer in link they have sent you explains it very well already; find the patience to read it thru (replacing the word TreeSet with PriorityQueue whenever it is used) – guido May 23 '15 at 23:41

1 Answers1

2

I assume that following line throws the ClassCastException:

hand[i].add(pile[j]);

You are trying to add instance of Card to PriorityQueue and apparently your Card can't be cast to comparable. You need to go to your Card.java file and make the class implement Comparable interface:

public class Card implements Comparable<Card>{
    ...
    ...
    ...

You also need to implement compareTo method, I assume that your cards will be compared by their value and that a Card object has an int attribute value ranging from 2(a two) to 13(an ace). If values are equal, we can compare by suit - I will leave that part to yourself :

@Override
public int compareTo(Card o) {
    int comparisonOutcome = ((Integer)o.getValue()).compareTo((Integer)this.getValue());
    if(comparisonOutcome == 1 || comparisonOutcome == -1){
        return comparisonOutcome;
    else{ 
        // here values are equal, we can compare by card suit
        // return -1, 1 or 0        
}

I hope it helps.

Suspended
  • 1,184
  • 2
  • 12
  • 28
  • Yeah I think this helps, you're saying that I need a compare method in my Card class, this method defines a way to compare two different cards. Ok this makes sense I was planning on implementing it later but why exactly is this required? It doesn't seem like this would be something needed by a class that creates a simple queue? – mhm.sherpa May 24 '15 at 01:39
  • It's required by PriorityQueue, which needs its contents to be Comparable. Based on comparisons it sets priorities. How the objects are compared, it is up to you. – Suspended May 24 '15 at 01:41