Thursday, 27 August 2015

S6P2

Create an interface Shape2D which declares a getArea() method. Point 3D contains coordinates of a point. The abstract class Shape declares abstract display() method and is extended by Circle class. it implements the Shape2D interface. The Shapes class instantiates this class and exercises its methods.


Code:

 interface Shape2D  
 {  
      void getArea();  
 }  
 abstract class Shape  
 {  
      abstract void display();  
 }  
 class Point3D  
 {  
      double x,y,z;  
      Point3D(double x,double y,double z)  
      {  
           this.x = x;  
           this.y = y;  
           this.z = z;  
      }  
 }  
 class Circle extends Shape implements Shape2D  
 {  
      Point3D CenterPoint, OtherPoint;  
      double area;  
      Circle(Point3D P1, Point3D P2)  
      {  
           CenterPoint = P1;  
           OtherPoint = P2;  
      }  
      public void getArea()  
      {       
           double d1 = CenterPoint.x - OtherPoint.x;  
           double d2 = CenterPoint.y - OtherPoint.y;  
           double d = (d1*d1) + (d2*d2);  
           double radius = Math.sqrt(d);  
           area = Math.PI*radius*radius;  
      }  
      public void display()  
      {  
           System.out.println("\nGiven points-> Center Point: ("+CenterPoint.x+", "+CenterPoint.y+", "+CenterPoint.z+") , Other Point: ("+OtherPoint.x+", "+OtherPoint.y+", "+OtherPoint.z+")\nArea for a circle having given points: "+area+" __\n");  
      }  
 }  
 class S6P2_Shapes_Class  
 {  
      public static void main(String args[])  
      {  
           Point3D point1 = new Point3D(7,4,6);  
           Point3D point2 = new Point3D(5,2,8);  
           Circle obj = new Circle(point1 , point2);  
           obj.getArea();  
           obj.display();  
      }  
 }  


Output:

No comments:

Post a Comment