Enjoy the refinemer If you want to set the uniqueness of a class, you need to ov
ID: 3710239 • Letter: E
Question
Enjoy the refinemer If you want to set the uniqueness of a class, you need to override the hashCode) method... ana the equals(Object o) method. The way Java works, it uses the hashcode value to determine whether or not you have the same object... If Java senses the same two instances of objects are equal, it'll execute the equals method In the example below, I'm setting the hashcode for a Rectangle using the Objects.hash0 method, using iWidth and iLength as parameters. I'm telling Java that two Rectangles with the same Width and Length are the SAME rectangle Question How would you code the hashcode() method in Deck if you wanted to consider each card unique? The correct code wouldn't allow the Ace of Spades to be added twice to the same HashSet if there's only one deck of cards * hashCode . I'm overriding the hashCode method in object- setting the hashCode to the Width & Length of the rectangle. What I'm trying to do is make rectangles unique by width & length Override public int hashCode) f return Objects.hash(iwidth, iLength); equals. This method is triggered when two objects with the same hashCode are detected Even though I'm setting hashcode myself, I have to also override the equals method to tell java when one objects another override public boolean equals (Object o) if (othis) return true; if ((o instanceof Rectangle)) return false Rectangle rec (Rectangle) o return objects.equals(hashCode(), rec.hashCode)); HTML EditorExplanation / Answer
Please find the java code for clarification of Deck I have used enums for card number and the shade of card.
Deck.java
import java.util.HashSet;
import java.util.Objects;
// Enum for holding the card number.
enum CardNumber
{
ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING;
}
// enum for holding the card shades which are four.
enum Shade {
HEART,DIAMOND,SPADE,CLUB;
}
// A card class will have a card Number and a shade.
class Card {
private CardNumber cardNumber;
private Shade shade;
// parameterized constructor
public Card(CardNumber cn, Shade s) {
this.cardNumber = cn;
this.shade = s;
}
public CardNumber getCardNumber() {
return cardNumber;
}
public void setCardNumber(CardNumber cardNumber) {
this.cardNumber = cardNumber;
}
public Shade getShade() {
return shade;
}
public void setShade(Shade shade) {
this.shade = shade;
}
// The hashcode will be on the cardNumber and shade.
// Similar to iWidth and iLength
@Override
public int hashCode() {
return Objects.hash(cardNumber, shade);
}
// Equals to method as written in the sample program
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
return Objects.equals(hashCode(), other.hashCode());
}
}
public class Deck {
// Creating a hashset of cards
public HashSet<Card> cards;
// Constructor Initializing the card set.
public Deck() {
cards = new HashSet<Card>();
}
public static void main(String[] args) {
Deck deck = new Deck();
boolean var;
// I am trying to add card ACE of SPADE in the set.
// Add method of hashset return true if the insertion is sucessfull
// false otherwise.
var = deck.cards.add(new Card(CardNumber.ACE,Shade.SPADE));
System.out.println("Insertion is " + var); // prints true.
var = deck.cards.add(new Card(CardNumber.ACE,Shade.SPADE));
System.out.println("Insertion is " + var); // prints false.
}
}
Sample output
Insertion is true
Insertion is false
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.