CSE 331: Equalities practice
Question 1: Equality and Hash Codes: Furniture Parts
An immutable Part describes a part in a furniture kit. Its fields
are shown below. Assume name is non-null, weight is positive
and finite, and quantity is positive.
You may assume no arithmetic here ever overflows.
private String name;
private double weight;
private int quantity;
Each proposed equals(Object o) first returns false if o is
not a Part, then declares Part p = (Part) o;. Its final
return statement is one of the following:
-
E1:
return this.name.equals(p.name) && this.weight == p.weight; -
E2:
return true; -
E3:
return this.name.equals(p.name); -
E4:
return this.weight == p.weight && this.quantity == p.quantity;
The rows give the proposed bodies of hashCode(). Casting a
positive double to int drops its fractional part.
For each hash function and equality relation, write in its box:
- Y if equal objects always have equal hash codes
- N if two equal objects can have different hash codes
Judge only compatibility with the given equality relation, not whether that relation is the right equality for the class. Collisions between unequal objects are allowed.
| E1 | E2 | E3 | E4 | |
|---|---|---|---|---|
A. H1: return this.name.hashCode() + 31*this.quantity; |
||||
B. H2: return 331; |
||||
C. H3: return this.name.hashCode() + 31*(int)this.weight; |
||||
D. H4: return this.name.hashCode(); |
A. H1: return this.name.hashCode() + 31*this.quantity;
Answer
| E1 | E2 | E3 | E4 |
|---|---|---|---|
| N | N | N | N |
E1: N.
("nail", 1.0, 1)and("nail", 1.0, 2)satisfy E1.- The quantity term changes their hash codes by 31.
E2: N.
- E2 equates
("nail", 1.0, 1)and("nail", 1.0, 2). - The quantity term changes their hash codes by 31.
E3: N.
("nail", 1.0, 1)and("nail", 1.0, 2)satisfy E3.- The quantity term changes their hash codes by 31.
E4: N.
("nail", 1.0, 1)and("bolt", 1.0, 1)satisfy E4.- The quantity terms agree, but the name hash codes differ.
The rule: Lecture 16 on equals and hashCode.
B. H2: return 331;
Answer
| E1 | E2 | E3 | E4 |
|---|---|---|---|
| Y | Y | Y | Y |
E1: Y.
- Every call returns 331, regardless of which fields E1 compares.
E2: Y.
- Even though E2 equates all Parts, every Part has hash code 331.
E3: Y.
- Every call returns 331, regardless of which fields E3 compares.
E4: Y.
- Every call returns 331, regardless of which fields E4 compares.
The rule: Lecture 16 on equals and hashCode.
C. H3: return this.name.hashCode() + 31*(int)this.weight;
Answer
| E1 | E2 | E3 | E4 |
|---|---|---|---|
| Y | N | N | N |
E1: Y.
- E1 requires equal names and equal weights.
- Equal names give equal String hash codes.
- Equal weights give equal casts to
int. Both terms agree.
E2: N.
- E2 equates
("nail", 1.0, 1)and("nail", 2.0, 1). - The weight term changes their hash codes by 31.
E3: N.
("nail", 1.0, 1)and("nail", 2.0, 1)satisfy E3.- The weight term changes their hash codes by 31.
E4: N.
("nail", 1.0, 1)and("bolt", 1.0, 1)satisfy E4.- The weight terms agree, but the name hash codes differ.
The rule: Lecture 16 on equals and hashCode.
D. H4: return this.name.hashCode();
Answer
| E1 | E2 | E3 | E4 |
|---|---|---|---|
| Y | N | Y | N |
E1: Y.
- E1 requires equal names, so their String hash codes agree.
E2: N.
- E2 equates
("nail", 1.0, 1)and("bolt", 1.0, 1). - Their names have different String hash codes.
E3: Y.
- E3 requires equal names, so their String hash codes agree.
E4: N.
("nail", 1.0, 1)and("bolt", 1.0, 1)satisfy E4.- Their names have different String hash codes.
The rule: Lecture 16 on equals and hashCode.
Question 2: Implementing Equality for an Integer Set
A final class IntSet represents an immutable set of integers.
Its representation is a private int[] values, used as a list.
The array is non-null and has no duplicates. Its order has no
meaning: [1, 2] and [2, 1] represent the same set.
The constructor copies its input, and no method exposes the array.
Each row proposes the entire equals(Object o) method for this
class. Consider each proposal independently. A good implementation
must terminate normally and return true exactly when the argument
is an IntSet representing the same abstract set. It must satisfy
Object's equals contract and leave both objects unchanged.
Arrays.equals(a, b) compares the lengths and then the integers
at corresponding positions. Arrays.sort(a) sorts a in place.
a.clone() makes a separate copy of the array.
For each proposed implementation, write in its box:
- T if it is a good equals method for this IntSet specification
- F if it is not
a.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
for (int x : values) {
boolean found = false;
for (int y : other.values) {
if (x == y) found = true;
}
if (!found) return false;
}
return true;
}
Answer
| Good equals? |
|---|
| F |
False: the containment check is not symmetric.
- Let
xhold[1]andyhold[1, 2]. x.equals(y)returns true because 1 is iny.y.equals(x)returns false because 2 is not inx.- The missing size check lets a proper subset compare equal.
The definition: Lecture 16 on equality of abstract values.
b.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
int[] a = values.clone();
int[] b = other.values.clone();
Arrays.sort(a);
Arrays.sort(b);
return Arrays.equals(a, b);
}
Answer
| Good equals? |
|---|
| T |
True: sorting copies gives one ordering for each abstract set.
- The arrays contain no duplicates.
- Sorting puts the same set of integers in the same order.
Arrays.equalsthen returns true exactly for equal sets.clonekeeps the sorting from changing either original array.
The definition: Lecture 16 on equality of abstract values.
c.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
return other.equals(this);
}
Answer
| Good equals? |
|---|
| F |
False: comparing two IntSets never returns a boolean.
x.equals(y)callsy.equals(x).- That call calls
x.equals(y)again, with no progress. - Even
x.equals(x)recurses without a base case. - In Java, the repeated calls eventually cause
StackOverflowError.
The definition: Lecture 16 on equality of abstract values.
d.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
if (values.length != other.values.length) return false;
for (int x : values) {
boolean found = false;
for (int y : other.values) {
if (x == y) found = true;
}
if (!found) return false;
}
return true;
}
Answer
| Good equals? |
|---|
| T |
True: it compares the abstract sets.
- Different lengths imply different sets because there are no duplicates.
- With equal lengths, the nested loops check that every element of this set belongs to the other set.
- A subset of a finite set with the same size is the whole set.
- Thus the method returns true exactly for equal abstract sets.
- The loops terminate and change neither array.
The definition: Lecture 16 on equality of abstract values.
e.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
return this == other;
}
Answer
| Good equals? |
|---|
| F |
False: distinct objects can represent the same set.
- Let two distinct objects each have an array
[1]. - Both represent the set
{1}. this == otherreturns false.- Reference equality is an equivalence relation, but it does not implement equality of the specified abstract values.
The definition: Lecture 16 on equality of abstract values.
f.
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) return false;
IntSet other = (IntSet) o;
return Arrays.equals(values, other.values);
}
Answer
| Good equals? |
|---|
| F |
False: it compares representations in order.
- Let two distinct objects have arrays
[1, 2]and[2, 1]. - Both represent the set
{1, 2}. Arrays.equalsreturns false because their first elements differ.- The method is an equivalence relation on array contents, but it does not implement equality of the specified abstract values.
The definition: Lecture 16 on equality of abstract values.