/* * T05n03.java -- Demonstration of an implementation of the Comparable * interface. * * Comparable is perhaps the easiest of the standard Java interfaces * to implement, because it has only one method: compareTo(). * * In this example, we have a PlayingCard class that implements Comparable. * Here, compareTo() is required to accept an Object as its argument, so a * bit of type-casting is necesssary. This is how you'd have to code a * method that implements an interface in a pre-1.5 version of Java. * To see how Java 1.5 simplifies this, see the next example program. * * Something else to note: compareTo() returns an int instead * of returning a boolean. This is because there are three possible * outcomes of a comparison: ab. compareTo() follows the * tradition of the strcmp() function from the language C: When ab, return a positive number; when a==b, * return 0. Here we just use -1 and +1, but sometimes the magnitude of * the value has meaning, too. */ import java.io.*; class PlayingCard implements Comparable { char suit; // 'C', 'D', 'H', or 'S' int rank; // 2, 3, ..., 9, 10, 11 (J), 12 (Q), 13 (K), 14 (A) PlayingCard () { rank = (int)Math.floor(Math.random()*13) + 2; // 0-12 + 2 = 2-14 assert (rank>1)&&(rank<15); // verify that the rank is in range switch ((int)Math.floor(Math.random()*4)) { case 0: suit = 'C'; case 1: suit = 'D'; case 2: suit = 'H'; case 3: suit = 'S'; default: suit = '#'; } } PlayingCard (char fromSuit) { rank = (int)Math.floor(Math.random()*13) + 2; // 0-12 + 2 = 2-14 suit = fromSuit; } PlayingCard (int fromRank, char fromSuit) { suit = fromSuit; rank = fromRank; } public int getRank() { return rank; } public char getSuit() { return suit; } public String toString() { String temp = ""; if (rank < 11) temp += rank; else switch (rank) { case 11: temp += "Jack"; break; case 12: temp += "Queen"; break; case 13: temp += "King"; break; case 14: temp += "Ace"; } temp += " of "; switch (suit) { case 'C': temp += "Clubs"; break; case 'D': temp += "Diamonds"; break; case 'H': temp += "Hearts"; break; case 'S': temp += "Spades"; } return temp; } public int compareTo (Object cardObject) { PlayingCard card = (PlayingCard) cardObject; if ( (suit == card.getSuit()) && (rank == card.getRank()) ) return 0; if (rank < card.getRank()) return -1; if ( (rank == card.getRank()) && (suit < card.getSuit()) ) return -1; return 1; } } public class T05n03 { public static void compareThem(PlayingCard card1, PlayingCard card2) { if (card1.compareTo(card2) < 0) System.out.println(card1 + " is less than " + card2); else if (card1.compareTo(card2) > 0) { System.out.println(card1 + " is greater than " + card2); } else { System.out.println(card1 + " is equal to " + card2); } } public static void main (String [] args) { int limit, powerOf2; PlayingCard card1 = new PlayingCard(14,'D'), card2 = new PlayingCard(6,'S'), card3 = new PlayingCard(14,'S'), card4 = new PlayingCard(13,'D'), card5 = new PlayingCard(6,'S'); compareThem(card1,card2); // diff. objects, diff. suits, diff. ranks compareThem(card2,card1); // diff. objects, diff. suits, diff. ranks compareThem(card1,card3); // diff. objects, diff. suits, same ranks compareThem(card4,card1); // diff. objects, same suits, diff. ranks compareThem(card2,card5); // diff. objects, same suits, same ranks compareThem(card2,card2); // same object! } }