I have been working on a PlayingCard class, and I am having difficulty writing an equals() method for the class. The intent for the equals method is to compare two playing cards to see if they are identical or not.
I have used an example from the Big Java Late Objects Book and altered it to try and check to see if the two cards are identical or not, but in both cases (identical and not identical), I get the same output. What is going wrong and how can I get it to work?
This is the PlayingCard class, with the equals method at the bottom.
public class PlayingCard
{
private Rank rank;
private Suit suit;
public PlayingCard(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;
}
public Rank getRank()
{
System.out.println(rank);
return rank;
}
public Suit getSuit()
{
System.out.println(suit);
return suit;
}
@Override
public String toString()
{
return getClass().getName() + "[rank " + rank + "suit " + suit + "]";
}
public void format()
{
System.out.format(rank + " OF " + suit);
System.out.println("");
}
@Override
public boolean equals(Object otherObject)
{
if (otherObject == null)
{
System.out.println("Match");
return false;
}
if (getClass() != otherObject.getClass())
{
System.out.println("Match");
return false;
}
System.out.println("No Match, True");
PlayingCard other = (PlayingCard) otherObject;
return suit.equals(other.suit) && rank == other.rank;
}
}
And this is the current tester:
public class PlayingCardTester
{
public static void main(String[] args)
{
PlayingCard test = new PlayingCard(Rank.ACE, Suit.DIAMONDS);
PlayingCard test2 = new PlayingCard(Rank.FIVE, Suit.CLUBS);
PlayingCard test3 = new PlayingCard(Rank.ACE, Suit.DIAMONDS);
test.getRank();
test2.getRank();
test.getSuit();
test2.getSuit();
test.toString();
test.format();
test2.toString();
test2.format();
test.equals(test2);
test.equals(test3);
System.out.println("");
}
}
Edit:
Enum Rank:
public enum Rank
{
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9),
TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);
private int value;
private Rank(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
}
Enum Suit:
public enum Suit
{
SPADES(-2), CLUBS(-1), HEARTS(0), DIAMONDS(1);
private int value;
private Suit(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
}
And the output I get from the tester:
run:
ACE // getRank and getSuit print out the Rank and Suit respectivly for
FIVE // for the card specified and return it
DIAMONDS
CLUBS
ACE OF DIAMONDS // toString and format work together to get output of
FIVE OF CLUBS // cards to show what they are
No Match, True // This is from the equals method, both outputs are the
No Match, True // same, first test-test2, second test-test3.
BUILD SUCCESSFUL (total time: 1 second)
What I am trying to do is to compare PlayingCard test with test2 and test3 to see if they are the same card or not, as in, having the same suit and rank or not, and printing out if they are equal or not.
Edit 2: Current equals method alteration, still just getting false as an output for both tests.
@Override
public boolean equals(Object otherObject)
{
boolean set = false;
if (!(otherObject instanceof PlayingCard))
{
set = false;
}
if (otherObject == this)
{
set = true;
}
System.out.println(set);
return set;
}