Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If you want to set the uniqueness of a class, you need to override the hashCode) metho... and the equals(Object o) method. The way Java

image text in transcribed
If you want to set the uniqueness of a class, you need to override the hashCode) metho... and 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 n 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) ( return objects.hash(iwidth, Length) * equals. This method is triggered when two objects with the same hashCode are detected. Even though I'n setting hashcode myself, I have to also override the equals method to tell java when one objectsanother. 0verride public boolean equals(object o) if (o-this) return true; if ((o instanceof Rectangle)) return false; Rectangle rec(Rectangle) o; return objects.equals (hashCode(), rec.hashCode(O) If you want to set the uniqueness of a class, you need to override the hashCode) metho... and 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 n 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) ( return objects.hash(iwidth, Length) * equals. This method is triggered when two objects with the same hashCode are detected. Even though I'n setting hashcode myself, I have to also override the equals method to tell java when one objectsanother. 0verride public boolean equals(object o) if (o-this) return true; if ((o instanceof Rectangle)) return false; Rectangle rec(Rectangle) o; return objects.equals (hashCode(), rec.hashCode(O)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

10. Microsoft Corporation

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago