diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/App.java b/src/main/java/io/github/auberonedu/equalsLivecode/App.java index 44717b0..5b47669 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/App.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/App.java @@ -11,87 +11,89 @@ 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(2, 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 + // We will look at this a bit later in the livecode, please ignore for first + // part public static void videoDemo() { List video = List.of( - // Frame 0: Salamander is at [2, 3] (all locations in [row, column] format) - // Food is at [4, 5] - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','s','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','f','.',}, - {'.','.','.','.','.','.','.',}}, - - // Frame 2: Salamander is at [2, 4] - // Food is at [4, 5] - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','s','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','f','.',}, - {'.','.','.','.','.','.','.',}}, - - // Frame 3: Salamander is at [2, 4] (it didn't move) - // Food is at [4, 5] - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','s','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','f','.',}, - {'.','.','.','.','.','.','.',}}, - - - // Frame 4: Salamander is at [3, 4] - // Food is at [4, 5] - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','s','.','.',}, - {'.','.','.','.','.','f','.',}, - {'.','.','.','.','.','.','.',}}, - - // Frame 5: Salamander is at [4, 4] - // Food is at [4, 5] - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','s','f','.',}, - {'.','.','.','.','.','.','.',}}, - - // Frame 6: Salamander is at [4, 5] - // No food present (the salamander ate it!) - new char[][]{{'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','.','.',}, - {'.','.','.','.','.','s','.',}, - {'.','.','.','.','.','.','.',}} - ); + // Frame 0: Salamander is at [2, 3] (all locations in [row, column] format) + // Food is at [4, 5] + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', 's', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', 'f', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }, + + // Frame 2: Salamander is at [2, 4] + // Food is at [4, 5] + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', 's', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', 'f', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }, + + // Frame 3: Salamander is at [2, 4] (it didn't move) + // Food is at [4, 5] + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', 's', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', 'f', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }, + + // Frame 4: Salamander is at [3, 4] + // Food is at [4, 5] + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', 's', '.', '.', }, + { '.', '.', '.', '.', '.', 'f', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }, + + // Frame 5: Salamander is at [4, 4] + // Food is at [4, 5] + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', 's', 'f', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }, + + // Frame 6: Salamander is at [4, 5] + // No food present (the salamander ate it!) + new char[][] { { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', '.', '.', }, + { '.', '.', '.', '.', '.', 's', '.', }, + { '.', '.', '.', '.', '.', '.', '.', } }); Map legend = Map.of( - "Salamander", 's', - "Food", 'f' - ); + "Salamander", 's', + "Food", 'f'); Finder finder = new Finder(legend); 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..1c3ab58 100644 --- a/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java +++ b/src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java @@ -4,7 +4,7 @@ public class Centroid { private int r; private int c; private String name; - + public Centroid(int r, int c, String name) { this.r = r; this.c = c; @@ -22,4 +22,72 @@ 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) { + // // is this equal to o? + // // ensure r, c, and name are all equal between this and o + // // ensure we meet Java eqauls contract + // if (o == null) + // return false; + + // if (!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 { + // // return false; + // // } + // } + + // return name.equals(other.name); + // // if (!name.equals(other.name)) { + // // return false; + // // } else { + // // return true; + // // } + + // } } diff --git a/src/main/java/io/github/auberonedu/equalsLivecode/NameCentroid.java b/src/main/java/io/github/auberonedu/equalsLivecode/NameCentroid.java new file mode 100644 index 0000000..bb0783f --- /dev/null +++ b/src/main/java/io/github/auberonedu/equalsLivecode/NameCentroid.java @@ -0,0 +1,5 @@ +package io.github.auberonedu.equalsLivecode; + +public record NameCentroid(String name, int r, int c) { + +}