Tuesday, July 30, 2024

JAVA-I Assignment 3

 Assignment 3: Inheritance and Interfaces


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.

import java.util.Scanner;

class continent {

    String cName;

}

class country extends continent {

    String country_name;

}

class state extends country {

    String state_name;

    String place;

    state(String c1, String c2, String c3, String c4) {

        cName = c1;

        country_name = c2;

        state_name = c3;

        place = c4;

    }


    public void display() {

        System.out.println("\n\t\t\t\t Data \t\t");

        System.out.println("------------------------------------------------------------------------");

        System.out.println("|\tContinent\tCountry\t\tState\t\tPlace\t\t|");

        System.out.println("------------------------------------------------------------------------");

        System.out.println("|\t" + cName + "\t\t" + country_name + "\t\t" + state_name + "\t\t" + place + "\t\t|");

        System.out.println("------------------------------------------------------------------------");

    }

}


public class multilevel {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter Continent Name: ");

        String c1 = sc.nextLine();

        System.out.println("Enter Country Name: ");

        String c2 = sc.nextLine();

       System.out.println("Enter State Name: ");

        String c3 = sc.nextLine();

        System.out.println("Enter Place Name: ");

        String c4 = sc.nextLine();

        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 details.

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|\t" + id + "\t\t" + name + "\t\t" + dept + "\t\t" + "|");

        System.out.println("------------------------------------------------------------------------");

    }

}


public class abclass {

    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 " + (i + 1) + " number employee Details: \n");


            System.out.println("Enter ID: ");

            int id = sc.nextInt();


            sc.nextLine();

            System.out.println("Enter Employee Name: ");

            String name = sc.nextLine();


            System.out.println("Enter Dept Name: ");

            String dept = sc.nextLine();


            s[i] = new OfficeStaff(id, name, dept);


        }

        System.out.println("\t\t\t\t Employee Details");

        System.out.println("------------------------------------------------------------------------");

        System.out.println("\t|\tID\t\tName\t\tDept\t\t|");

        System.out.println("------------------------------------------------------------------------");

        for (int i = 0; i < n; i++) {

            s[i].display();

        }

        sc.close();

    }

}


c) Define an interface “Operation” which has methods 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 calculate the area and volume.

import java.util.Scanner;


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 * 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.

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();


        function d = new demo();

        d.cube(n);


        sc.close();

    }

}



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 details.

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();


            sc.nextLine();

            System.out.println("Enter Description :");

            String desc = 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)+" Vendor Data :");


            System.out.println("Enter ID :");

            int cid = sc.nextInt();


            sc.nextLine();

            System.out.println("Enter Description :");

            String desc = 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();

    }

}


b) Write a program to using marker 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 contents of each object and Also display the object count.

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.nextLine();


            System.out.println("Enter Product Cost:");

            int pc = sc.nextInt();


            System.out.println("Enter Product Quantity:");

            int pq = sc.nextInt();


            pr[i] = new product(pid, pn, pc, pq);

        }

        System.out.println("\n\t\t Product Details\n");

        System.out.println("\tId\tPname\tCost\tQuantity\n");

        for (int i = 0; i < n; i++) {

            pr[i].display();

        }

        sc.close();

    }


}


Set C

a) Create an interface Department containing attributes deptName and deptHead. It also has abstract methods for printing the attributes. Create a class hostel containing hostelName, hostelLocation and numberOfRooms. The class contains method printing the attributes. Then write Student class extending the Hostel class and implementing the Department interface. This class contains attributes studentName, regNo, electiveSubject and avgMarks. Write suitable printData method for this class. Also, implement the abstract methods of the Department interface. Write a driver class to test the Student class. The program should be menu driven containing the options:

i. Admit new student

ii. Migrate a student

iii. Display details of a student

For the third option, a search is to be made on the basis of the entered registration Number.


import java.util.Scanner;


interface Department {

    String deptName = "Computer Science";

    String deptHead = "Dr.Ladda Sir";


    void printData();

}


class Hostel {

    String hostelName;

    String hostelLocation;

    int roomNumber;


    Hostel(String hostelName, String hostelLocation, int roomNumber) {

        this.hostelName = hostelName;

        this.hostelLocation = hostelLocation;

        this.roomNumber = roomNumber;

    }


    void printData() {

        System.out.print(hostelName + "\t\t" + hostelLocation + "\t\t\t" + roomNumber +"\n");

    }

}


class Student extends Hostel implements Department {

    String studentName;

    int regNo;

    String electiveSubject;

    double avgMarks;


    Student(int regNo, String studentName, String electiveSubject, double avgMarks, String hostelName,

            String hostelLocation, int roomNumber) {

        super(hostelName, hostelLocation, roomNumber);

        this.studentName = studentName;

        this.regNo = regNo;

        this.electiveSubject = electiveSubject;

        this.avgMarks = avgMarks;

    }


    public void printData() {

        System.out.println("\n----------------------------------------------------------------------------------------------------------");

        System.out.print("RegNo\tStud Name\tElected Sub\tAvgMarks\tHostelName\tHostelLocation\t\tRoomNumber");

        System.out.println("\n----------------------------------------------------------------------------------------------------------");

        System.out.print(regNo + "\t" + studentName + "\t\t" +

                electiveSubject + "\t\t" + avgMarks + "\t\t");

                super.printData();

    }

}


