Article View: borland.public.jbuilder.java.language
Article #713Re: Collections.sort(List)
From: Alan Shiers
Date: Thu, 02 Dec 2004 20:04
Date: Thu, 02 Dec 2004 20:04
174 lines
5219 bytes
5219 bytes
> How is Card defined? Does it implement Comparable<Card>?
Damn! They went and changed Comparable too!! I didn't see that at first.
My class Card did implement Comparable but it was based on the old java 1.4.
I changed it and this seemed to compile fine. Do you see anything else I
should be aware of?
Is the signature for the compareTo() method correct?
Alan
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<Card>
{
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(Card 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(o.suit == rank[otherCardSuit])
break;
}
for(; thisCardSuit < rank.length; thisCardSuit++)
{
if(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(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:
<41af7630$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41af522d@newsgroups.borland.com> <41af5ea1$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,2868f4df97a5ed32 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 16:04:54 -0400 Lines: 176 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: <41af7630$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1102018096 24.222.101.2 (2 Dec 2004 12:08:16 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail