Last active
January 27, 2021 20:02
-
-
Save bfdes/b79379ec16642065e12c9a95d4ceb539 to your computer and use it in GitHub Desktop.
Java 8 VS Java 15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Point { | |
public final double x; | |
public final double y; | |
public Point(double x, double y) { | |
this.x = x; | |
this.y = y; | |
} | |
@Override | |
public boolean equals(Object that) { | |
if(that == this) | |
return true; | |
if(that == null || that.getClass() != getClass()) | |
return false; | |
var q = (Point) that; | |
if(x != q.x) return false; | |
return y == q.y; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 17; // non-zero constant | |
hash = 31*hash + ((Double) x).hashCode(); | |
hash = 31*hash + ((Double) y).hashCode(); | |
return hash; | |
} | |
public double distTo(Point q) { | |
double dx = x - q.x; | |
double dy = y - q.y; | |
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); | |
} | |
} | |
public record Point(double x, double y) { | |
public double distTo(Point q) { | |
var dx = x - q.x; | |
var dy = y - q.y; | |
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment