Sunday, October 21, 2012

Java program to find distance between two points in a plane


import java.io.*;
class distance
{
            int x,y,dist;
            distance()
             {  x=0;
              y=0;
 }
  void calc(distance c,distance d)
  {
     int xd=c.x-d.x;
     int xs=xd*xd;
     int yd=c.y-d.y;
     int ys=yd*yd;
     int sum=xs+ys;
     double dist=Math.sqrt(sum);
     System.out.println("The distance is "+dist);
  }
  void read()throws IOException
  { 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the x coordinate");
     x=Integer.parseInt(br.readLine());
     System.out.println("Enter the y coordinate");
     y=Integer.parseInt(br.readLine());
     System.out.println("The entered point is "+"("+x+","+y+")");
  }
  public static void main(String s[])throws IOException
  {
     distance c=new distance();
     distance d=new distance();
     System.out.println("First point");
     c.read();
     System.out.println("Second point");
     d.read();
     d.calc(c,d);
  }
}
Output

2 comments: