diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 53efd3e..14b4e5f 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -1,7 +1,6 @@ package io.github.auberonedu.equalsLivecode; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -13,11 +12,26 @@ public static void main(String[] args) { Centroid locA = new Centroid(4, 9, "Salamander"); Centroid locB = new Centroid(4, 9, "Salamander"); - - System.out.println("Result of locA == locB " + (locA == locB)); - System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); - - videoDemo(); + Centroid locC = locA; + + // System.out.println("Result of locA == locB " + (locA == locB)); + // System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); + + // Set centroids = new HashSet<>(); + // System.out.println(locA.hashCode()); + // System.out.println(locB.hashCode()); + // centroids.add(locA); + // centroids.add(locB); + // System.out.println(centroids.contains(locB)); + // System.out.println(centroids.size()); + + Location a = new Location(5, 1, "food"); + Location b = new Location(5, 1, "food"); + System.out.println(a.equals(b)); + System.out.println(a.hashCode()); + System.out.println(b.hashCode()); + + // videoDemo(); } // We will look at this a bit later in the livecode, please ignore for first part diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java index b0e343a..1394d6e 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -11,6 +11,10 @@ public Centroid(int r, int c, String name) { this.name = name; } + //we're allowed to get other instance if we're in the same class??? + //if I'm a centroid and I have this instance of a centroid, I'm allowed + //to get the private variables of any other private centroid that + //gets passed to me public int getR() { return r; } @@ -19,7 +23,59 @@ public int getC() { return c; } + //this override is putting into hash table based on values, not + //memory reference + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + r; + result = prime * result + c; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Centroid other = (Centroid) obj; + if (r != other.r) + return false; + if (c != other.c) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + public String getName() { return name; } + + // @Override + // public boolean equals(Object o) { + // if(o == null) return false; + // //if(this.getClass() != that.getClass) return false + // if(getClass() != o.getClass()) return false; + // //casting + // Centroid other = (Centroid) o; + // //primitive ints cannot be null!! + // if(r != other.r) return false; + // if(c != other.c) return false; + // if(name == null) { + // if(other.name == null) return true; + // if(other.name != null) return false; + // } + // //if(name != other.name) return false;//don't compare strings with == + // if(!name.equals(other.name)) return false; + // return true; + // } } diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/Location.java b/src/main/java/io/github/auberonedu/equalsLivecode/Location.java new file mode 100644 index 0000000..2884a30 --- /dev/null +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Location.java @@ -0,0 +1,6 @@ +package io.github.auberonedu.equalsLivecode; + +public record Location (int x, int y, String label) { + +} + \ No newline at end of file