Skip to content

Commit c93e018

Browse files
authored
Issue #2642 solution - hashcode for dominoes. (#2704)
* Issue #34 solution - hashcode for dominoes. * Add a comment in the Domino class making explicit that changes in the Domino class are not accepted by the solution. (Believe me, I was really surprised during my first submission that my changes in the Domino class were rejected. As well as my mentee ). * Change the way equals and hashcode are implemented to use the min+max value of each side instead of left+right. * Added a constraint validation within the Domino class constructor, that tiles must have a maximum number of 9 on each side (this is needed for the hash implementation). * In the equals code, a comparison between a Domino and an Object would generate a class cast exception instead of the expected value, false. * Issue #34 - fix lack of () * checkstyle fixes [no important files changed]
1 parent 4dd7e25 commit c93e018

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
import java.util.Objects;
2-
1+
// This class can't be changed by the solution.
2+
//
33
class Domino {
44
private int left;
55
private int right;
6+
private int hash;
67
Domino(int left, int right) {
8+
if (left < 0 || left > 9 || right < 0 || right > 9) {
9+
throw new IllegalArgumentException("Domino tiles must have a number between 0 and 9 on each side");
10+
}
711
this.left = left;
812
this.right = right;
13+
this.hash = Integer.min(left, right) + Integer.max(left, right) * 10;
914
}
10-
15+
1116
int getLeft() {
1217
return this.left;
1318
}
14-
19+
1520
int getRight() {
1621
return this.right;
1722
}
18-
23+
1924
@Override
2025
public boolean equals(Object o) {
26+
if (!(o instanceof Domino)) {
27+
return false;
28+
}
2129
Domino otherDomino = (Domino) o;
22-
return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) ||
23-
(this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft());
30+
return this.hash == otherDomino.hash;
2431
}
25-
32+
2633
@Override
2734
public int hashCode() {
28-
return Objects.hash(left, right);
35+
return hash;
2936
}
3037
}

exercises/practice/dominoes/src/main/java/Domino.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import java.util.Objects;
22

3+
4+
// This class can't be changed by the solution.
5+
//
36
class Domino {
47
private int left;
58
private int right;
9+
private int hash;
610
Domino(int left, int right) {
11+
if (left < 0 || left > 9 || right < 0 || right > 9 ) {
12+
throw new IllegalArgumentException("Domino tiles must have a number between 0 and 9 on each side");
13+
}
714
this.left = left;
815
this.right = right;
16+
this.hash = Integer.min(left,right) + Integer.max(left, right) * 10;
917
}
1018

1119
int getLeft() {
@@ -18,13 +26,15 @@ int getRight() {
1826

1927
@Override
2028
public boolean equals(Object o) {
29+
if (!(o instanceof Domino) ) {
30+
return false;
31+
}
2132
Domino otherDomino = (Domino) o;
22-
return (this.getLeft() == otherDomino.getLeft() && this.getRight() == otherDomino.getRight()) ||
23-
(this.getLeft() == otherDomino.getRight() && this.getRight() == otherDomino.getLeft());
33+
return this.hash == otherDomino.hash;
2434
}
2535

2636
@Override
2737
public int hashCode() {
28-
return Objects.hash(left, right);
38+
return hash;
2939
}
3040
}

0 commit comments

Comments
 (0)