From 4124d298bb22f66fe4227578a4b63249008d2551 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 6 May 2025 14:23:57 -0700 Subject: [PATCH 1/2] Initial Commit --- .../github/auberonedu/equalsLivecode/App.java | 5 +- .../auberonedu/equalsLivecode/Centroid.java | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 53efd3e..54cf96d 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -13,11 +13,14 @@ public static void main(String[] args) { Centroid locA = new Centroid(4, 9, "Salamander"); Centroid locB = new Centroid(4, 9, "Salamander"); + Centroid locC = locA; + + System.out.println(); 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..4cb989a 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,51 @@ 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; + // if(this.getClass() != o.getClass()) return false; + // Centroid other = (Centroid) o; + + // 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.equals(other.name)) return false; + // return true; + // } } From 05a8c4d14f07ea0e4f9bdf8cb136bd718703544b Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 6 May 2025 14:46:05 -0700 Subject: [PATCH 2/2] Second commit --- .../java/io/github/auberonedu/equalsLivecode/App.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 54cf96d..341f463 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -13,13 +13,20 @@ public static void main(String[] args) { Centroid locA = new Centroid(4, 9, "Salamander"); Centroid locB = new Centroid(4, 9, "Salamander"); - Centroid locC = locA; + // Centroid locC = locA; System.out.println(); 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.size()); + // videoDemo(); }