A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Oracle asked Java Question: Let’s consider there is a List of Employees class and that display all data like mechanical engineers list, computer software engineer list and other list. Can we find index-wise number of employees of same type and if list contains empty then also display the index?

Oracle asked Java Question for Java developer - techieuncle.com
Use the following picture and write the code.

techieuncle.com




import java.util.ArrayList;
import java.util.Collection;

class Employee
{
       String name;
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
       @Override
       public String toString() {
              return "Employee [name=" + name + "]";
       }
             
}

class SEmployee extends Employee
{
}
class EEmployee extends Employee
{
      
}
class MEmployee extends Employee
{
      
}
public class Test
{
    public static void main(String a[])
    {
       //create employee object
       Employee  empObj1=new Employee();
       empObj1.setName("Heera babu");
       Employee  empObj2=new Employee();
       empObj2.setName("Dev D");
       Employee  empObj3=new Employee();
       empObj3.setName("Navi K");
       Employee  empObj4=new Employee();
       empObj4.setName("Pawan K");
       
      
       //create EmployeeList
         ArrayList<Employee> emplist = new ArrayList<>();
         emplist.add(empObj1);
         emplist.add(empObj2);
         emplist.add(empObj3);
         emplist.add(empObj4);
        
       
       //create Software engineer object
       SEmployee  emp1=new SEmployee();
       emp1.setName("Satish");
       SEmployee  emp2=new SEmployee();
       emp2.setName("Bhanu");
       SEmployee  emp3=new SEmployee();
       emp3.setName("Nitu");
       SEmployee  emp4=new SEmployee();
       emp4.setName("Tarun");
        
         //create Software engineer List
         ArrayList<SEmployee> SElist = new ArrayList<>();
         SElist.add(emp1);
         SElist.add(emp2);
         SElist.add(emp3);
         SElist.add(emp4);
        
      MEmployee  memp1=new MEmployee();
      memp1.setName("Raj");

         //create Mech engineer List
         ArrayList<MEmployee> MElist = new ArrayList<>();
         MElist.add(memp1);
        
         //create Electronic engineer List
         ArrayList<EEmployee> Elist = new ArrayList<>();
        
         //adding otherlist in main list
         emplist.addAll(SElist);
         emplist.addAll(Elist);
         emplist.addAll(MElist);
        
         int flag = 0;
         if(emplist.contains(emplist.iterator().next()))
         {
         for (MEmployee eEmployee : MElist) {
              flag=1;
                System.out.println("Mechanical Employee List:"+eEmployee);             
         }
        
         for (EEmployee eEmployee : Elist) {
              flag=2;
              System.out.println("ElectEngineer  Employee List:"+eEmployee);         
         }
        
         for (SEmployee eEmployee : SElist) {
              flag=3;
                System.out.println("Software Engineer Employee List:"+eEmployee);            
         }
        
         }
        
         if(MElist.isEmpty())
         {
              System.out.println("No Mechanical Engineer in Employee List"); 
         }
         if(Elist.isEmpty())
         {
              System.out.println("No ElectEngineer Engineer in Employee List"); 
         }
         if(SElist.isEmpty())
         {
              System.out.println("No Software Engineer in Employee List"); 
         }

         //System.out.println("index=0 List value: "+emplist.get(0));
    }
}




Console Output:

Mechanical Employee List:Employee [name=Raj]
Software Engineer Employee List:Employee [name=Satish]
Software Engineer Employee List:Employee [name=Bhanu]
Software Engineer Employee List:Employee [name=Nitu]
Software Engineer Employee List:Employee [name=Tarun]
No ElectEngineer Engineer in Employee List

LEARN TUTORIALS

.

.