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
public static final Comparator
// 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
Updated Card class with the compareTo method implemented to achieve the desired sorting import javau... View full answer
Get step-by-step solutions from verified subject matter experts
