Question

Complete the implementation of the Card class. The two methods that you need to complete are...

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:

  1. Cards are first compared by rank. If the rank of this card is less than the rank of the other card then -1 is returned. If the rank of this card is greater than the rank of the other card then 1 is returned. If the ranks of the two cards are equal then the method proceeds to the next step. The ranks are compared using the compareTo method from the Rank enum.
  2. Cards are compared by suit. If the suit of this card is less than the suit of the other card then -1 is returned. If the suit of this card is greater than the suit of the other card then 1 is returned. If the suits of the two cards are equal then the method returns 0. The suits are compared using the compareTo method from the Suit enum.

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) {

}

}

Homework Answers

Answer #1

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
   }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class. Rules are the following: - Deck and PockerHand should be Iterable (note that you may just reuse one of the underlying iterators to provide this functionality) - Poker should implement 2 methods checking for two possible poker hands , see the comments inside the classes for details. Hint: remember how one can count the frequency of words in text? - whenever you...
This is my course class. Can someone please explain what the equals method is for and...
This is my course class. Can someone please explain what the equals method is for and also the compareTo method is for in general and then explain what the methods are doing in this class. public class Course {    private boolean isGraduateCourse;    private int courseNum;    private String courseDept;    private int numCredits;       public Course(boolean isGraduateCourse, int courseNum, String courseDept, int numCredits) {        this.isGraduateCourse=isGraduateCourse;        this.courseNum=courseNum;        this.courseDept=courseDept;        this.numCredits=numCredits;   ...
in java Write a test class with two unit tests that test different cases for the...
in java Write a test class with two unit tests that test different cases for the hasSeq() function described below: class Hand { // Add Card to Hand public void add(Card card); // Returns true if hand has 3 Card’s of consecutive ranks of the same suit // “consecutive” would mean something like 9, 10 and 11 public boolean hasSeq(); } class Card { // return suit of card: Spades, Hearts, Diamonds or Clubs public String suit(); // return rank...
The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in...
The class Term encapsulates the coefficient and exponent of a single term. Exponents are limited in range from 0 to 99. public class Term : IComparable { private double coefficient; private integer exponent; // Creates a term with the given coefficient and exponent public Term (double coefficient, integer exponent) { … } // Evaluates the current term at x public double Evaluate (double x) { … } // Returns -1, 0, or 1 if the exponent of the current term...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
Can you explain if my answer is correct and why the others are not? Which line...
Can you explain if my answer is correct and why the others are not? Which line should be added into the removeCard method of class GroupOfCards? public class GroupOfCards { private ArrayList<Card> cards; // list of cards // Remove and return a card object at a specified location public Card removeCard(int index) { ________________ } // end removeCard } // end class GroupOfCards A. cards[index] = null; B. Card = cards.get(index); return card; C. cards.remove(index); (correct answer) D. return cards.remove(index);...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Here is a basic Counter class. class Counter { private int num = 0; // Not...
Here is a basic Counter class. class Counter { private int num = 0; // Not needed for this question public void increment() { num++; } public boolean equals(Counter other) { return this.num == other.num; } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Counter[] counters, int numCounters, Counter counter) The goal of the method is to...
Using the interface and class definitions below, write the headings of all of the methods which...
Using the interface and class definitions below, write the headings of all of the methods which still need to be defined in the class Class2. https://hastebin.com/kopejolila.java public interface Interface1 { public float method1(int i) ; } public interface Interface2 extends Interface1 { public int method2(int i) ; public void method3(int i) ; } public abstract class Class1 { public float method1(int anInt) { return anInt * 2.0f ; } public abstract void method3(Object anObject) ; public abstract void method4(int anInt)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT