0% found this document useful (0 votes)
5 views15 pages

Second Highest Salary Employee Code

Uploaded by

vivekpvns678
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Second Highest Salary Employee Code

Uploaded by

vivekpvns678
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1.

Return Employee of second Highest Salary

import [Link].*;
class Employee
{
private int EmployeeId;
private String EmployeeName;
private int age;
private char gender;
private double salary;

public Employee(int EmployeeId,String EmployeeName,int age,char gender,double


salary)
{
[Link]=EmployeeId;
[Link]=EmployeeName;
[Link]=age;
[Link]=gender;
[Link]=salary;
}
public int getEmpId()
{
return EmployeeId;
}
public String getEmployeName()
{
return EmployeeName;
}
public int getAge()
{
return age;
}
public char getGender()
{
return gender;
}
public double getSalary()
{
return salary;
}
}

public class Main


{
public static Employee getEmpWithSecLowestSalry(Employee[] obj)
{
Employee minn=null;
Employee sec_min=null;
for(Employee emp: obj)
{
if(minn==null || [Link]()>[Link]())
{
sec_min=minn;
minn=emp;
}
else if ((sec_min == null || [Link]() < sec_min.getSalary())
&& [Link]() > [Link]()) {
sec_min = emp;
}
}
return sec_min;
}
public static int countBasedOnAge(Employee[] obj,int age)
{
int count=0;
for(Employee emp: obj)
{
if(age==[Link]())
{
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int n=[Link]();
Employee[] emp=new Employee[n];
for(int i=0;i<n;i++)
{
int a=[Link]();
[Link]();
String b=[Link]();
int c=[Link]();
[Link]();
char d=[Link]().charAt(0);
[Link]();
double e=[Link]();
[Link]();
emp[i]=new Employee(a,b,c,d,e);
}
int age=[Link]();

Employee res=getEmpWithSecLowestSalry(emp);
if(res!=null)
{
[Link]([Link]());
[Link]([Link]());
}
else
{
[Link]("Null");
}
int res2=countBasedOnAge(emp,age);
if(res2!=0)
{
[Link](res2);
}
else
{
[Link]("No employee found for the given age");
}
}
}
2. sort array object

import [Link].*;
class Course
{
private int courseId;
private String courseName;
private String courseAdmin;
private int quiz;
private int handson;

public Course(int courseId,String courseName,String courseAdmin,int quiz,int


handson)
{
[Link]=courseId;
[Link]=courseName;
[Link]=courseAdmin;
[Link]=quiz;
[Link]=handson;
}
public void setCourseId(int courseId)
{
[Link]=courseId;
}
public int getCourseId()
{
return courseId;
}
public String getCourseName()
{
return courseName;
}
public void setCourseName(String courseName)
{
[Link]= courseName;
}
public String getCourseAdmin()
{
return courseAdmin;
}
public void setCourseAdmin(String courseAdmin)
{
[Link]=courseAdmin;
}
public int getQuiz()
{
return quiz;
}
public void setQuiz(int quiz)
{
[Link]=quiz;
}
public int getHandson()
{
return handson;
}
public void setHandson(int handson)
{
[Link]=handson;
}
}
public class Main
{
public static double findAvgByAdmin(Course[] arr, String Admin)
{
int count=0;
double avg=0;
for(Course course: arr)
{
if([Link]([Link]()))
{
count++;
avg+=[Link]();
}
}
if(count==0)
{
return 0;
}
return avg/count;
}

public static Course[] setCourseByHandsOn(Course[] arr,int handson)


{
List<Course> obj=new ArrayList<>();
int count=0;
for(Course cs: arr)
{
if([Link]()<handson)
{
count++;
}
}

Course[] obj2=new Course[count];


int index=0;
for(Course cs: arr)
{
if([Link]()<handson)
{
obj2[index++]=cs;
}
}
[Link](obj2,[Link](Course:: getQuiz));
return obj2;

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
int n;
n=[Link]();
[Link]();

Course[] course=new Course[n];


int i;
for(i=0;i<n;i++)
{
int a=[Link]();
[Link]();
String b=[Link]();
String c=[Link]();
int d=[Link]();
[Link]();
int e=[Link]();
[Link]();
course[i]=new Course(a,b,c,d,e);

}
String admin=[Link]();
int quiz=[Link]();
[Link]();
double res=findAvgByAdmin(course,admin);
if(res!=0)
{
[Link](res);
}
else
{
[Link]("No course found");
}
Course[] res2=setCourseByHandsOn(course,quiz);
if(res2!=null)
{
for(Course arr: res2)
{
[Link]([Link]());
[Link]([Link]());

}
}
else
{
[Link]("No course found with mentiioned attribute");
}

3. Return second max employee based on ..........

import [Link].*;
class Employee
{
private int employeeld;
private String name;
private String branch;
private double rating;
private boolean transport;

public Employee(int employeeld,String name,String branch,double rating,boolean


transport)
{
[Link]=employeeld;
[Link]=name;
[Link]=branch;
[Link]=rating;
[Link]=transport;
}
public void setEmployeeld(int employeeld)
{
[Link]=employeeld;
}
public int getEmployeeld()
{
return employeeld;
}
public String getName()
{
return name;
}
public void setName(String name)
{
[Link]= name;
}
public String getBranch()
{
return branch;
}
public void setBranch(String branch)
{
[Link]=branch;
}
public double geRating()
{
return rating;
}
public void seRating(double rating)
{
[Link]=rating;
}
public boolean getTransport()
{
return transport;
}
public void setTransport(boolean transport)
{
[Link]=transport;
}
}
public class Main
{
public static int findCountOfEmployeesUsingCompTransport(Employee[] arr, String
str)
{
int count=0;
for(Employee emp: arr)
{
if([Link]([Link]()) && [Link]()==true)
{
count++;
}
}
return count;
}

public static Employee findEmployeeWithSecondHighestRating(Employee[] arr)


{
Employee maxx=null;
Employee sec_max=null;
for(Employee obj: arr )
{
if([Link]()==false)
{
if(maxx==null || [Link]()<[Link]())
{
sec_max=maxx;
maxx=obj;
}
else if((sec_max==null || sec_max.geRating()<[Link]()) &&
[Link]()<[Link]())
{
sec_max=obj;
}
}

}
return sec_max;
}

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
int n;
n=[Link]();
[Link]();

Employee[] emp=new Employee[n];


int i;
for(i=0;i<n;i++)
{
int a=[Link]();
[Link]();
String b=[Link]();
String c=[Link]();
double d=[Link]();
[Link]();
boolean e=[Link]();
[Link]();
emp[i]=new Employee(a,b,c,d,e);

}
String branch=[Link]();

int res=findCountOfEmployeesUsingCompTransport(emp,branch);
if(res!=0)
{
[Link](res);
}
else
{
[Link]("Not found with given attribute");
}
Employee obj=findEmployeeWithSecondHighestRating(emp);
if(obj!=null)
{
[Link]([Link]());
[Link]([Link]());
}
else
{
[Link]("All using company transposrt");
}

3. Sort array of obj


import [Link].*;
class Medicine
{
private String medicineName;
private String batch;
private String disease;
private int price;

public Medicine(String medicineName,String batch,String disease,int price)


{
[Link]=medicineName;
[Link]=batch;
[Link]=disease;
[Link]=price;
}
public String getMedicineName()
{
return medicineName;
}
public void setMedicineName(String medicineName)
{
[Link]=medicineName;
}
public String getBatch()
{
return batch;
}
public void setBatch(String batch)
{
[Link]=batch;
}
public String getDisease()
{
return disease;
}
public void setDisease(String disease)
{
[Link]=disease;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
[Link]=price;
}
}

public class Main


{
public static Medicine[] getPriceByDisease(Medicine[] arr, String disease)
{
int count=0;
List<Medicine> med=new ArrayList<>();
for(Medicine obj: arr)
{
if([Link]([Link]()))
{
[Link](obj);
count++;
}
}
// [Link](med,[Link](Medicine::getPrice));

// Medicine[] me=[Link](new Medicine[0]);


Medicine[] me=new Medicine[count];
int indx=0;
for(Medicine obj: arr)
{
if([Link]([Link]()))
{
me[indx++]=obj;
}
}
[Link](me,[Link](Medicine::getPrice));
if([Link]>0)
{
return me;

}
return null;

public static void main(String[] args)


{

Scanner sc=new Scanner([Link]);


int n=[Link]();
[Link]();
int i;
Medicine[] medc=new Medicine[n];
for(i=0;i<n;i++)
{
String a=[Link]();
String b=[Link]();
String c=[Link]();
int d=[Link]();
[Link]();
medc[i]=new Medicine(a,b,c,d);
}
String dis=[Link]();
Medicine[] res=getPriceByDisease(medc,dis);
if(res!=null)
{
for(Medicine arr: res)
{
[Link]([Link]());
}
}
else
{
[Link]("No medicine found with given disease");

}
}

4. Institution based on set and get Rating

import [Link].*;
class Institution
{
private int instituteId;
private String instituteName;
private int noOfStdPlaced;
private int noOfStdCleared;
private String location;
private String grade;

public Institution(int instituteId,String instituteName,int noOfStdPlaced,int


noOfStdCleared,String location)
{
[Link]=instituteId;
[Link]=instituteName;
[Link]=noOfStdPlaced;
[Link]=noOfStdCleared;
[Link]=location;

}
public int getId()
{
return instituteId;
}

public void setId(int instituteId)


{
[Link]=instituteId;
}
public String getName()
{
return instituteName;
}

public void setName()


{
[Link]=instituteName;
}

public int getPlacedStd()


{
return noOfStdPlaced;
}

public void setPlacedStd(int noOfStdPlaced)


{
[Link]=noOfStdPlaced;
}

public int getClearedStd()


{
return noOfStdCleared;
}

public void setClearedStd(int noOfStdCleared)


{
[Link]=noOfStdCleared;
}

public String getLocation()


{
return location;
}

public void setLocation(String location)


{
[Link]=location;
}

public String getGrade()


{
return grade;
}

public void setGrade(String grade)


{
[Link]=grade;
}

public class Main


{

public static int FindNumClearanceByLocation(Institution[] arr, String


location)
{
int sum=0;
for(Institution ins: arr)
{
if([Link]([Link]()))
{
sum=sum+[Link]();
}
}
if(sum>0)
{
return sum;
}
return 0;
}

public static Institution UpdateInstitutionGrade(Institution[] arr,String


nameIns)
{
Institution obj=null;
int rating=0;
for(Institution ins: arr)
{
if([Link]([Link]()))
{
rating=([Link]()*100)/[Link]();
obj=ins;
}

}
if(rating>=80)
{
[Link]("A");
}
else
{
[Link]("B");
}
return obj;

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
int n=[Link]();
[Link]();
Institution[] ins=new Institution[n];
int i;
for(i=0; i<n; i++)
{
int a=[Link]();
[Link]();
String b=[Link]();
int c=[Link]();
[Link]();
int d=[Link]();
[Link]();
String e=[Link]();
// String f=[Link]();
ins[i]=new Institution(a,b,c,d,e);
}
String location=[Link]();
String NameIns=[Link]();
int res1=FindNumClearanceByLocation(ins,location);
if(res1>0)
{
[Link](res1);
}
else
{
[Link]("There are no cleared students in this particular
location.");
}
Institution obj=UpdateInstitutionGrade(ins,NameIns);
if(obj!=null)
{
[Link]([Link]()+"::"+[Link]());
}
else
{
[Link]("No Institute is avialable with the specified
name");
}
}
}

5. max and return obj

import [Link].*;
class TravelAgency
{
private int regNo;
private String agencyName;
private String packageType;
private int price;
private boolean flightFacility;

public TravelAgency(int regNo,String agencyName,String packageType,int


price,boolean flightFacility)
{
[Link]=regNo;
[Link]=agencyName;
[Link]=packageType;
[Link]=price;
[Link]=flightFacility;
}

public int getRegNo()


{
return regNo;
}
public void getRegNo(int regNo)
{
[Link]=regNo;
}
public String getAgencyName()
{
return agencyName;
}
public void setAgencyName(String agencyName)
{
[Link]=agencyName;
}
public String getPackageType()
{
return packageType;
}
public void getPackageType(String packageType)
{
[Link]=packageType;
}
public int getPrice()
{
return price;
}
public void setPrice(int price)
{
[Link]=price;
}
public boolean getFlightFacility()
{
return flightFacility;
}
public void getFlightFacility(boolean flightFacility)
{
[Link]=flightFacility;
}
}
public class Main
{

public static int findAgencyWithHighestPackagePrice(TravelAgency[] arr)


{
int maxx=Integer.MIN_VALUE;

for(TravelAgency trv: arr)


{
if(maxx<[Link]())
{
maxx=[Link]();

}
}
return maxx;
}

public static TravelAgency AgencyDetailForGivenIdAndType(TravelAgency[]


arr,int reg,String packageType)
{
TravelAgency obj=null;
for(TravelAgency trv: arr)
{
if([Link]() && reg==[Link]() &&
[Link]([Link]()))
{
obj=trv;
}
}
return obj;
}

public static void main(String[] args)


{
Scanner sc=new Scanner([Link]);
int n=[Link]();
[Link]();
TravelAgency[] travel=new TravelAgency[n];
int i;

for(i=0;i<n;i++)
{
int a=[Link]();
[Link]();
String b=[Link]();
String c=[Link]();
int d=[Link]();
[Link]();
boolean e=[Link]();
[Link]();
travel[i]=new TravelAgency(a,b,c,d,e);
}
int reg=[Link]();
[Link]();
String typ=[Link]();
int res1=findAgencyWithHighestPackagePrice(travel);
[Link](res1);
TravelAgency res2=AgencyDetailForGivenIdAndType(travel,reg,typ);
[Link]([Link]()+"::"+[Link]());
}
}

Common questions

Powered by AI

Using 'Arrays.sort()' in Java is significant because it provides a convenient, efficient way to sort collections of custom objects based on specified properties by allowing developers to define comparison logic through comparators. It optimizes sorting at a complexity of O(n log n), leveraging highly tuned quicksort or mergesort algorithms internally. This illustrates Java's robust handling of object arrays and enhances readability and maintainability of code when sorting isn't essential to application logic, only needed for final output or aggregation .

The use of null-check handling in methods such as 'getEmpWithSecLowestSalry' reflects a defensive programming tactic common in Java, aimed at handling potential incorrect or unexpected inputs gracefully. By checking if certain variables remain null before proceeding, these methods avoid null pointer exceptions, instead returning a meaningful placeholder (e.g., null) when conditions aren't met, suggesting robust practices for error prevention and code resilience .

Custom comparators in Java are employed to define custom sorting logic for objects; as seen with the 'Medicine' class where 'Arrays.sort()' uses 'Comparator.comparingInt' to sort based on price. This approach encapsulates the sorting rule, 'getPrice()', within a comparator, allowing objects without natural ordering to be ordered logically, supporting nuanced data arrangements critical for meeting application-specific requirements .

The 'findEmployeeWithSecondHighestRating' function filters employees by checking if 'emp.getTransport()' returns 'false', indicating they do not use company transport. Only employees meeting this criterion are considered for the maximum and second-maximum rating evaluation. This could be useful to identify and reward employees not utilizing company resources, often incentivizing personal expense management or recognizing distinctive performance behaviors in a second category .

The 'findAvgByAdmin' function calculates the average quiz score by iterating over a 'Course' object array, summing the quiz scores of courses administered by a specified admin (case insensitive), and dividing this sum by the count of such courses. It handles the edge case of no matching courses by returning 0 to indicate the absence of scores, avoiding division by zero .

The 'getEmpWithSecLowestSalry' method determines the employee with the second-lowest salary by iterating through an array of 'Employee' objects. It maintains two variables, 'minn' and 'sec_min', which store the employees with the lowest and second-lowest salaries, respectively. Initially, both are null. For each employee, if 'minn' is null or the current employee's salary is less than 'minn's salary, 'minn' and 'sec_min' are updated. If neither condition is met, and the current salary is higher than 'minn' but lower than 'sec_min', then 'sec_min' is updated. The method assumes that the input array contains at least two employees with different salaries to function correctly .

The 'setCourseByHandsOn' method creates a list of 'Course' objects whose 'handson' score is less than a given threshold. It creates this list by iterating through the array of courses and counting those that satisfy the condition, allowing for array allocation. Then, it sorts this new array using 'Arrays.sort()', passing a comparator that sorts courses based on their 'quiz' attribute in ascending order, achieved through 'Comparator.comparingInt(Course::getQuiz)' .

The method 'findAgencyWithHighestPackagePrice' determines the travel agency with the highest package price by iterating through an array of 'TravelAgency' objects, using a variable 'maxx' initialized to 'Integer.MIN_VALUE'. As it iterates, it compares each agency's price to 'maxx' and updates 'maxx' if a larger price is encountered. This method assumes the existence of at least one agency entry to produce a meaningful result .

The 'UpdateInstitutionGrade' function calculates the grade for an institution based on the ratio of 'PlacedStd' to 'ClearedStd', expressed as a percentage. If this value is 80% or higher, the method sets the grade to 'A', otherwise it is set to 'B'. This logic is implemented based on the integer division of the total number of placed students by the total number cleared .

The 'findCountOfEmployeesUsingCompTransport' method limits its search scope by filtering employees based on both the branch match and their usage of company transport, in a single logical statement using '&&'. By narrowing the scope preemptively, it efficiently counts only the subset satisfying both conditions, reducing processing time as it avoids unnecessary checks, enhancing performance in large datasets. The implication is a focused, efficient handling of employee records for specific logistics management tasks .

You might also like