From 60a92911100cd523c59d8ce036899ea82a97f203 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Tue, 25 Nov 2025 11:48:33 -0800 Subject: [PATCH] Live code class --- .../github/auberonedu/equalsLivecode/App.java | 14 ++-- .../auberonedu/equalsLivecode/Centroid.java | 70 +++++++++++++++++++ .../equalsLivecode/NamedCentroid.java | 4 ++ 3 files changed, 82 insertions(+), 6 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..013612e 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, null); - 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 @@ -88,10 +87,13 @@ public static void videoDemo() { Set salamanderLocations = finder.allLocations(video, "Salamander"); Set foodLocations = finder.allLocations(video, "Food"); + //5 System.out.println("Number of locations the salamander visited " + salamanderLocations.size()); + //1 System.out.println("Number of locations the food was at " + foodLocations.size()); boolean hasOverlap = !Collections.disjoint(salamanderLocations, foodLocations); + // true System.out.println("Do the salamander locations overlap with the food locations: " + hasOverlap); } diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java index b0e343a..413ee12 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -22,4 +22,74 @@ 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(r != other.r) return false; + // if(c != other.c) return false; + + // if(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; + // // } + + + + @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; + } + + } 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..72960f7 --- /dev/null +++ b/src/main/java/io/github/auberonedu/equalsLivecode/NamedCentroid.java @@ -0,0 +1,4 @@ +package io.github.auberonedu.equalsLivecode; + +public record NamedCentroid(String name, int r, int c) { +}