Complete the implementation of the Card class. The two methods that you need to complete are compareTo and equals.
CopareTo API: Compares this card to another card for order. This method imposes the following order on cards:
Equals API: Compares this card to the specified object for equality. The result is true if obj is a card having the same rank and the same suit as this card, false otherwise.
Code:
public class Card implements Comparable<Card> {
/**
* The rank of this card.
*/
private Rank rank;
/**
* The suit of this card.
*/
private Suit suit;
public Card(Rank rank, Suit suit) {
if (rank == null || suit == null) {
throw new NullPointerException();
}
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return this.rank;
}
public Suit suit() {
return this.suit;
}
public Colour color() {
return this.suit.color();
}
@Override
public int hashCode() {
return Objects.hash(this.rank, this.suit);
}
@Override
public String toString() {
return this.rank + " of " + this.suit;
}
/*
* You need to implement the compareTo and equals methods below.
*
* For compareTo, you may compare ranks using the compareTo method
* found in the Rank class. You may compare suits using the compareTo method
* found in the Suit class.
*/
@Override
public int compareTo(Card other) {
}
@Override
public boolean equals(Object obj) {
}
}
public class Card implements Comparable<Card> {
/**
* The rank of this card.
*/
private Rank rank;
/**
* The suit of this card.
*/
private Suit suit;
public Card(Rank rank, Suit suit) {
if (rank == null || suit == null) {
throw new NullPointerException();
}
this.rank = rank;
this.suit = suit;
}
public Rank rank() {
return this.rank;
}
public Suit suit() {
return this.suit;
}
public Colour color() {
return this.suit.color();
}
@Override
public int hashCode() {
return Objects.hash(this.rank,
this.suit);
}
@Override
public String toString() {
return this.rank + " of " + this.suit;
}
/*
* You need to implement the compareTo and equals methods below.
*
* For compareTo, you may compare ranks using the compareTo method
* found in the Rank class. You may compare suits using the compareTo method
* found in the Suit class.
*/
@Override
public int compareTo(Card other) {
// first compare ranks of the
cards
if(rank.compareTo(other.rank) <
0) // If the rank of this card is less than the rank of the other
card then return -1
return -1;
else if(rank.compareTo(other,rank)
> 0) // If the rank of this card is greater than the rank of the
other card then return 1
return 1;
else // compre suits
{
if(suit.compareTo(other.suit) < 0) // If the suit of this card
is less than the suit of the other card then return -1
return -1;
else
if(suit.compareTo(other.suit) > 0) // If the suit of this card
is greater than the suit of the other card then return 1
return 1;
else // If the
suits of the two cards are equal then the method returns 0
return 0;
}
}
@Override
public boolean equals(Object obj) {
if( obj instanceof Card) // check
if obj is instance of Card class
{
Card card =
(Card) obj; // cast obj to Card
// return true
if this card having the same rank and the same suit as this card,
else false
return((rank.compareTo(card.rank) == 0) &&
(suit.compareTo(card.suit) == 0));
}
return false; // return false
}
}
Get Answers For Free
Most questions answered within 1 hours.