logo

Crowdly

Browser

Add to Chrome

Assume the following class Animal and its use are given: package com.comp...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

Assume the following class Animal and its use are given:

package com.company;

import java.util.Comparator;

import java.util.SortedSet;

import java.util.TreeSet;

class Animal implements Comparable<Animal>{

final String kind;

final String name;

final int age;

Animal(String kind, String name, int age) {

this.kind = kind;

this.name = name;

this.age = age;

}

@Override

public int compareTo (Animal other){

int retVal = kind.compareTo(other.kind);

if (retVal == 0) {retVal= name.compareTo(other.name);}

return retVal;

}

/*...*/

static class MyComparator implements Comparator<Animal> {

/*...*/

public int compare( Animal a1, Animal a2){

return a2.age-a1.age;

}

}

}

public static class Main(String[] args){

SortedSet<Animal> animals = new TreeSet<>();

animals.add(new Animal(“Cat”,”Zero”,2));

animals.add(new Animal(“Cat”, “Filou”, 5));

animals.add(new Animal(“Dog”, “Puppy”, 3));

SortedSet<Animal> animals2 = new TreeSet<>(new Animal.MyComparator());

animals2.addAll(animals);

}

 

Which of the following statements are true?

a) The set animals will be ordered as follows: Filou – Puppy - Zero

b) The set animals will be ordered as follows: Filou – Zero – Puppy

c) The set animals2 is ordered as follows: Zero – Puppy - Filou

d) When multiple sorting schemes should be provided, create and use multiple Comparators.

More questions like this

Want instant access to all verified answers on moodle.jku.at?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome