Search code examples
javasortingcomparable

compareTo() in two different classes, one not reachable


I have two different classes with two different compareTo()-methods. When I sort the list, I would like it to first sort by age. But if it's two Fysiker getting sorted, and their ages are the same, I would like it to then sort on "year", which is the year they started studying.

When I run my main class, I put 80 Fysiker in a list, I sort them, and then I print them. They are all sorted by age, but not by year (if they've got the same age).

Edit1 (why it's not a duplicate): The reason I can't use abstract to solve this (which they have done in the duplicate marked version of this question), is because I need to be able to instantiate a Human by writing Human humantest = new Human().

Main class:

public class Main {
public static void main(String[] args) {

    ArrayList<Human> randomFysiker = new ArrayList<Human>();
    for (int j=0;j<80;j++) {

        Fysiker f = new Fysiker();
        randomFysiker.add(f);
}       
    Collections.sort(randomFysiker);

    System.out.println(randomFysiker);
}


}

Human class:

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Human implements Comparable<Human> {

int age;
String name;
List<String> people = Arrays.asList("A","B","C","D","E","F","G","H","I","J","K","L","M","N","P");
Random randomGenerator = new Random();

public Human(int myAge, String myName) {
    name = myName;
    age = myAge;
}

public Human() {
    this.age = randomAge();
    this.name = randomName();
        }

public int compareTo(Human o) {
    return this.age - o.age;
}

private int randomAge() {
    return randomGenerator.nextInt(100);
}

protected String randomName() {
    return people.get(randomGenerator.nextInt(people.size()));
}

public int getAge() {
    return this.age;
}

public String getName() {
    return name;
}

public String toString() {
    return "\nName: " + name + "\nAge: " + age + " yrs old\n";
}
}

Fysiker class:

public class Fysiker extends Human {

private int year;

public Fysiker(int myAgeF, String myNameF, int myYearF) {
    this.name = myNameF;
    this.age = myAgeF;
    this.year = myYearF;
}

public Fysiker() {
    this.name = randomName();
    this.age = randomAge();
    this.year = randomYear();
}

public int compareTo(Fysiker o) {
    if (this.age - o.age != 0) {
        return super.compareTo(o);
    }
    else {
        return this.year - o.year;
    }
}

private int randomAge() {
    return 15 + randomGenerator.nextInt(86);
}

private int randomYear() {
    int yearfifteen = 2015 - age + 15;
    if (yearfifteen >= 1932 && 2015 >= yearfifteen){
        return randomGenerator.nextInt(age-14) + yearfifteen; //(max-min)=(yearfifteen+age-15+1)-(yearfifteen+1)
    }
    else {
        return 1932 + randomGenerator.nextInt(84);
    }
}

public int getYear(){
    return year;
}

public String toString() {
    return super.toString() + "Started Physics: " + String.format("F%02d", this.year%100) + "\n";
}
}

Solution

  • The compareTo()-method of Fysiker should be changed to;

    public int compareTo(Human o) {
        if(this.age==o.age && o instanceof Fysiker) {
            return this.year - ((Fysiker)o).year;
        }
        return super.compareTo(o);
    }
    

    If the two Humans have the same age and both are of class Fysiker, then they will be sorted by year with this method.