Challenge 4: Calculate Distance Between Points
In this exercise, you have to create a class called Point which can calculate distance between two points in the x-y plane.
We'll cover the following
Problem Statement
You have to implement a class called Point
that represents a specific point in the x-y plane. It should contain the following:
-
fields:
-
x
( integer type) -
y
( integer type)
-
-
methods:
-
default constructor that initializes the point at
-
parameterized constructor that takes input
x
andy
and initializes the point to the respective coordinates. -
float distance()
, a method which calculates the distance of the point (represented by the object) from the origin, i.e. -
float distance(x1, y1)
, a method which calculates the distance between the point represented by the class object and
-
The distance between two points and can be calculated by the following formula:
Sample Input
Point p1 = new Point(5, 5);
Sample Output
distance() => 7.071
distance(2, 1) => 5.0
Get hands-on with 1400+ tech skills courses.