Charlie Lonergan
Charlie Lonergan

Reputation: 11

What is wrong with my Card class, I do not want it being abstract

I have to make this class comparable, so I can compare cards. Every time I try it keeps asking me to make the card abstract, the exact error is "Card is not abstract and does not override abstract method compareTo(Card) in Comparable"

        package p2cw1;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;

public class Card implements Serializable, Comparable<Card> {

    static final long serialVersionUID = 100L;

    public enum Suit {

        CLUBS, SPADES, DIAMONDS, HEARTS
    };

    public enum Rank {

        TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
    };

    public Rank rank;
    public Suit suit;
    public Card Compare1;
    public Card Compare2;

    public Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return rank;
    }

    public Suit suit() {
        return suit;
    }

    public String toString() {
        return rank + " of " + suit;
    }

    public Rank getNext(Rank r) {
        this.rank = r;
        int s = rank.ordinal()+1;
        Card.Rank result = Card.this.rank();
        System.out.println(s);
        return result;
    }
    }

My apologies if I did not put the code correctly on this site. First time using it

Upvotes: 0

Views: 74

Answers (1)

Steve Smith
Steve Smith

Reputation: 2270

If your class implements Comparable, you need to implement the method int compareTo(T o). Otherwise your class is not complete, and thus the compiler thinks it should be abstract.

Basically, if you don't want it to be abstract, add this method:-

@Override
public int compareTo(Card other) {
    return <some code to determine which card is "higher">();
}

Your method should return a negative integer, zero, or a positive integer if this card is less than, equal to, or greater than the other Card.

Upvotes: 2

Related Questions