From f144b038c770c08d4c0254e7606990189ab98264 Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Tue, 6 May 2025 14:50:03 -0700 Subject: [PATCH] Finished exercise --- .../github/auberonedu/equalsLivecode/App.java | 16 ++++-- .../auberonedu/equalsLivecode/Centroid.java | 50 ++++++++++++++++++- .../auberonedu/equalsLivecode/Location.java | 4 ++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/auberonedu/equalsLivecode/Location.java diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 53efd3e..07e7bb8 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -14,10 +14,20 @@ 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)); + // System.out.println("Result of locA == locB " + (locA == locB)); + // System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); - videoDemo(); + // Set centroids = new HashSet<>(); + // centroids.add(locA); + // System.out.println(centroids.contains(locB)); + + Location a = new Location(5, 7, "Food"); + Location b = new Location(5, 7, "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..0712650 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,52 @@ 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; + } + + // Our version of equals method + // @Override + // public boolean equals(Object o) { + // if (o == null) return false; + // if (getClass() != o.getClass()) return false; // if we have the same class + // 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; + // } +} \ No newline at end of file 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..490b7de --- /dev/null +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Location.java @@ -0,0 +1,4 @@ +package io.github.auberonedu.equalsLivecode; + +public record Location(int x, int y, String label) { +} \ No newline at end of file