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
28 changes: 24 additions & 4 deletions src/main/java/io/github/auberonedu/equalsLivecode/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@ public static void main(String[] args) {
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)); // comparing reference
System.out.println("Result of locA.equals(locB) " + locA.equals(locB)); // hopefully value equality (if rewritten in Centroid.java)

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

System.out.println(locA.hashCode());
System.out.println(locB.hashCode());

System.out.println(centroids.contains(locB));


System.out.println("--- Records ---");
Location a = new Location(5, 1, "food");
Location b = new Location(5, 1, "food");

System.out.println(a.equals(b));
System.out.println(a.hashCode());
System.out.println(b.hashCode());


//videoDemo();
}

// We will look at this a bit later in the livecode, please ignore for first part

public static void videoDemo() {
List<char[][]> video = List.of(
// Frame 0: Salamander is at [2, 3] (all locations in [row, column] format)
Expand Down Expand Up @@ -94,5 +114,5 @@ public static void videoDemo() {
boolean hasOverlap = !Collections.disjoint(salamanderLocations, foodLocations);
System.out.println("Do the salamander locations overlap with the food locations: " + hasOverlap);
}

}
53 changes: 53 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,57 @@ 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
// // allowed to access private fields of different objects in the same class
// public boolean equals(Object o) {
// if (o == null) return false; // checking if o is null -- false
// if (getClass() != o.getClass()) return false; // checking if o is not a Centroid -- false

// Centroid other = (Centroid) o; // casting o as a Centroid

// // compare fields
// if(r != other.r) return false;
// if(c != other.c) return false;
// 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) {
}