Object Passing Program in Java: Point Coordinate Program

A class defines the coordinates of a point in a plane. The details of the class are given below:

class name: point

Instance variables:

int x,y --> define the x and y coordinate

void input(): To accept the x and y coordinates

void calculate(point p): To pass the object which depicts the 2nd coordinate. Then find the distance between the two coordinates.


import java.util.Scanner;
public class Point 
{
    int x,y;
    void input(){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter x coordinate:");
        x=sc.nextInt();
        System.out.println("Enter y coordinate:");
        y=sc.nextInt();
        sc.close();
    }
    void calculate(Point p)
    {
        int x1 = this.x;
        int y1 = this.y;
        int x2 = p.x;
        int y2 = p.y;
        System.out.println("The first coordinate is:("+x1+","+y1+")");
        System.out.println("The second coordinate is:("+x2+","+y2+")");
        int d = (int)Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));
        System.out.println("Approximate distance is "+d+" units");
    }
    public static void main(String[] args) {
        Point m = new Point();
        m.input();
        Point n = new Point();
        n.input();
        m.calculate(n);
    }
}
File Handling Java Notes Class 11 ISC