Java Program to Find Mid-Point of a Line

In the previous article, we have seen Java Program to Count of Different Straight Lines with Total n Points With m collinear

In this article we will discuss about how to find mid-point of a line using Java programming language.

Java Program to Find Mid-Point of a Line

Before jumping into the program directly, let’s first know how can we find mid-point of a line.

Explanation:

Formula to Find Mid-Point Of A Line: {(x1+x2)/2 , (y1+y2)/2}

Where,

  • x1 and x2 are the points on x-coordinate.
  • y1 and y2 are the points on y-coordinate.

Example:

When coordinates of the points are ( -3,1) and (4,-5)

Mid point = ((x1+x2)/2 , (y1+y2)/2)

=> ((-3+4)/2 , (1-5)/2)

=> (0.5,-2)

Let’s see different ways to find mid-point of a line.

Method-1: Java Program to Find Mid-Point of a Line By Using Static Input Values

Approach:

  1. Declare the value for both point coordinates(i.e. x and y coordinate).
  2. Then call the midPoint() method by passing x1 and x2 value as parameter.
  3. Repeat the above step for y1 and y2.
  4. In this method the mid point will be calculated using the formula (a+b)/2 for each axis.
  5. Then print the result.

Program:

// JAVA Code to Find Mid-Point of a Line
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        
        double x1 = -3 , y1 = 1, x2 = 4,y2 = -5;
        // Prints the mid point
        System.out.println("The mid-point coordinates are ("+midPoint(x1,x2)+","+midPoint(y1,y2)+")");
    }

    // Returns the mid point between two points
    public static double midPoint(double coordinate1, double coordinate2)
    {
        double mid = (coordinate1+coordinate2)/2;
        return mid;
    }
} 

Output:

The mid-point coordinates are (0.5,-2.0)

Method-2: Java Program to Find Mid-Point of a Line By Using User Input Values

Approach:

  1. Take user input for both points coordinates.
  2. Then call the midPoint() method by passing x1 and x2 value as parameter.
  3. Repeat the above step for y1 and y2.
  4. In this method the mid point will be calculated using the formula (a+b)/2 for each axis.
  5. Then print the result.

Program:

// JAVA Code to Find Mid-Point of a Line

import java.util.Scanner;

public class Main
{
    public static void main(String[] args){
        // Scanner class to take input from user
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first coordinate : ");
        double x1 = scan.nextDouble(), y1=scan.nextDouble();
        System.out.print("Enter second coordinate : ");
        double x2 = scan.nextDouble(), y2=scan.nextDouble();
        // Prints the mid point
        System.out.println("The mid-point coordinates are ("+midPoint(x1,x2)+","+midPoint(y1,y2)+")");
    }

    // Returns the mid point between two points
    public static double midPoint(double coordinate1, double coordinate2)
    {
        double mid = (coordinate1+coordinate2)/2;
        return mid;
    }
}
Output:

Enter first coordinate : -1 2
Enter second coordinate : 3 -6
The mid-point coordinates are (1.0,-2.0)

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Articles: