A super class Demand has been defined to store the details of the demands for a product. Define a subclass Supply which contains the production and supply details of the products. 
  The details of the members of both the classes are given below:
  Class name: Demand
  Data members/instance variables:
  pid:string to store the product ID
  pname:string to store the product name
  pdemand: integer to store the quantity demanded for the product
  Methods/Member functions:
  Demand(...) : parameterised constructor to assign values to the data members
  void display():to display the details of the product
  Class name:Supply
  Data members/instance variables:
  pproduced: integer to store the quantity of the product produced
  prate:to store the cost per unit of the product in decimal
  Methods/Member functions:
  Supply(...): parameterised constructor to assign values to the data members of both the classes
  double calculation(): returns the difference between the amount of demand (rate x demand) and the amount produced(rate x produced)
  void display():to display the details of the product and the difference in amount of demand and amount of supply by invoking the method calculation()
  Assume that the super class Demand has been defined. Using the concept of inheritance, specify the class Supply giving the details of the constructor(...), double calculation() and void display().
  The super class, main function and algorithm need NOT be written.
Solution:
,,
 

  
  class Demand
  {
  String pid,pname;int pdemand;
  Demand(String pi,String pn,int pd)
  {
  pid=pi;
  pname=pn;
  pdemand=pd;
  }
  void display()
  {
          System.out.println("product id is :"+ pid);
          System.out.println("product name is :" + pname);
          System.out.println("quantity demanded " + pdemand);
  }
  }
  
      class Supply extends Demand
  {
      int pproduced;
      double prate;
          Supply(String pid,String pname,int pdemand,double pr,int pp)
          {
                      super(pid,pname,pdemand);
                      prate=pr;
                      pproduced=pp;
          }
         double calculation()
         {
             double aod=prate*pdemand;
             double ap=prate*pproduced;
             return aod-ap;
      }
      void display()
      {
      super.display();
      System.out.println("Quantity of the product produced :"+ pproduced);
          System.out.println("cost per unit of the product :"+ prate);
          System.out.println("Difference:"+ calculation());
      }
  }
                           
                                                                    


                                            



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com