🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #705

Re: Collections.sort(List)

#705
From: Alan Shiers
Date: Thu, 02 Dec 2004 19:21
173 lines
5112 bytes
> How is Card defined?  Does it implement Comparable<Card>?
>
> -- 
> Gillmer J. Derge [TeamB]

Yes it does.

*******************************

package games;

import java.util.*;
import javax.swing.*;

/**
 * <p>Title: Card</p>
 * <p>Description: You can create 52 instances of this class, each
instance<br>
 * containing a CardValue and a Suit</p>
 * <p>You can use any container to hold the Cards, even a Hashtable if so
desired.
 * @author Alan Shiers
 * @version 1.0
 */

public class Card extends JLabel implements Comparable
{
  private Suit suit;
  private CardValue value;
  private volatile int hash = 0;
  private String alias = "";

  public Card(CardValue value, Suit s )
  {
    suit = s;
    this.value = value;
  }
  /**
   * Set an alias name for this Card for identification
   * @param st String
   */
  public void setAlias(String st)
  {
    alias = st;
  }
  /**
   * Get this Card(s) alias name
   * @return String
   */
  public String getAlias()
  {
    return alias;
  }

  /**
   * Get the value of this Card
   * @return CardValue
   */
  public CardValue getValue()
  {
     return value;
  }
  /**
   * Get the suit of this Card
   * @return Suit
   */
  public Suit getSuit()
  {
    return suit;
  }

  /**
   * This compareTo method will only return either -1 for less than,
   * or 1 for greater than results. No two cards are equal in a
   * standard deck of cards. Therefore, this method does not return zero.
   * This method will throw a ClassCastException if a Card type object
   * is not passed as a parameter.
   * @return int Either -1 or 1
   * @param o Object expected to be a Card type
   */
  public int compareTo(Object o)
  {
    //To compare cards, we first need to determine the ranking
    //of the suits, then their values. The Deck class contains
    //a default ranking of the Suits and a mutator method
    //to change the default ranking.
    Suit[] rank = Deck.getSuitRanking();
    //Match the location of the card suit with that
    //of the rank array.
    int otherCardSuit = 0;
    int thisCardSuit = 0;
    for(; otherCardSuit < rank.length; otherCardSuit++)
    {
        if(((Card)o).suit == rank[otherCardSuit])
          break;
    }
    for(; thisCardSuit < rank.length; thisCardSuit++)
    {
        if(((Card)o).suit == rank[thisCardSuit])
          break;
    }
    //Now, we check the values of each card
    List list = CardValue.VALUES;
    if(thisCardSuit == otherCardSuit)
    {
      //In the same suit you can't have two identicle values.
      //Therefore, we only need to test if this cards value
      //is less than or greater than the other cards value.
      if(list.indexOf(this.value) < list.indexOf(((Card)o).value))
        return -1;
      else
        return 1;
    }
    else if(thisCardSuit < otherCardSuit)
    {
      return -1;
    }
    return 1;
  }
  /**
   * Find out if this Card is equal to the one being passed as a parameter.
   * @param o Object expected to be of type Card
   * @return boolean True if equal, False otherwise
   */
  public boolean equals(Object o)
  {
     if(!(o instanceof Card))
       return false;
     if(this.suit == ((Card)o).suit)
     {
       if(this.value == ((Card)o).value)
         return true;
       else
         return false;
     }
     return false;
  }
  /**
   * For Hashtables, obtain a unique hashcode.
   * @return int The hashcode.
   */
  public int hashCode()
  {
    if(hash == 0)
    {
      int result = 17;
      result = 37 * result + calcHash(suit.toString());
      result = 37 * result + calcHash(value.toString());
      hash = result;
    }
    return hash;
  }

  private int calcHash(String st)
  {
    int i = 0;
    int j = 0;
    char ac[] = st.toCharArray();
    for(int l = 0; l < ac.length; l++)
        i = 31 * i + ac[j++];

    return i;
  }
  /**
   * Obtain a string representation of this Card.
   * @return String
   */
  public String toString()
  {
    return  value + " of " + suit;
  }

}
Technical Details
Message-ID: <41af6bfb@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41af522d@newsgroups.borland.com> <41af5ea1$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,2868f4df97a5ed32,start
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Alan Shiers" <ashiers@hfx.eastlink.ca>
Newsgroups: borland.public.jbuilder.java.language
References: <41af522d@newsgroups.borland.com> <41af5ea1$1@newsgroups.borland.com>
Subject: Re: Collections.sort(List)
Date: Thu, 2 Dec 2004 15:21:20 -0400
Lines: 175
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 24.222.101.2
Message-ID: <41af6bfb@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1102015483 24.222.101.2 (2 Dec 2004 11:24:43 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail