Skip to main content

Assignment No:3

//Assignment No:3


Set A:

a)Write a program for multilevel inheritance such that country is inherited from continent. State is inherited from country. Display the place, state, country, and Continent.

CODE:

👇

  1. import java.util.*;
  2. class continent
  3. {
  4. String Cname;
  5. }

  6. class country extends continent
  7. {
  8. String Couname;
  9. }

  10. class state extends country
  11. {
  12. String Sname;
  13. String Pname;

  14. state(String c1, String c2, String c3, String c4)
  15. {
  16. Cname = c1;
  17. Couname = c2;
  18. Sname = c3;
  19. Pname = c4;
  20. }
  21. void display()
  22. {
  23. System.out.println("Continent\tCountry\t\tState\tPlace");
  24. System.out.println(Cname+"\t\t"+Couname+"\t\t"+Sname+"\t"+Pname);
  25. }
  26. public static void main(String[] args) 
  27. {
  28. Scanner sc = new Scanner(System.in);

  29. System.out.println("Enter Continent Name");
  30. String c1 = sc.next();

  31. System.out.println("Enter Country Name");
  32. String c2 = sc.next();

  33. System.out.println("Enter State Name");
  34. String c3 = sc.next();

  35. System.out.println("Enter Place Name:");
  36. String c4 = sc.next();

  37. state s = new state(c1, c2, c3, c4);
  38. s.display();
  39. sc.close(); 
  40. }
  41. }

b) Define an abstract class Staff with protected members id and name. Define a parameterized constructor. Define one subclass OfficeStaff with member department. Create n objects of OfficeStaff and display all deatails.

CODE:

👇

  1. import java.util.*;
  2. abstract class  Staff
  3. {
  4. protected int id;
  5. protected String name;
  6. Staff(int id, String name)
  7. {
  8. this.id = id;
  9. this.name = name;
  10. }
  11. abstract public void display();
  12. }

  13. class OfficeStaff extends Staff
  14. {
  15. String dept;
  16. OfficeStaff(int id, String name, String dept)
  17. {
  18. super(id,name);
  19. this.dept = dept;
  20. }
  21. public void display()
  22. {
  23. System.out.println("\t"+id+"\t"+name+"\t"+dept);
  24. System.out.println("--------------------------------------------");
  25. }
  26. }

  27. public class absclass
  28. {
  29. public static void main(String[] args)
  30. {
  31. Scanner sc = new Scanner(System.in);
  32. System.out.println("Enter Total Number of Employee:");
  33. int n = sc.nextInt();

  34. OfficeStaff s[] = new OfficeStaff[n];
  35. for(int i=0; i<n; i++)
  36. {
  37. System.out.println("\nEnter Number-"+(i+1)+"Emplyoee Details\n");
  38. System.out.println("Enter ID");
  39. int id = sc.nextInt();
  40. System.out.println("Enter Employee Name:");
  41. String name = sc.next();

  42. System.out.println("Enter Dept Name:");
  43. String dept = sc.next();

  44. s[i] = new OfficeStaff(id, name, dept);
  45. }
  46. System.out.println("\t\t\tEmpolyee Details");
  47. System.out.println("-------------------------------------");
  48. System.out.println("\tID\tName\tDept");
  49. System.out.println("-------------------------------------");

  50. for(int i=0; i<n; i++)
  51. {
  52. s[i].display();
  53. }
  54. sc.close();
  55. }
  56. }



c) Define an interface "Operations" which has method area(), volume(). Define a constant PI

having a value 3.142. Create a class cylinder which implements this interface (members - radius, height) Create one object and object and calculate the area and volume.

CODE:

👇

  1. import java.util.*;
  2. interface operation
  3. {
  4. void area();
  5. void volume();
  6. final double PI = 3.142;
  7. }

  8. class cylinder implements operation
  9. {
  10. int r;
  11. int h;
  12. cylinder(int r, int h)
  13. {
  14. this.r = r;
  15. this.h = h;
  16. }
  17. public void area()
  18. {
  19. System.out.println("Area: "+(2*PI*r*h+2*PI*r)+" sq units\n");
  20. }
  21. public void volume()
  22. {
  23. System.out.println("Volume:"+(PI*r*r*h)+" cubic units\n");
  24. }
  25. }
  26. public class CylinderVol
  27. {
  28. public static void main(String[] args)
  29. {
  30. Scanner sc = new Scanner(System.in);

  31. System.out.println("Enter radius:");
  32. int r = sc.nextInt();
  33. System.out.println("Enter Height:");
  34. int h = sc.nextInt();

  35. cylinder c = new cylinder(r,h);
  36. c.area();
  37. c.volume();
  38. sc.close();
  39. }
  40. }




d) Write a program to find the cube of given number using function interface.

CODE:

👇

  1. import java.util.*;
  2. interface function
  3. {
  4. void cube(int n);
  5. }

  6. class demo implements function
  7. {
  8. public void cube(int n)
  9. {
  10. System.out.println("Cube: "+(n*n*n));
  11. }
  12. }

  13. public class funinterface
  14. {
  15. public static void main(String[] args) 
  16. {
  17. Scanner sc = new Scanner(System.in);
  18. System.out.println("Enter Number for Finding Cube:");
  19. int n = sc.nextInt();
  20. demo d = new demo();
  21. d.cube(n);
  22. sc.close();
  23. }
  24. }


Set B:
    a) Create an abstract class "order" having members id, description. Create two subclasses "Purchase Order" and "Sales Order" having members customer name and Vendor name respectively. Define methods accept and display in all cases. Create 3 objects each of Purchase Order and Sales Order and accept and display deatils.
 CODE:

