public class
Card implements Comparable
{
public String suit; //hearts, diamonds, spades or clubs
public int value;
public Card()
{
value = (int)(Math.random() * 13 +
1);
double rn
= Math.random();
if (rn
<= 0.25)
suit = "hearts";
else if (rn <= 0.50)
suit = "diamonds";
else if (rn <= 0.75)
suit = "spades";
else
suit = "clubs";
}
public String toString()
{
return value + " of
" + suit;
}
@Override
public int compareTo(Object
o)
{
//We will compare
this and o
Card other =
(Card)o;
//If suits are the
same
if (this.suit.equals(other.suit))
{
if (this.value
< other.value)
{
return -1;
}
else if (this.value == other.value)
{
return 0;
}
else
{
return 1;
}
}
//If suits are
different
//Order of suits: clubs,
diamonds, spades and then hearts.
if (this.suit.equals("clubs"))
{
return -1;
}
else if (this.suit.equals("diamonds")
&& !other.suit.equals("clubs"))
{
return -1;
}
else if (this.suit.equals("spades")
&& other.suit.equals("hearts"))
{
return -1;
}
else
{
return 1;
}
}
}
|