Page 490 - Computer science 868 Class 12
P. 490

class name                         :   Base
                     Data Members
                     double rad                         :   to store radius in decimal
                     Member Functions
                     Base(...)                          :   parameterised constructor to initialise the data member
                     void show()                        :   displays the radius with an appropriate message
                     class name                         :   CalVol
                     Data Member                        :
                     double ht                          :   to store height in decimal
                     Member Functions
                     CalVol(...)                        :  parameterised constructor to initialise the data members of both classes
                     double volume                      :     calculates the volume of a sphere by using the formula (pi × radius2 × height)
                     void show()                        :     displays the data members of both the classes and the volume of the sphere
                                                           with appropriate message
                     Assume that the interface Data and the super class Base has been defined. Using the concept of inheritance, specify the class
                    CalVol giving the details of the constructor(…), double volume() and void show(). The interface, super class, main function, and
                    algorithm need NOT be written.
                Ans. class CalVol extends Base implements Data
                    {
                       double ht;
                       CalVol(double r, double h)
                       {
                          super(r);
                          ht=h;
                       }
                       public double volume()
                       {
                          double x=pi * rad *rad * ht;
                          return x;
                       }
                       void show()
                       {
                          super.show();
                          System.out.println("Height= " + ht);
                          System.out.println("Volume= " + volume());
                       }
                    }
                  8.  Design a class Convert to find the date and the month from a given day number for a particular year. Example: If day number
                    is 64 and the year is 2020, then the corresponding date would be: March 4, 2020 i.e. (31 + 29 + 4 = 64) Some of the members
                    of the class are given below:                                                              [ISC 2020]
                     Classname                          :   Convert
                     Data Members/Instance variables
                     n                                  :   integer to store the day number
                     d                                  :   integer to store the day of the month (date)
                     m                                  :   integer to store the month
                     y                                  :   integer to store the year
                     Methods/Member Functions
                     Convert()                          :   constructor to initialize the data members with legal initial values
                     void accept()                      :   to accept the day number and the year


                488488  Touchpad Computer Science-XII
   485   486   487   488   489   490   491   492   493   494   495