👇

  1. import java.util.*;
  2. abstract class order
  3. {
  4. int id;
  5. String descp;
  6. Scanner sc=new Scanner(System.in);

  7. public void setData(int id,String descp)
  8. {
  9.  this.id=id;
  10.  this.descp=descp;
  11.  }
  12.  
  13.  abstract public void accept();
  14.  abstract public void display();
  15.  }
  16.  
  17.  class purchase_order extends order
  18.  {
  19.   String cname;
  20.    
  21.    public void accept()
  22.   {
  23.   System.out.println("Enter Customer Name :");
  24.   String n=sc.nextLine();
  25.   cname=n;
  26.   }
  27.   
  28.   public void display()
  29.   {
  30.     System.out.println("\t"+id+"\t"+descp+"\t\t"+cname);
  31.     }
  32.  }
  33.  
  34.  class sales_order extends order
  35.  {
  36.    String vname;
  37.    
  38.  public void accept()
  39.  {
  40.    System.out.println("Enter Vendor name:");
  41.    String n=sc.nextLine();
  42.    vname=n;
  43.    }
  44.    
  45.    public void display()
  46.    {
  47.        System.out.println("\t"+id+"\t"+descp+"\t\t"+vname);
  48.     }
  49.  }  
  50.  
  51. public class Sales
  52. {
  53.    public static void main(String[]args)
  54.     {
  55.       Scanner sc=new Scanner(System.in);
  56.       purchase_order p[]=new purchase_order[3];
  57.        for(int i=0; i<3; i++)
  58.          {
  59.    p[i]=new purchase_order();
  60.    System.out.println("\nEnter "+(i+1)+" Customer data : ");
  61.    System.out.println("Enter ID:");
  62.    int cid=sc.nextInt();
  63.    System.out.println("Enter Description: ");
  64.    String desc=sc.next();
  65.    sc.nextLine();
  66.    p[i].setData(cid,desc);
  67.    p[i].accept();
  68.           }
  69.       System.out.println("\n\t\tPurchased Details.\n");
  70.       System.out.println("\tID\tDescription\tCname");
  71.       for(int i=0;i<3;i++)
  72. {
  73.    p[i].display();
  74.   }
  75.       
  76.    sales_order s[]=new sales_order[3];
  77.    for(int i=0; i<3; i++)
  78.    {
  79.          s[i]=new sales_order();
  80.           System.out.println("\nEnter "+(i+1)+" Vender data : ");
  81.            System.out.println("Enter ID:");
  82.            int cid=sc.nextInt();
  83.            System.out.println("Enter Description: ");
  84.            String desc=sc.next();
  85.            sc.nextLine();
  86.            s[i].setData(cid,desc);
  87.            s[i].accept();
  88.            }
  89.           System.out.println("\n\t\tSales Details.\n");
  90.           System.out.println("\tID\tDescription\tVname");        
  91.          for(int i=0;i<3;i++)
  92.      {
  93.        s[i].display();
  94.       }
  95.         sc.close();
  96.       }
  97.  }             

    

   

b) Write a program to using market interface create a class product(product_id, product_name, product_cost, product_quantity) define a default and parameterized constructor. Create objects of class product and display the cotents of each object and Also display the object count. 

  1. import java.util.*;
  2. interface MarkerInt
  3. {
  4. }

  5. class product implements MarkerInt
  6. {
  7.   int pid,pcost,quantity;
  8.   String pname;
  9.   static int cnt ;
  10.   
  11.   //Default constructor
  12.   
  13.   product()
  14.   {
  15.     pid=1;
  16.     pcost=10;
  17.     quantity=1;
  18.     pname="pencil";
  19.     cnt++;
  20.     }
  21.     
  22.   //Parameterized constructor
  23.   
  24.  product(int id, String n, int c, int q)
  25. {
  26. pid=id;
  27. pname=n;
  28. pcost=c;
  29. quantity=q;
  30. cnt++;
  31. System.out.println("\nCOUNT OF OBJECT IS: "+cnt+ "\n");
  32. }   
  33.  
  34. public void display()
  35.   { 
  36. System.out.println("\t"+pid+"\t"+pname+"\t"+pcost+"\t"+quantity);
  37.   }
  38. }

  39. public class MarkerInterface
  40. {
  41.   public static void main(String[]args)
  42.   {
  43.    Scanner sc=new Scanner(System.in);
  44.     System.out.println("Enter number of Product: ");
  45.     int n=sc.nextInt();
  46.     
  47.     product pr[]=new product[n];
  48.     for(int i=0; i<n; i++)
  49.       {
  50.         System.out.println("\nEnter "+(i+1)+ " Product details :\n");
  51.         System.out.println("Enter product ID");
  52.         int pid=sc.nextInt();
  53.         sc.nextLine();
  54.         System.out.println("Enter product name : ");
  55.         String pn=sc.next();
  56.         sc.nextLine();
  57.         System.out.println("Enter product cost : ");
  58.         int pc=sc.nextInt();
  59.         System.out.println("Enter product quatity :");
  60.         int pq=sc.nextInt();
  61.         pr[i]=new product(pid,pn,pc,pq);
  62.        }
  63.     System.out.println("\n\t\tProduct details\n");
  64.      System.out.println("\tId\tPname\tCost\tQuantity\n");
  65.      for(int i=0; i<n; i++)
  66.      {
  67.      pr[i].display();
  68.      }
  69.    sc.close();
  70.   }
  71. }