Search This Blog

Sunday, August 5, 2012

Passing object as parameter: Calculate distance between two points Coordinate Geometry

class sukhman
{
    public static void main(String args[])
    {
       point conobj=new point(1,1);  //parametrized constructor will get invoked
       point ob=new point();         //object 1   
       point ob1=new point();        //object 2
       ob.translator(-2,-3);
       ob1.translator(-4,4);
       double ans=ob.distance(ob1);   //passing object as parameter
       System.out.println("The distance between two points is = "+ans);
    }
}


class point
{
    int x,y;
    point()
    {
        x=0;
        y=0;
    }
    point(int a,int b)
    {
        x=a;
        y=b;
    }
    void translator(int xp, int yp)
    {
        x=xp;
        y=yp;
    }
    double distance(point obj)
    {
        double xs=Math.pow((obj.x-x),2);
        double ys=Math.pow((obj.y-y),2);
        double d=Math.sqrt(xs+ys);   //formula in coordinate geometry
        return d;
    }
}

OUTPUT:
The distance between two points is = 7.280109889280518

No comments:

Post a Comment