Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main reads x1, y1, x2, y2 with a Scanner.
  2. main creates p1 = new Point(x1, y1) and p2 = new Point(x2, y2). The constructor copies the parameters into the data members using this.x = x.
  3. main calls p1.midpoint(p2). The method builds a new Point from the averages of the coordinates and returns it; main keeps it in mid.
  4. main displays p1 and p2 with display().
  5. p1.distance(p2) finds dx = p.x - x and dy = p.y - y and returns Math.sqrt(dx * dx + dy * dy). The squares are made by multiplication, not Math.pow.
  6. main prints the distance with printf("%.2f") and then displays the object mid.

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

VariableData TypePurpose
xdoubleData member: x-coordinate of the point (also the constructor parameter)
ydoubleData member: y-coordinate of the point (also the constructor parameter)
pPointParameter of distance() and midpoint(): the other point
dxdoubleDifference of the x-coordinates, p.x - x
dydoubleDifference of the y-coordinates, p.y - y
scScannerReads the coordinates typed by the user
x1doublex-coordinate of the first point, read in main
y1doubley-coordinate of the first point, read in main
x2doublex-coordinate of the second point, read in main
y2doubley-coordinate of the second point, read in main
p1PointObject for the first point
p2PointObject for the second point
midPointNew object returned by midpoint(): the midpoint
argsString[]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)

One value per line

Dry run

Take the input $(1.5, -2)$ and $(-3.5, 4)$.

StatementValue
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 - y4 - (-2) = 6.0
dx * dx + dy * dy25 + 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

  1. Triangle area "Write a program using the class Point to find the area of a triangle given three points."
  2. Collinear points "Write a program to check whether three given points lie on one straight line."