The Aquaman
The Aquaman

Reputation: 3

comparison logic in this if statement

I was wondering if you can explain to me this comparison logic:

public class Workplace {
    private int index = 0;
    private String name;
    private Employee[] employees;

    public Workplace(String name, int numberOfEmployees){
        this.name = name;
        this.employees = new Employee[numberOfEmployees];
    }

    public Employee[] returnEmployees(Employee e){
        Employee youngestEmployee = null;
        Employee oldestEmployee = null;

        for(int i=0; i<index; i++){
            if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
                youngestEmployee = employees[i];
            }
            else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
                oldestEmployee = employees[i];
            }
        }
        return new Employee[] {youngestEmployee, oldestEmployee};
    }
}

In particular, the if statement comparison logic over here:

if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
    youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
    oldestEmployee = employees[i];
}

I've got another class called Employee, in which I have declared the get and set methods. And have the age variable declared as an int. So I know that this if statement comparison logic wants to get the youngest and oldest employee but I am getting confused at this part:

employees[i].getAge() > youngestEmployee.getAge()

Instead of saying less than, here it is greater than. I couldn't find an answer yet. Thank you for your time!

Upvotes: 0

Views: 73

Answers (2)

Blessing
Blessing

Reputation: 2720

Lets suppose

this.employees = new Employee[5]; Employee youngestEmployee = null;

The logic here is that it's looping through a list of all employees(currently we have five employees) and takes the current employee meaning the employee at i(employees[i]) in that list of employees that you declared on the top and use a get method or a getter from your Employee class to get the age of that current employee from you employees array class and then compare it with the age of the previous employee(youngestEmployee). At first the youngestEmployee=null so it will set youngestEmployee=employees[i](suppose the value of employees[i]=25) but at the second time it will compare the age of the employee at i(employees[i]) and then compare it to the previous value(which was 25). Then it will decide the new value of youngestEmployee accoding to the result it got from the if statement.

Upvotes: 0

Federico Bicciato
Federico Bicciato

Reputation: 76

You are right to doubt on the code. The comparison looks wrong.

if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
    youngestEmployee = employees[i];
}

This says: if the age of the current employee is greater than the youngest, set the current as the youngest. It should be the contrary.

Upvotes: 2

Related Questions