Coordinates And Point In Java

Write a menu driven program to perform the following using objects of the same class:
i)distance of a given point from the origin
ii)distance between two given points
iii)coordinates of the midpoint of a line joining two points

import java .util.*;
class Point
{ double x,y;
  public Point()
  {x=0;
   y=0;
  }
  public Point(double a,double b)
  {x=a;
   y=b;
  }
  public double distance(Point ob)
  {double d;
   d=Math.sqrt((Math.pow((this.x-ob.x),2))+(Math.pow((this.y-ob.y),2)));  
  return d;
  }//end of distance()
   public double disfromOrigin()
  {double dis;
   dis=Math.sqrt(x*x+y*y);  
   return dis;
  }
   public Point mid(Point ob)
  {Point m;
   m=new Point();
   m.x=(ob.x+this.x)/2;
   m.y=(ob.y+this.y)/2;
   return m;
  }//end of mid()
  public static void main()
  { int c;double u,v,w,z,d;
    Scanner sc=new Scanner(System.in);
    System.out.println("Press 1 for distance between two points, 2 for distance from origin , 3 for mid point between two points");
    c=sc.nextInt();
    if(c==1)
     { System.out.println("enter the coordinates of the 1st point");
        w=sc.nextDouble();
        z=sc.nextDouble();
       System.out.println("enter the coordinates of the 2nd point");
        u=sc.nextDouble();
        v=sc.nextDouble();
        Point ob1=new Point(w,z);
        Point ob2=new Point(u,v);
        d=ob2.distance(ob1);
        System.out.println("the dist is "+d);
    }
    else if(c==2)
    {  System.out.println("enter the coordinates of the point");
        w=sc.nextDouble();
        z=sc.nextDouble();
        Point ob1=new Point(w,z);
        d=ob1.disfromOrigin();
       System.out.println("the dist from origin is "+d);
   
    }
    else if(c==3)
    {System.out.println("enter the coordinates of the 1st point");
        w=sc.nextDouble();
        z=sc.nextDouble();
       System.out.println("enter the coordinates of the 2nd point");
        u=sc.nextDouble();
        v=sc.nextDouble();
        Point ob1=new Point(w,z);
        Point ob2=new Point(u,v);
        Point ob3=new Point();
        ob3=ob1.mid(ob2);
        System.out.println("the mid point is"+"("+ob3.x+","+ob3.y+")");
    }
    else
      System.out.println("Wrong Choice");
   }//end of main
}//end of class

OUTPUT---
Press 1 for distance between two points, 2 for distance from origin , 3 for mid point between two points
1
enter the coordinates of the 1st point
2
4
enter the coordinates of the 2nd point
3
5
the dist is 1.4142135623730951Press 1 for distance between two points, 2 for distance from origin , 3 for mid point between two points
2
enter the coordinates of the point
4
6
the dist from origin is 7.211102550927978
Press 1 for distance between two points, 2 for distance from origin , 3 for mid point between two points
3
enter the coordinates of the 1st point
4
6
enter the coordinates of the 2nd point
3
5
the mid point is(3.5,5.5)

Comments

Popular posts from this blog

Sort Boundary Elements Of A Matrix

Lucky Number In Java ISC

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.