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;   ...
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...
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)...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 7. How have angiosperms coevolve with pollinators to enhance the fitness of both partners? Consider factors...
    asked 3 minutes ago
  • If the effect size for a four-group study was .5, the sum of square between was...
    asked 16 minutes ago
  • What technology out there do you find the most exciting and interesting currently? Why?  What interests you...
    asked 22 minutes ago
  • 5. Classify each description as observational or experimental (a)Participants in a study to determine the effects...
    asked 25 minutes ago
  • Python Write function stringCount() that takes two string inputs—a file name and a target string— and...
    asked 25 minutes ago
  • If you pay more in tuition to go to a top business​school, will it necessarily result...
    asked 31 minutes ago
  • Choosing manual vs automated testing comes down to benefits gained on your particular project. In this...
    asked 33 minutes ago
  • In banker's algorithm, which process will be handle first? process   need (A, B) p1            1, 4...
    asked 43 minutes ago
  • Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
    asked 46 minutes ago
  • Justify answer. Is the Jaccard coefficient for two binary strings (i.e., string of 0s and 1s)...
    asked 47 minutes ago
  • Solve the following system of equations and determine if there is a unique solution, an infinite...
    asked 55 minutes ago
  • In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function...
    asked 1 hour ago