public class hostelProg {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        int n = 0, choice, i, flag = 0;

        Student s[] = new Student[20];

        do {

            System.out.println("\n1.Admit new Student");

            System.out.println("2.Migrate a student by Hostel");

            System.out.println("3.Display details of a student");

            System.out.println("0.Exit");

            System.out.println("\nEnter your Choice");

            choice = sc.nextInt();

            switch (choice) {

                case 1:

                    System.out.print("Enter Reg number : ");

                    int regNo = sc.nextInt();

                    sc.nextLine();


                    System.out.print("Enter Student Name : ");

                    String studentName = sc.nextLine();


                    System.out.print("Enter Subject : ");

                    String electiveSubject = sc.nextLine();


                    System.out.print("Enter AvgMarks : ");

                    double avgMarks = sc.nextDouble();

                    sc.nextLine();


                    System.out.print("Enter Hostel Name : ");

                    String hostelName = sc.nextLine();


                    System.out.print("Enter Hostel Location: ");

                    String hostelLocation = sc.nextLine();


                    System.out.print("Enter Room Number : ");

                    int roomNumber = sc.nextInt();


                    s[n] = new Student(regNo, studentName, electiveSubject, avgMarks, hostelName, hostelLocation,

                            roomNumber);

                    n++;

                    System.out.println("Sucessfully Record Added");

                    break;


                case 2:

                    if (n > 0) {

                        flag = 0;

                        System.out.println("Migrate Student by Hostel Name");

                        System.out.print("Enter Reg Number : ");

                        int rno = sc.nextInt();

                        sc.nextLine();


                        for (i = 0; i < n; i++) {

                            if (s[i].regNo == rno) {

                                System.out.print("Enter New Hostel Name : ");

                                String hname = sc.nextLine();


                                System.out.print("Enter New Hostel Location : ");

                                String lname = sc.nextLine();


                                System.out.print("Enter New Room Number : ");

                                int rnumber = sc.nextInt();


                                s[i].hostelName = hname;

                                s[i].hostelLocation = lname;

                                s[i].roomNumber = rnumber;

                                flag = 1;

                            }

                        }

                        if (flag == 0) {

                            System.out.println("Invalid Reg Number or Student Name");

                        }

                    } else {

                        System.out.println("Empty Students Records ");

                    }

                    break;

                case 3:

                    if (n > 0) {

                        flag = 0;

                        System.out.print("Enter Reg Number : ");

                        int rno = sc.nextInt();

                        for (i = 0; i < n; i++) {

                            if (s[i].regNo == rno) {

                                s[i].printData();

                                flag = 1;

                                break;

                            }

                        }

                        if (flag == 0) {

                            System.out.println("Invalid Reg Number ");

                        }

                    } else {

                        System.out.println("Empty Students Records ");

                    }

                    break;

                case 0:

                    System.out.println("Thank You !!");

                    break;

                default:

                    System.out.println("Invalid choice");

            }

        } while (choice != 0);

        sc.close();

    }

}


/*

PS C:\Users\shubham Deshmukh\Desktop\College\Java\Practical Assignments\Practical Assignment 3> javac hostelProg.java

PS C:\Users\shubham Deshmukh\Desktop\College\Java\Practical Assignments\Practical Assignment 3> java hostelProg


1.Admit new Student

2.Migrate a student by Hostel 

3.Display details of a student

0.Exit


Enter your Choice

1

Enter Reg number : 11

Enter Student Name : Shubham

Enter Subject : Math

Enter AvgMarks : 87

Enter Hostel Name : Private

Enter Hostel Location: Sangamner

Enter Room Number : 78

Sucessfully Record Added


1.Admit new Student     

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

1

Enter Reg number : 56

Enter Student Name : Prasad

Enter Subject : Java

Enter AvgMarks : 89

Enter Hostel Name : Govt

Enter Hostel Location: Vadgaon

Enter Room Number : 67

Sucessfully Record Added


1.Admit new Student

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

3

Enter Reg Number : 11


----------------------------------------------------------------------------------------------------------

RegNo   Stud Name       Elected Sub     AvgMarks        HostelName      HostelLocation          RoomNumber

----------------------------------------------------------------------------------------------------------

11      Shubham         Math            87.0            Private         Sangamner                       78


1.Admit new Student

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

3

Enter Reg Number : 56


----------------------------------------------------------------------------------------------------------

RegNo   Stud Name       Elected Sub     AvgMarks        HostelName      HostelLocation          RoomNumber

----------------------------------------------------------------------------------------------------------

56      Prasad          Java            89.0            Govt            Vadgaon                 67


1.Admit new Student

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

2

Migrate Student by Hostel Name

Enter Reg Number : 56

Enter New Hostel Name : Private

Enter New Hostel Location : Sangamner

Enter New Room Number : 78


1.Admit new Student

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

3

Enter Reg Number : 56


----------------------------------------------------------------------------------------------------------

RegNo   Stud Name       Elected Sub     AvgMarks        HostelName      HostelLocation          RoomNumber

----------------------------------------------------------------------------------------------------------

56      Prasad          Java            89.0            Private         Sangamner                       78


1.Admit new Student

2.Migrate a student by Hostel

3.Display details of a student

0.Exit


Enter your Choice

0

Thank You !!

*/


No comments:

Post a Comment