ISC Practicals and Theory - Point Distance
Updated 21 Oct 2026
A point on the plane is given by two coordinates, $(x, y)$. This program defines a class Point whose objects hold such a point, and finds the distance between two points and their midpoint. This is the single-class version: a default constructor, an accept() method that reads a point, and a method distance(Point p) that receives the other point 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}$
The midpoint of the line joining them is
$M = \left(\dfrac{x_1 + x_2}{2}, \dfrac{y_1 + y_2}{2}\right)$
For example, for $(0, 0)$ and $(3, 4)$: $d = \sqrt{9 + 16} = 5$ and $M = (1.5, 2)$.
Algorithm
maincreates two objectsp1andp2of the classPoint. The default constructor sets both to $(0, 0)$.maincallsp1.accept("point 1")andp2.accept("point 2"); each readsxandywith aScanner.maincallsdisplay()on each object to print the points.maincallsp1.distance(p2):- it finds
dx = p.x - xanddy = p.y - y; - it returns
Math.sqrt(dx * dx + dy * dy). The squares are made by multiplication, notMath.pow.
- it finds
mainprints the distance withprintf("%.2f"), which gives 2 decimal places.maincallsp1.midpoint(p2), which finds the average of the x-coordinates and of the y-coordinates and prints the point.
Program
Each method does one job: accept() reads, distance() calculates, midpoint() prints the midpoint and display() prints a point. The point p passed to distance() and midpoint() is an object, so its data members are read with p.x and p.y.
import java.util.Scanner;
/**
* Class Point stores a point (x, y) of the plane and finds the distance
* and the midpoint between two points. Main version: default constructor,
* accept() reads the point, and the methods take the other Point object.
*/
public class Point
{
double x; // x-coordinate of the point
double y; // y-coordinate of the point
/**
* Default constructor: places the point at the origin (0, 0).
*/
Point()
{
x = 0;
y = 0;
}
/**
* Reads the x and y coordinates of this point from the keyboard.
* Parameter: name - label of the point shown in the prompt.
*/
void accept(String name)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter x of " + name + ": ");
x = sc.nextDouble();
System.out.print("Enter y of " + name + ": ");
y = sc.nextDouble();
}
/**
* 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
}
/**
* Prints the midpoint of this point and the point p.
* Parameter: p - the other Point object. Returns nothing.
*/
void midpoint(Point p)
{
double mx = (x + p.x) / 2; // x-coordinate of the midpoint
double my = (y + p.y) / 2; // y-coordinate of the midpoint
System.out.printf("Midpoint = (%.2f, %.2f)%n", mx, my);
}
/**
* Displays this point, with 2 decimal places, as (x, y).
*/
void display()
{
System.out.printf("(%.2f, %.2f)%n", x, y);
}
/**
* Creates two Point objects, reads them and prints the results.
*/
public static void main(String args[])
{
Point p1 = new Point(); // first point
Point p2 = new Point(); // second point
p1.accept("point 1");
p2.accept("point 2");
System.out.print("Point 1 = ");
p1.display();
System.out.print("Point 2 = ");
p2.display();
System.out.printf("Distance = %.2f%n", p1.distance(p2));
p1.midpoint(p2);
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
x | double | Data member: x-coordinate of the point |
y | double | Data member: y-coordinate of the point |
name | String | Parameter of accept(): label of the point shown in the prompt |
sc | Scanner | Reads the coordinates typed by the user |
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 |
mx | double | x-coordinate of the midpoint |
my | double | y-coordinate of the midpoint |
p1 | Point | Object for the first point, created in main |
p2 | Point | Object for the second point, created in main |
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, -1)$ and $(2, 3)$. Here p1 is $(-1, -1)$, p2 is $(2, 3)$, and distance() is called as p1.distance(p2).
| Statement | Value |
|---|---|
x, y (of p1) | -1, -1 |
p.x, p.y (of p2) | 2, 3 |
dx = p.x - x | 2 - (-1) = 3 |
dy = p.y - y | 3 - (-1) = 4 |
dx * dx + dy * dy | 9 + 16 = 25 |
Math.sqrt(25) | 5.0, printed as 5.00 |
mx = (x + p.x) / 2 | (-1 + 2) / 2 = 0.5 |
my = (y + p.y) / 2 | (-1 + 3) / 2 = 1.0 |
The program prints Distance = 5.00 and Midpoint = (0.50, 1.00), as in the third 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."
- Perimeter "Find the perimeter of a triangle whose vertices are three
Pointobjects."