Question: Using the Card class below, add the code to have the Cards sorted by suite and then denim. Your solution should check for invalid

Using the Card class below, add the code to have the Cards sorted by suite and then denim.  Your solution should check for invalid objects and throw an exception if an illegal object is passed.

public class Card implements Comparable {

    // Comparators by suit and by denomination

    public static final Comparator SUIT_ORDER = new SuitOrder();

    public static final Comparator DENOM_ORDER = new DenomOrder();

 

    // Suit of the card (CLUBS = 1, DIAMONDS = 2, HEARTS = 3, SPADES = 4)

    private final int suit;

    

    // Denomination of the card

    private final int denom;

    

    public Card(int suit, int denom) {

        if (suit < 1 || suit > 4)

            throw new IllegalArgumentException("Invalid suit");

        if (denom < 1 || denom > 13)

            throw new IllegalArgumentException("Invalid denomination");

        this.suit = suit;

        this.denom = denom;

    }

 

    // COMPLETE THE FOLLOWING FUNCTION

    public int compareTo(Object o) {

        /*

         *  YOUR CODE HERE

         */

        return 0;

    }

}

Step by Step Solution

3.48 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Updated Card class with the compareTo method implemented to achieve the desired sorting import javau... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!