/* * T05n04.java -- Demonstration of the implementation of the Comparable * interface, using parameterized types (a.k.a. generics). * * In Java 1.5 and beyond, programmers can use parameterized types. * This is mostly useful when we're manipulating a collection of * objects -- we can tell Java exactly which variety of objects can * be held within the collection. This makes it easier for Java to * warn the programmer when s/he tries something potentially dangerous. * We'll talk about Java's collection classes soon. * * In the meantime, be aware that Comparable can be parameterized, too. * In this case, we're implementing Comparable to tell the world that * PlayingCard objects can be compared. Beyond that, we want to let * it be known that PlayingCard objects can be compared only to other * PlayingCard objects. (Comparing them to any other kind of object * doesn't make much sense.) The advantage of doing this can be seen * in the code of the compareTo() method, below. */ 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; } // Note that with the ... implements Comparable // at the top, we have told Java to what we'll be comparing // PlayingCard objects: Other PlayingCard objects! Java // no longer must fall back on the default Object assumption, // and as a result we no longer have to write compareTo() to // accept an Object and cast it before we can use it. We can // accept PlayingCard objects specifically, and simply use them. public int compareTo (PlayingCard card) { 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 T05n04 { 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! } }