From c46b9e1db305f903f32f51095a3e3732e9c0ff71 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 25 Nov 2025 20:03:52 -0800 Subject: [PATCH 1/3] Getting help from classmate --- .../auberonedu/equalsLivecode/Centroid.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java index b0e343a..e947d02 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,40 @@ public int getC() { public String getName() { return name; } + + + @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 + if(o == null) return false; + + if(!this.getClass().equals(o.getClass())) { + return false; + } + + Centroid other = (Centroid) o; + + if (this.r != other.r) return false; + if (this.c != other.c) return false; + + if (this.name == null) { + return other.name == null; + // if(other.name == null) { + // return true; + // } + // else { // other.name is not null + // return false; + // } + } + + return this.name.equals(other.name); + // if (!this.name.equals(other.name)) { + // return false; + // } else { + // return true; + // } + } + } From a83e7f1534b4bdba05a642bdcafc6af82a84c8fb Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 25 Nov 2025 20:07:08 -0800 Subject: [PATCH 2/3] Working well --- .../auberonedu/equalsLivecode/Centroid.java | 91 +++++++++++++------ 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java index e947d02..154deac 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -23,39 +23,70 @@ public String getName() { return name; } - @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 - if(o == null) return false; + 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; + } - if(!this.getClass().equals(o.getClass())) { + @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; - } - - Centroid other = (Centroid) o; - - if (this.r != other.r) return false; - if (this.c != other.c) return false; - - if (this.name == null) { - return other.name == null; - // if(other.name == null) { - // return true; - // } - // else { // other.name is not null - // return false; - // } - } - - return this.name.equals(other.name); - // if (!this.name.equals(other.name)) { - // return false; - // } else { - // return true; - // } + return true; } + + // @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 + // if(o == null) return false; + + // if(!this.getClass().equals(o.getClass())) { + // return false; + // } + + // Centroid other = (Centroid) o; + + // if (this.r != other.r) return false; + // if (this.c != other.c) return false; + + // if (this.name == null) { + // return other.name == null; + // // if(other.name == null) { + // // return true; + // // } + // // else { // other.name is not null + // // return false; + // // } + // } + + // return this.name.equals(other.name); + // // if (!this.name.equals(other.name)) { + // // return false; + // // } else { + // // return true; + // // } + // } + } From 9b5dd92a946488b0772b695a95d880a56d3c62ae Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 25 Nov 2025 20:20:08 -0800 Subject: [PATCH 3/3] Got help from a classmate --- .../io/github/auberonedu/equalsLivecode/App.java | 10 +++++----- .../github/auberonedu/equalsLivecode/Centroid.java | 12 ++++++------ .../auberonedu/equalsLivecode/NamedCentroid.java | 5 +++++ 3 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/github/auberonedu/equalsLivecode/NamedCentroid.java 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 154deac..86c0644 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -29,7 +29,7 @@ public int hashCode() { int result = 1; result = prime * result + r; result = prime * result + c; - result = prime * result + ((name == null) ? 0 : name.hashCode()); + // result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @@ -46,11 +46,11 @@ public boolean equals(Object obj) { 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; + // if (name == null) { + // if (other.name != null) + // return false; + // } else if (!name.equals(other.name)) + // return false; return true; } diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/NamedCentroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/NamedCentroid.java new file mode 100644 index 0000000..46c5aef --- /dev/null +++ b/src/main/java/io/github/auberonedu/equalsLivecode/NamedCentroid.java @@ -0,0 +1,5 @@ +package io.github.auberonedu.equalsLivecode; + +public record NamedCentroid(String name, int r, int c) { + +}