Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this snippet of code from an assignment of mine that is supposed to rand

ID: 3623829 • Letter: I

Question

I have this snippet of code from an assignment of mine that is supposed to rand people by priority and I can't seen to order the if statements correctly. It'll prioritize by name but thats it.
What I need to happen is this...if a perosn is less than 18 years old it should prioritize the two people by last name first, then first name if last names are the same. Lexicographically.

If they are older than 18 years old they should be compared by what sex they are first. Female is prioritized over male. Male is a boolean value. If they are both the same sex then they are compared by how much income they have. If they have the same income then they should be compared by their last name, then compared by first name if last name is the same.

From what I have written, it seems like it would work, but it doesn't :/


private int age; //parameters that we were told we should use.
private String fname;
private String lname;
private static boolean male = true;
private static boolean female = false;
private int income;

public Evacuee(java.lang.String fname, java.lang.String lname, boolean male, int age, int income)
{
this.fname = fname;
this.lname = lname;

this.male = male;
this.age = age;
this.income = income;
}


@Override
public int compareTo(Evacuee e)
{
if(age < 18) //for kids' prioritization
{
if (age != e.age)
return age - e.age;
if(lname.compareTo(e.lname) != 0)
return lname.compareTo(e.lname);
return fname.compareTo(e.fname);
}

if(age > 18) // for adult prioritization
{ if(male == true && e.male == true) //if both evacuees are males...
{
if(income - e.income != 0)
{
return lname.compareTo(e.lname);
}
return fname.compareTo(e.fname);
}
if(male == true && e.male == false)
return 1;
if(male == false && e.male == true)
return -1;
if(male == false && e.male == false)
{
if(income - e.income !=0)
{
return lname.compareTo(e.lname);
}
}
}

return fname.compareTo(e.fname);

Explanation / Answer

public int compareTo(Evacuee e)
{
/*
if a perosn is less than 18 years old it should prioritize the two people by last name first, then first name if last names are the same. Lexicographically.
*/

if(age < 18 || e.age < 18)
{
// last name
if(!lname.equals(e.lname))
return lname.compareTo(e.lname);
// first name
else
return fname.compareTo(e.fname);
}
/*
If they are older than 18 years old they should be compared by what sex they are first. Female is prioritized over male. Male is a boolean value. If they are both the same sex then they are compared by how much income they have. If they have the same income then they should be compared by their last name, then compared by first name if last name is the same.
*/

else
{
// females > males
if(male != e.male)
return male?1:-1;
// income
else if(income != e.income)
return income - e.income;
// last name
else if(!lname.equals(e.lname))
return lname.compareTo(e.lname);
// first name
else
return fname.compareTo(e.fname);
}
}

Also, you don't need two booleans for gender. If a person is male, then male is true. Otherwise, male is false. female is redundant. There are only two possibilities.