What is the correct way to calculate the distance between two points using the Java distance formula, and how can I fix my current implementation?

Another cleaner and more readable way — especially when you’re new to Java math operations is to use Math.pow():

double distance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));

This approach makes it obvious that you’re squaring the differences. And it’s especially useful if you’re going to reuse this logic or teach it to someone.