//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:
👇
- import java.util.*;
- class continent
- {
- String Cname;
- }
- class country extends continent
- {
- String Couname;
- }
- class state extends country
- {
- String Sname;
- String Pname;
- state(String c1, String c2, String c3, String c4)
- {
- Cname = c1;
- Couname = c2;
- Sname = c3;
- Pname = c4;
- }
- void display()
- {
- System.out.println("Continent\tCountry\t\tState\tPlace");
- System.out.println(Cname+"\t\t"+Couname+"\t\t"+Sname+"\t"+Pname);
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter Continent Name");
- String c1 = sc.next();
- System.out.println("Enter Country Name");
- String c2 = sc.next();
- System.out.println("Enter State Name");
- String c3 = sc.next();
- System.out.println("Enter Place Name:");
- String c4 = sc.next();
- state s = new state(c1, c2, c3, c4);
- s.display();
- sc.close();
- }
- }
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:
👇
- import java.util.*;
- abstract class Staff
- {
- protected int id;
- protected String name;
- Staff(int id, String name)
- {
- this.id = id;
- this.name = name;
- }
- abstract public void display();
- }
- class OfficeStaff extends Staff
- {
- String dept;
- OfficeStaff(int id, String name, String dept)
- {
- super(id,name);
- this.dept = dept;
- }
- public void display()
- {
- System.out.println("\t"+id+"\t"+name+"\t"+dept);
- System.out.println("--------------------------------------------");
- }
- }
- public class absclass
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter Total Number of Employee:");
- int n = sc.nextInt();
- OfficeStaff s[] = new OfficeStaff[n];
- for(int i=0; i<n; i++)
- {
- System.out.println("\nEnter Number-"+(i+1)+"Emplyoee Details\n");
- System.out.println("Enter ID");
- int id = sc.nextInt();
- System.out.println("Enter Employee Name:");
- String name = sc.next();
- System.out.println("Enter Dept Name:");
- String dept = sc.next();
- s[i] = new OfficeStaff(id, name, dept);
- }
- System.out.println("\t\t\tEmpolyee Details");
- System.out.println("-------------------------------------");
- System.out.println("\tID\tName\tDept");
- System.out.println("-------------------------------------");
- for(int i=0; i<n; i++)
- {
- s[i].display();
- }
- sc.close();
- }
- }
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:
👇
- import java.util.*;
- interface operation
- {
- void area();
- void volume();
- final double PI = 3.142;
- }
- class cylinder implements operation
- {
- int r;
- int h;
- cylinder(int r, int h)
- {
- this.r = r;
- this.h = h;
- }
- public void area()
- {
- System.out.println("Area: "+(2*PI*r*h+2*PI*r)+" sq units\n");
- }
- public void volume()
- {
- System.out.println("Volume:"+(PI*r*r*h)+" cubic units\n");
- }
- }
- public class CylinderVol
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter radius:");
- int r = sc.nextInt();
- System.out.println("Enter Height:");
- int h = sc.nextInt();
- cylinder c = new cylinder(r,h);
- c.area();
- c.volume();
- sc.close();
- }
- }
d) Write a program to find the cube of given number using function interface.
CODE:
👇
- import java.util.*;
- interface function
- {
- void cube(int n);
- }
- class demo implements function
- {
- public void cube(int n)
- {
- System.out.println("Cube: "+(n*n*n));
- }
- }
- public class funinterface
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter Number for Finding Cube:");
- int n = sc.nextInt();
- demo d = new demo();
- d.cube(n);
- sc.close();
- }
- }
👇
- import java.util.*;
- abstract class order
- {
- int id;
- String descp;
- Scanner sc=new Scanner(System.in);
- public void setData(int id,String descp)
- {
- this.id=id;
- this.descp=descp;
- }
- abstract public void accept();
- abstract public void display();
- }
- class purchase_order extends order
- {
- String cname;
- public void accept()
- {
- System.out.println("Enter Customer Name :");
- String n=sc.nextLine();
- cname=n;
- }
- public void display()
- {
- System.out.println("\t"+id+"\t"+descp+"\t\t"+cname);
- }
- }
- class sales_order extends order
- {
- String vname;
- public void accept()
- {
- System.out.println("Enter Vendor name:");
- String n=sc.nextLine();
- vname=n;
- }
- public void display()
- {
- System.out.println("\t"+id+"\t"+descp+"\t\t"+vname);
- }
- }
- public class Sales
- {
- public static void main(String[]args)
- {
- Scanner sc=new Scanner(System.in);
- purchase_order p[]=new purchase_order[3];
- for(int i=0; i<3; i++)
- {
- p[i]=new purchase_order();
- System.out.println("\nEnter "+(i+1)+" Customer data : ");
- System.out.println("Enter ID:");
- int cid=sc.nextInt();
- System.out.println("Enter Description: ");
- String desc=sc.next();
- sc.nextLine();
- p[i].setData(cid,desc);
- p[i].accept();
- }
- System.out.println("\n\t\tPurchased Details.\n");
- System.out.println("\tID\tDescription\tCname");
- for(int i=0;i<3;i++)
- {
- p[i].display();
- }
- sales_order s[]=new sales_order[3];
- for(int i=0; i<3; i++)
- {
- s[i]=new sales_order();
- System.out.println("\nEnter "+(i+1)+" Vender data : ");
- System.out.println("Enter ID:");
- int cid=sc.nextInt();
- System.out.println("Enter Description: ");
- String desc=sc.next();
- sc.nextLine();
- s[i].setData(cid,desc);
- s[i].accept();
- }
- System.out.println("\n\t\tSales Details.\n");
- System.out.println("\tID\tDescription\tVname");
- for(int i=0;i<3;i++)
- {
- s[i].display();
- }
- sc.close();
- }
- }
- import java.util.*;
- interface MarkerInt
- {
- }
- class product implements MarkerInt
- {
- int pid,pcost,quantity;
- String pname;
- static int cnt ;
- //Default constructor
- product()
- {
- pid=1;
- pcost=10;
- quantity=1;
- pname="pencil";
- cnt++;
- }
- //Parameterized constructor
- product(int id, String n, int c, int q)
- {
- pid=id;
- pname=n;
- pcost=c;
- quantity=q;
- cnt++;
- System.out.println("\nCOUNT OF OBJECT IS: "+cnt+ "\n");
- }
- public void display()
- {
- System.out.println("\t"+pid+"\t"+pname+"\t"+pcost+"\t"+quantity);
- }
- }
- public class MarkerInterface
- {
- public static void main(String[]args)
- {
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter number of Product: ");
- int n=sc.nextInt();
- product pr[]=new product[n];
- for(int i=0; i<n; i++)
- {
- System.out.println("\nEnter "+(i+1)+ " Product details :\n");
- System.out.println("Enter product ID");
- int pid=sc.nextInt();
- sc.nextLine();
- System.out.println("Enter product name : ");
- String pn=sc.next();
- sc.nextLine();
- System.out.println("Enter product cost : ");
- int pc=sc.nextInt();
- System.out.println("Enter product quatity :");
- int pq=sc.nextInt();
- pr[i]=new product(pid,pn,pc,pq);
- }
- System.out.println("\n\t\tProduct details\n");
- System.out.println("\tId\tPname\tCost\tQuantity\n");
- for(int i=0; i<n; i++)
- {
- pr[i].display();
- }
- sc.close();
- }
- }