ISC Practicals and Theory - Point Distance (OOP)
Updated 21 Oct 2026
This version of the distance-between-two-points program uses a parameterised constructor Point(double x, double y) and a method midpoint(Point p) that **returns a new Point object**. The coordinates are read in main, the objects are created from them, and the other point is passed to the methods as an object.
Definition
The distance between $P_1(x_1, y_1)$ and $P_2(x_2, y_2)$ is $d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$, and the midpoint is $\left(\dfrac{x_1 + x_2}{2}, \dfrac{y_1 + y_2}{2}\right)$. For $(0, 0)$ and $(3, 4)$ the distance is $5$ and the midpoint is $(1.5, 2)$.
Algorithm
mainreadsx1,y1,x2,y2with aScanner.maincreatesp1 = new Point(x1, y1)andp2 = new Point(x2, y2). The constructor copies the parameters into the data members usingthis.x = x.maincallsp1.midpoint(p2). The method builds a newPointfrom the averages of the coordinates and returns it;mainkeeps it inmid.maindisplaysp1andp2withdisplay().p1.distance(p2)findsdx = p.x - xanddy = p.y - yand returnsMath.sqrt(dx * dx + dy * dy). The squares are made by multiplication, notMath.pow.mainprints the distance withprintf("%.2f")and then displays the objectmid.
Program
Unlike the default-constructor version, there is no accept(): the object gets its values when it is created. In midpoint() a new object is returned, so the original points are not changed.
import java.util.Scanner;
/**
* Class Point stores a point (x, y) of the plane. OOP version:
* parameterised constructor, and midpoint() returns a NEW Point object.
*/
public class Point
{
double x; // x-coordinate of the point
double y; // y-coordinate of the point
/**
* Parameterised constructor: sets the coordinates of the point.
* Parameters: x, y - the coordinates.
*/
Point(double x, double y)
{
this.x = x;
this.y = y;
}
/**
* Finds the distance between this point and the point p
* using sqrt((x2 - x1)^2 + (y2 - y1)^2).
* Parameter: p - the other Point object.
* Returns: the distance as a double.
*/
double distance(Point p)
{
double dx = p.x - x; // difference of the x-coordinates
double dy = p.y - y; // difference of the y-coordinates
return Math.sqrt(dx * dx + dy * dy); // squares by multiplication
}
/**
* Finds the midpoint of this point and the point p.
* Parameter: p - the other Point object.
* Returns: a new Point object holding the midpoint.
*/
Point midpoint(Point p)
{
return new Point((x + p.x) / 2, (y + p.y) / 2);
}
/**
* Displays this point, with 2 decimal places, as (x, y).
*/
void display()
{
System.out.printf("(%.2f, %.2f)%n", x, y);
}
/**
* Reads the coordinates, creates the Point objects and prints the results.
*/
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter x of point 1: ");
double x1 = sc.nextDouble(); // x of the first point
System.out.print("Enter y of point 1: ");
double y1 = sc.nextDouble(); // y of the first point
System.out.print("Enter x of point 2: ");
double x2 = sc.nextDouble(); // x of the second point
System.out.print("Enter y of point 2: ");
double y2 = sc.nextDouble(); // y of the second point
Point p1 = new Point(x1, y1); // first point
Point p2 = new Point(x2, y2); // second point
Point mid = p1.midpoint(p2); // new object: the midpoint
System.out.print("Point 1 = ");
p1.display();
System.out.print("Point 2 = ");
p2.display();
System.out.printf("Distance = %.2f%n", p1.distance(p2));
System.out.print("Midpoint = ");
mid.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
x | double | Data member: x-coordinate of the point (also the constructor parameter) |
y | double | Data member: y-coordinate of the point (also the constructor parameter) |
p | Point | Parameter of distance() and midpoint(): the other point |
dx | double | Difference of the x-coordinates, p.x - x |
dy | double | Difference of the y-coordinates, p.y - y |
sc | Scanner | Reads the coordinates typed by the user |
x1 | double | x-coordinate of the first point, read in main |
y1 | double | y-coordinate of the first point, read in main |
x2 | double | x-coordinate of the second point, read in main |
y2 | double | y-coordinate of the second point, read in main |
p1 | Point | Object for the first point |
p2 | Point | Object for the second point |
mid | Point | New object returned by midpoint(): the midpoint |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter x of point 1: 0
Enter y of point 1: 0
Enter x of point 2: 3
Enter y of point 2: 4
Point 1 = (0.00, 0.00)
Point 2 = (3.00, 4.00)
Distance = 5.00
Midpoint = (1.50, 2.00)Sample 2
Enter x of point 1: 2
Enter y of point 1: 2
Enter x of point 2: 2
Enter y of point 2: 2
Point 1 = (2.00, 2.00)
Point 2 = (2.00, 2.00)
Distance = 0.00
Midpoint = (2.00, 2.00)Sample 3
Enter x of point 1: -1
Enter y of point 1: -1
Enter x of point 2: 2
Enter y of point 2: 3
Point 1 = (-1.00, -1.00)
Point 2 = (2.00, 3.00)
Distance = 5.00
Midpoint = (0.50, 1.00)Sample 4
Enter x of point 1: 1.5
Enter y of point 1: -2
Enter x of point 2: -3.5
Enter y of point 2: 4
Point 1 = (1.50, -2.00)
Point 2 = (-3.50, 4.00)
Distance = 7.81
Midpoint = (-1.00, 1.00)Dry run
Take the input $(1.5, -2)$ and $(-3.5, 4)$.
| Statement | Value |
|---|---|
p1 | (1.5, -2.0) |
p2 | (-3.5, 4.0) |
midpoint(p2): (x + p.x) / 2 | (1.5 + (-3.5)) / 2 = -1.0 |
midpoint(p2): (y + p.y) / 2 | (-2 + 4) / 2 = 1.0 |
dx = p.x - x | -3.5 - 1.5 = -5.0 |
dy = p.y - y | 4 - (-2) = 6.0 |
dx * dx + dy * dy | 25 + 36 = 61 |
Math.sqrt(61) | 7.8102..., printed as 7.81 |
The program prints Distance = 7.81 and Midpoint = (-1.00, 1.00), as in the fourth sample run.
Variations
- Triangle area "Write a program using the class
Pointto find the area of a triangle given three points." - Collinear points "Write a program to check whether three given points lie on one straight line."