Page 471 - Computer science 868 Class 12
P. 471

8         void show()
                   9         { System.out.println("x= "+x+" n= "+n);}

                   10      }
                   11      class CalSeries extends Series

                   12      { double s;
                   13        CalSeries(int xx,int nn) // constructor

                   14        { super(xx,nn);
                   15          s=0.0;
                   16        }

                   17        void calculate() // non abstract method

                   18        { double t;
                   19          for(int i=1;i<=n;i++)
                   20          { t=Math.pow(x,i)/i;

                   21            if(i!=n) // printing terms
                   22              System.out.print(t+" + ");

                   23            else
                   24              System.out.print(t+" = ");

                   25          s=s+t; //calculating sum
                   26        }

                   27      }
                   28      void show()

                   29      { super.show();
                   30        calculate();

                   31        System.out.print(s);
                   32      }

                   33      public static void main(int xx,int nn)
                   34      { CalSeries ob=new CalSeries(xx,nn);

                   35        ob.show();
                   36        }

                   37      }
                 The output of the preceding program is as follows:












                                                                                                                       469
                                                                                  Inheritance, Interfaces and Polymorphism  469
   466   467   468   469   470   471   472   473   474   475   476