Created class:

public class Employee { private String fullName; private String position; private String email; private int phoneNumber; private int salary; private byte age; //геттеры и сеттеры public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(int phoneNumber) { this.phoneNumber = phoneNumber; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public byte getAge() { return age; } public void setAge(byte age) { this.age = age; } //параметризированный конструктор public Employee(String fullName, String position, String email, int phoneNumber, int salary, byte age) { this.fullName = fullName; this.position = position; this.email = email; this.phoneNumber = phoneNumber; this.salary = salary; this.age = age; } 

In another class, an array is created:

 public class EmployeeDemo { public static void main(String[] args) { Employee [] mas = new Employee[5]; mas[0] = new Employee("KozlovAA", "developer", "kozlovaa@mail.ru", 123456, 10000, (byte) 30); mas[1] = new Employee("IvanovAA", "engineer", "IvanovAA@mail.ru", 321654, 15000, (byte) 35); mas[2] = new Employee("PetrovEE", "manager", "PetrovEE@mail.ru", 234651, 8000, (byte) 40); mas[3] = new Employee("SidorovAA", "seller", "SidorovAA@mail.ru", 654321, 7000, (byte) 45); mas[4] = new Employee("AbramovichRO", "businessman", "AbramovichRO@mail.ru", 112233, 100000, (byte) 50); } } 

It is necessary in the EmployeeDemo class to create a method that, using a cycle, will output to the console all the objects of this array, whose field is age> 40; This method does not work - please tell me what am I doing wrong?

 public void sortOfAge() { for (int i = 0; i < mas.length; i++) { if (this.getAge() > 40) { System.out.println("ФИО: " + fullName); System.out.println("должность: " + position); System.out.println("email: " + email); System.out.println("телефон: " + phoneNumber); System.out.println("зарплата: " + salary); System.out.println("возраст: " + age); } } 

    1 answer 1

    In the sortOfAge method, sortOfAge need to iterate through the array and select only those that match the criteria for display. Those. there must be something like this:

     for(Employee employee: array){ if(employee.age<40){ System.out.println("ФИО: " + fullName); System.out.println("должность: " + position); System.out.println("email: " + email); System.out.println("телефон: " + phoneNumber); System.out.println("зарплата: " + salary); System.out.println("возраст: " + age); } } 

    Or in a more functional style:

     Arrays .stream(employees) .filter(e -> e.age > 40) .forEach(System.out::println); 
    • Arrays .stream (employees) .filter (e -> e.age> 40) .forEach (System.out :: println); From this I am still far away: ( - Dmitry
    • The problem is that I don’t see the mas array from the method and don’t understand how to access the age field. public void sortOfAge() { for(Employee employee: mas){ if(employee.age<40){ System.out.println("ФИО: " + fullName); System.out.println("должность: " + position); System.out.println("email: " + email); System.out.println("телефон: " + phoneNumber); System.out.println("зарплата: " + salary); System.out.println("возраст: " + age); } } public void sortOfAge() { for(Employee employee: mas){ if(employee.age<40){ System.out.println("ФИО: " + fullName); System.out.println("должность: " + position); System.out.println("email: " + email); System.out.println("телефон: " + phoneNumber); System.out.println("зарплата: " + salary); System.out.println("возраст: " + age); } } public void sortOfAge() { for(Employee employee: mas){ if(employee.age<40){ System.out.println("ФИО: " + fullName); System.out.println("должность: " + position); System.out.println("email: " + email); System.out.println("телефон: " + phoneNumber); System.out.println("зарплата: " + salary); System.out.println("возраст: " + age); } } - Dmitry
    • define a method like this public static void sortOfAge (Employee [] array), then you can call it from the main method as follows: sortOfAge (mass) - Artem Konovalov
    • Exactly, it turned out, thank you very much. And tell me please, and how to turn to the age field? if (employee.age <40) - this code does not work - as I understand it because the age variable is encapsulated, and I don’t understand how to compare it with the value 40 through the getter, and even in the array. - Dmitry
    • In my opinion, in the first code it should be like this: employee.getAge() > 40 - the "more" sign. And in both cases .getAge() , not .age . - Regent