Solution Review: Calculate Distance Between Points
This review provides a detailed analysis to solve the 'Calculate Distance Between Points' challenge.
We'll cover the following...
Solution
Press + to interact
import java.lang.Math;class Point {// Private fieldsprivate int x;private int y;// Default Constructorpublic Point() {x = 0;y = 0;}// Parameterized Constructorpublic Point(int x, int y) {this.x = x;this.y = y;}// Distance from originpublic double distance() {double distance = Math.sqrt(x*x + y*y);return distance;}// Distance from (x2, y2)public double distance(int x2, int y2) {double distance = Math.sqrt(((x2-x)*(x2-x))+((y2-y)*(y2-y)));return distance;}}class Demo {public static void main(String args[]) {Point p1 = new Point(5, 5);System.out.println(p1.distance());System.out.println(p1.distance(2, 1));}}
Explanation
- We have implemented the
Point
class, which has the data membersx