diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 44717b0..0ce4329 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; @@ -11,13 +10,13 @@ public static void main(String[] args) { // To start, look at Centroid.java // BEFORE running the code, make a prediction of what the following will print: - Centroid locA = new Centroid(4, 9, "Salamander"); - Centroid locB = new Centroid(4, 9, "Salamander"); + // Centroid locA = new Centroid(4, 9, null); + // 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)); + // System.out.println("Result of locA == locB " + (locA == locB)); + // System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); - //videoDemo(); + 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..92a80d2 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,54 @@ public int getC() { public String getName() { return name; } + + @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; + } + + // @Override + // public boolean equals(Object o) { + // if(o == null) return false; // null check + + // if (!getClass().equals(o.getClass())) return false; // class check + + // Centroid other = (Centroid) o; // cast generic Object o to Centroid o + + // if(r != other.r) return false; + // if(c != other.c) return false; + + // if(name == null) return other.name == null; + + // return name.equals(other.name); + + // } + + }