Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/main/java/io/github/auberonedu/equalsLivecode/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ public static void main(String[] args) {

Centroid locA = new Centroid(4, 9, "Salamander");
Centroid locB = new Centroid(4, 9, "Salamander");
Centroid locC = locA; // Assignment operator; thing on the left points to the thing on the right

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)); // false
System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); // false
System.out.println("Result of locA == locA " + (locA == locA)); // true
System.out.println("Result of locA == locC " + (locA == locC)); // true

videoDemo();
Set<Centroid> centroids = new HashSet<>();
centroids.add(locA);
System.out.println(centroids.contains(locB));

Location a = new Location(1, 5, "Salamander");
Location b = new Location(1, 5, "Salamander");
System.out.println("Locations a and b are the same: " + a.equals(b));

// videoDemo();
}

// We will look at this a bit later in the livecode, please ignore for first part
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/io/github/auberonedu/equalsLivecode/Centroid.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,61 @@ 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()); // Strings have their own 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) {
// // if null, return false
// if (o == null) return false;
// // if a different class, return false
// if(getClass() != o.getClass()) return false;

// // At this point, we can assume these are both non null and the same class
// Centroid other = (Centroid) o; // casting (Type)

// // Check each part (this can be more concise, but we're making it more readable)
// // these primitive ints cannot be null
// if (r != other.r) return false;
// if (c != other.c) return false;
// // name might be null, so needs handled differently
// if (name == null) {
// if (other.name == null) return true;
// if (other.name != null) return false;
// }
// if (!name.equals(other.name)) return false;

// return true;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.auberonedu.equalsLivecode;

public record Location(int x, int y, String label) {
}