Ad

Polymorphism


Polymorphism

Task 1:

Make a class Faculty that is inherited by 2 classes PermanentFaculty and VisitingFaculty.
Class Faculty has two attributes id and name, fully parametrized constructor and calculateSalary() method.
Class PermanentFaculty has one attribute salary, fully parametrized constructor and overrides calculateSalary() method.
Class VisitingFaculty() has two attributes hours,salaryPerHr, fully parametrized constructor and overrides calculateSalary() method.
In PermanantFaculty class, calculateSalry method returns basic salary.  In VisitingFaculty class, calculateSalry method returns hours*salaryPerHr.
In Test class create an array of Faculty (n elements) and ask user which object data to enter, 1 for PermanentFaculty, 2 for Visiting. Input data for appropriate object and save in the array.
After n objects are saved, show salary of all objects through polymorphic processing.
Now update salary of permanantFaculty by 10%. (HINT: You will have to first check for required Faculty using the instanceof operator, then, you will have to downcast to set appropriate value of the object)




                        ////////////////////////////////////////////Main class////////////////////////////////////////



package com.lab.polymorhism;

/**
 *
 * @author Usman Hashmi
 */
import java.util.*;
public class TestClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the no of employee:");
        int n = input.nextInt();
        Faculty[] fa = new Faculty[n];
     
        System.out.println("Enter choice 1 for permanentFaculty and 2 for visitingFaculty");
        int var = input.nextInt();
        for(int i=0;i<var;i++)
        {
        switch (var) {
            case 1:
                System.out.println("Enter trhe Employee ID:");
                int id  = input.nextInt();
                System.out.println("Enter the Name:");
                String name  = input.nextLine();
                System.out.println("Enter the salary:");
                double salary = input.nextDouble();
                fa[i] = new PermanentFaculty(salary,id,name);
           
                break;
            case 2:
               System.out.println("Enter trhe Employee ID:");
                int id1  = input.nextInt();
                System.out.println("Enter the Name:");
                String name1  = input.nextLine();
                System.out.println("Enter the hour:");
                int hour = input.nextInt();
                System.out.println("Enter the salary per hour:");
                double perHour = input.nextDouble();
                fa[i] = new VisitingFaculty(hour,perHour,id1,name1);
           
            default:
                System.out.println("Invalid");
               break;
        }}
        for (Faculty faculty : fa) {
            System.out.println(faculty.calculateSalary());
         
        }
    }
 
}

Post a Comment

0 Comments