From 0653f722b5ca55a033486ac741f7449cbf876b9e Mon Sep 17 00:00:00 2001 From: dangrabo <193651636+dangrabo@users.noreply.github.com> Date: Tue, 25 Nov 2025 11:48:11 -0800 Subject: [PATCH] livecode --- .../github/auberonedu/equalsLivecode/App.java | 10 +-- .../auberonedu/equalsLivecode/Centroid.java | 62 +++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 44717b0..ffc44b9 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -11,13 +11,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, "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(); + 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..f514dbf 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,66 @@ 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; + } + + + // // this is not needed throughout the method, java assumes you are talking about this when referenceing any instance variable + // @Override + // public boolean equals(Object o) { + // // is this equal to o + // // ensure r, c, and name are all equal between this and o + // // ensure we meet Java equals contract + + // // check if o is null + // if (o == null) return false; + + // // if classes are not equal return false + // if(!this.getClass().equals(o.getClass())) return false; + + // // cast Centroid type + // Centroid other = (Centroid) o; + + // // check fields + // if (this.r != other.r) return false; + // if (this.c != other.c) return false; + + // // check if .name is null + // if (this.name == null) { + // // if (other.name == null) return true; + // // return false; + // return other.name == null; + // } + + // return this.name.equals(other.name); + // } }