Page 487 - Computer science 868 Class 12
P. 487

super.display( );
                      System.out.println(calculation( ));
                      }
                      }
                    4.  A super class Item has been defined to store the details of an item sold by a wholesaler to a retailer. Define a subclass Taxable
                       to compute the total amount paid by the retailer along with service tax. Some of the members of both the classes are given
                       below.                                                                                    [ISC 2022]
                        Class name                            :   Item
                        Data Members
                        name                                  :   to store the name of the item
                        code                                  :   integer to store the item code
                        amount                                :   to store the total sale amount of the item  (in decimals)
                        Member Functions
                        Item(...)                             :   parameterised constructor to assign value to the data members
                        void show()                           :   to display item details

                        Class name                            :   Taxable
                        Data Members
                        tax                                   :   to store service tax (in decimals)
                        taxamt                                :   to store total amount (in decimals)
                        Member Functions
                        Taxable(...)                          :    Parameterised constructor to  assign  values  to  the  data  members of
                                                                 both classes
                        void cal_tax()                        :     to calculate the service tax @ 10.2% of the total sale amount calculates
                                                                 the total amount paid by the retailer as total sale amount + service tax
                        void show()                           :   to display the item details along with the total amount
                        Assume that the super class Item has been defined. Using the concept of inheritance, specify the class Taxable giving details of
                       the constructor, void cal_tax( ) and void show(). The super class, main function and algorithm need NOT be written.
                   Ans. class Item
                      { protected String name;
                        protected int code;
                        protected double amount;
                        Item(String n,int c, double a)//constructor
                        { name=n;
                          code=c;
                          amount=a;
                        }
                        void show() //display
                        { System.out.println("Name "+name);
                          System.out.println("Code "+code);
                          System.out.println("Amount "+amount);
                        }
                      }

                      class Taxable extends Item
                      { double tax,taxamt;
                        Taxable(String n,int c,double a)
                        { super(n,c,a);
                          tax=taxamt=0.0;
                        }
                        void cal_tax()
                        { tax=amount*0.102;
                          taxamt=amount+tax;
                        }



                                                                                                                       485
                                                                                  Inheritance, Interfaces and Polymorphism  485
   482   483   484   485   486   487   488   489   490   491   492