JAVA-11
Q.11 Create class EMPLOYEE in java with id, name and salary as data-members. Again create 5 different employee objects by taking input from user. Display all the information of an employee which is having maximum salary.
Solution :-
import java.util.*;
class Employee {
int id;
String name;
double salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
public class EmployeeMaxSalary {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Employee[] employees = new Employee[5];
for (int i = 0; i < 5; i++) {
System.out.println("Enter details for Employee " + (i + 1) + ":");
System.out.print("ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
employees[i] = new Employee(id, name, salary);
}
Employee maxSalaryEmp = employees[0];
for (int i = 1; i < employees.length; i++) {
if (employees[i].salary > maxSalaryEmp.salary) {
maxSalaryEmp = employees[i];
}
}
System.out.println("\nEmployee with Maximum Salary:");
maxSalaryEmp.display();
scanner.close();
}
}