Guest User

Untitled

a guest
Mar 21st, 2011
1,019
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. public class Card{
  4.     private short rank, suit;
  5.        
  6.     private static String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
  7.     private static String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
  8.  
  9.     //Constructor
  10.     public Card(short rank, short suit){
  11.         this.rank = rank;
  12.         this.suit = suit;
  13.     }
  14.  
  15.     // Getter and Setters
  16.     public short getSuit(){
  17.         return suit;
  18.     }
  19.  
  20.     public short getRank(){
  21.         return rank;
  22.     }
  23.    
  24.     protected void setSuit(short suit){
  25.         this.suit = suit;
  26.     }
  27.    
  28.     protected void setRank(short rank){
  29.         this.rank = rank;
  30.     }
  31.  
  32.     // methods
  33.     public static String rankAsString(int __rank){
  34.         return ranks[__rank];
  35.     }
  36.    
  37.     public static String suitAsString(int __suit){
  38.         return suits[__suit];
  39.     }
  40.    
  41.     public @Override String toString(){
  42.         return rank + " of " + suit;
  43.     }
  44.    
  45.     // Print card to string
  46.     protected String printCard(){
  47.         return ranks[rank] + " of " + suits[suit];
  48.     }
  49.    
  50.     // Determine if two cards are the same (Ace of Diamonds == Ace of Diamonds)
  51.     public static boolean sameCard(Card card1, Card card2){
  52.         return (card1.rank == card2.rank && card1.suit == card2.suit);
  53.     }  
  54.    
  55. }
  56.  
  57. class rankComparator implements Comparator<Object>{
  58.     public int compare(Object card1, Object card2) throws ClassCastException{
  59.         // verify two Card objects are passed in
  60.         if (!((card1 instanceof Card) && (card2 instanceof Card))){
  61.             throw new ClassCastException("A Card object was expeected.  Parameter 1 class: " + card1.getClass()
  62.                     + " Parameter 2 class: " + card2.getClass());
  63.         }
  64.        
  65.         short rank1 = ((Card)card1).getRank();
  66.         short rank2 = ((Card)card2).getRank();
  67.        
  68.         return rank1 - rank2;
  69.     }
  70. }
  71.  
  72. class suitComparator implements Comparator<Object>{
  73.     public int compare(Object card1, Object card2) throws ClassCastException{
  74.         // verify two Card objects are passed in
  75.         if (!((card1 instanceof Card) && (card2 instanceof Card))){
  76.             throw new ClassCastException("A Card object was expeected.  Parameter 1 class: " + card1.getClass()
  77.                     + " Parameter 2 class: " + card2.getClass());
  78.         }
  79.        
  80.         short suit1 = ((Card)card1).getSuit();
  81.         short suit2 = ((Card)card2).getSuit();
  82.        
  83.         return suit1 - suit2;
  84.     }
  85. }
RAW Paste Data