Page 477 - Computer science 868 Class 12
P. 477

Class name                             :  D3Point
                        Data Members
                        double z                              :   To store the value of z co ordinate
                        D3Point(double nx, double ny, double nz)   :     Constructor to assign variables to the data members of both base and
                                                                 derived classes
                        double distance2D(D2Point b)          :  To return distance between point object b and the current point object in
                                                                 space using formula
                                                                        2
                                                                                         2
                                                                                 2
                                                                   (x −  x 1 ) + (y −  y 1 ) + (z −  z 1 )  assuming (x , y , z ) and (x , y , z ) as the
                                                                                    2
                                                                                                   1
                                                                                                      1
                                                                                                                   2
                                                                            2
                                                                    2
                                                                                                                 2
                                                                                                        1
                                                                                                              2
                                                                 two points
                   Ans. class d2Point
                       { double x,y;
                         d2Point()
                         { x=y=0.0;}
                         d2Point(double nx,double ny)
                         { x=nx; y=ny;}
                         double distance2d(d2Point b)
                         {  double d= Math.sqrt(Math.pow((b.x-this.x),2)+Math.pow((b.y-this.y),2));
                            System.out.println("Distance ("+b.x+","+b.y+") & ("+this.x+","
                            +this.y+")="+d);
                            return d;
                           }
                       }
                       class d3Point extends d2Point
                       { double z;
                         d3Point()
                         { super();
                            z=0.0;}
                         d3Point(double nx,double ny,double nz)
                         { super(nx,ny);
                           z=nz;}
                         double distance3d(d3Point b)
                         {  double d1=distance2d(b);
                            double d= Math.sqrt(d1*d1 +Math.pow((b.z-this.z),2));
                            System.out.println("Distance ("+b.x+","+b.y+","+b.z+") & ("+this.x+","
                            +this.y+","+this.z+")= "+d);
                            return d;
                           }
                         public static void main(double a1,double b1,double c1,
                         double a2,double b2,double c2)
                         { d3Point oba=new d3Point(a1,b1,c1);
                           d3Point obb=new d3Point(a2,b2,c2);
                           oba=oba.distance3d(obb);// current oba , b object obb
                         }
                       }














                                                                                                                       475
                                                                                  Inheritance, Interfaces and Polymorphism  475
   472   473   474   475   476   477   478   479   480   481   482