1

For my assignment, I have been asked to carry out the following:

  • Compare the ages of the Animals so that the animals can be ordered from highest to lower age.

I've created the if statements that should do this if I'm correct but I'm not sure how to refer to each Animal within the array list so I've used pseudocode and refereed to the first array in the ArrayList as "AL1" the second as "AL2" and so on.

Here is the code:

Demo class

import java.util.ArrayList;
public class Demo {

    public static ArrayList<Animal> animalGroup; // make this an instance static variable to access in other class with main method;

    public static void main(String[] args) 
{
                   animalGroup = new ArrayList<>();

 //Add new Animals with properties such as name and age?           
animalGroup.add(new Wolf("Sam", 5));
animalGroup.add(new Parrot("George", 3));
animalGroup.add(new Wolf("Wesley", 7));
animalGroup.add(new Parrot("Pat", 10));

System.out.println("Compare Aimal ages" + AL1.getAge().compareTo(AL2.getAge, AL3.getAge(), AL4.getAge()));

int result = AL1AGE.compareTo(AL2AGE, AL3AGE, AL4AGE);

if(result == 3)
    System.out.println(AL1.getName() + "comes before " + AL2.getName() + ", " + AL3.getName() + " and " + A|L4.getName());
if(result == 5)
        System.out.println(AL2.getName() + "comes before " + AL3.getName() + ", " + AL4.getName() + " and  after " + A|L1.getName());
if(result == 7)
            System.out.println(AL3.getName() + "comes before " + AL4.getName() + " and after " + AL1.getName() + " and " + A|L2.getName());
else
                System.out.println(AL4.getName() + "comes after " + AL1.getName() + ", " + AL2.getName() + " and " + A|L3.getName());

}

}

Main method call

import java.util.ArrayList;
import java.util.Collections;

public class Main {


    public static void main(String[] args) 
    {

    System.out.println("************ArrayList after sorting************");


Collections.sort(animalGroup);
Collections.reverse(animalGroup);

for(Animal animal2 : animalGroup) {
         System.out.println(animal2.getName()+","+animal2.getAge());
}
}
}

Desired output

Sam comes before George, Wesley and Pat
George comes before Wesley, Pat and after Sam
Wesley comes before Pat and after Sam and George
Pat comes after Sam, George and Wesley

Any help on how do accomplish the desired outcome would be much appreciated, thanks.

1
  • What about a Comparator?Have you tried it? Commented Nov 28, 2016 at 7:02

4 Answers 4

1

Make a Class thats implements Comparator. like this:

import java.util.Comparator;

public class AgeComparator implements Comparator<Animal>{
    @Override
    public int compare(Animal animal1, Animal animal2) {
        if(animal1.getAge() < animal2.getAge()){
            return 1;
        } else if (animal1.getAge() > animal2.getAge()) {
            return -1;
        } else {
            return 0;
        }
    }

}

and sort it using the AgeComparator class you create to Collections.sort().

public static void main(String[] args) {

    System.out.println("************ArrayList after sorting************");

    Collections.sort(animalGroup,new AgeComparator()); // Sort highest to lowest

    for (Animal animal2 : animalGroup) {
        System.out.println(animal2.getName() + "," + animal2.getAge());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

if you are using Java-8 than you can sort your list using comparator with lambda like this :

animalGroup.sort((animalGroup r1,animalGroup r2) -> r1.getAge() - r2.getAge());

Comments

0

Implement comparator in separate class and implement its method and sort them by age and use two parameter instead of one in sort method. comparator tutorial

Comments

0

Do as bellow..

Create Animal POJO class

public class Animal implements Comparable<Animal>{

    private String animalName;
    private int animalAge;

    public Animal(String animalName,int animalAge){

        this.animalName = animalName;
        this.animalAge = animalAge;
    }

    public String getAnimalName() {
        return animalName;
    }
    public void setAnimalName(String animalName) {
        this.animalName = animalName;
    }

    public int getAnimalAge() {
        return animalAge;
    }
    public void setAnimalAge(int animalAge) {
        this.animalAge = animalAge;
    }

    @Override
    public int compareTo(Animal o) {

        return this.getAnimalAge() - o.getAnimalAge();
    }

    @Override
    public String toString() {

        return "Animal name: "+getAnimalName()+" , Age:  "+getAnimalAge();
    }
}

Main class as bellow...

public class Sample {

    public static void main(String[] args) {

        List<Animal> animalList = new ArrayList<Animal>();

        animalList.add(new Animal("abc",3));
        animalList.add(new Animal("bac",6));
        animalList.add(new Animal("cve",19));
        animalList.add(new Animal("yyy",13));
        animalList.add(new Animal("zzz",9));
        animalList.add(new Animal("ttt",33));

        // Before sorting

        for (Animal animal : animalList) {

            System.out.println(animal.toString());
        }

        System.out.println("-------------------------");

        // Sorting the animals 

        Collections.sort(animalList);

        // After sorting

        for (Animal animal : animalList) {

            System.out.println(animal.toString());
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.